diff --git a/Cargo.toml b/Cargo.toml index 74f5715..a9f7039 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ authors = ["Kyle Clemens "] edition = "2018" [features] -default = ["logic"] +default = ["logic", "with_serde"] logic = [ "cssparser", @@ -14,23 +14,37 @@ logic = [ "lazy_static", "scraper", ] +with_serde = [ + "serde", + "serde_derive", + "url_serde", +] [dependencies] cssparser = { version = "0.24", optional = true } failure = { version = "0.1", optional = true } lazy_static = { version = "1", optional = true } scraper = { version = "0.9", optional = true } -serde = "1" -serde_derive = "1" -serde_json = "1" +serde = { version = "1", optional = true } +serde_derive = { version = "1", optional = true } url = "1" -url_serde = "0.2" +url_serde = { version = "0.2", optional = true } -[dependencies.chrono] +# with serde +[target.'cfg(feature = "with_serde")'.dependencies.chrono] version = "0.4" features = ["serde"] -[dependencies.ffxiv_types] +[target.'cfg(feature = "with_serde")'.dependencies.ffxiv_types] version = "1" default-features = false features = ["worlds", "races", "clans", "guardians", "with_serde"] + +# without serde +[target.'cfg(not(feature = "with_serde"))'.dependencies.chrono] +version = "0.4" + +[target.'cfg(not(feature = "with_serde"))'.dependencies.ffxiv_types] +version = "1" +default-features = false +features = ["worlds", "races", "clans", "guardians"] diff --git a/src/error.rs b/src/error.rs index 910f38a..7207e6a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,7 +20,7 @@ pub enum Error { impl Error { pub fn missing_element(select: &scraper::Selector) -> Self { use cssparser::ToCss; - let css = select.selectors.iter().map(|x| x.to_css_string()).collect::>().join(" "); + let css = select.selectors.iter().map(ToCss::to_css_string).collect::>().join(" "); Error::MissingElement(css) } diff --git a/src/lib.rs b/src/lib.rs index 07bdc42..1a1f037 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,7 @@ #![feature(crate_visibility_modifier)] +#![allow(clippy::unreadable_literal)] -#[cfg(feature = "logic")] extern crate failure; -#[cfg(feature = "logic")] #[macro_use] extern crate lazy_static; pub extern crate ffxiv_types; -#[macro_use] extern crate serde_derive; #[cfg(feature = "logic")] pub mod error; diff --git a/src/logic.rs b/src/logic.rs index 724ba45..6fd2fd0 100644 --- a/src/logic.rs +++ b/src/logic.rs @@ -14,6 +14,8 @@ use scraper::{ macro_rules! selectors { ($($name:ident => $selector:expr);+$(;)?) => { + use lazy_static::lazy_static; + lazy_static! { $( static ref $name: scraper::Selector = scraper::Selector::parse($selector).unwrap(); @@ -38,7 +40,7 @@ crate fn plain_parse(html: &Html, select: &scraper::Selector) -> Result let string = html .select(select) .next() - .ok_or(Error::missing_element(select))? + .ok_or_else(|| Error::missing_element(select))? .text() .collect(); Ok(string) @@ -48,13 +50,13 @@ crate fn plain_parse_elem<'a>(html: ElementRef<'a>, select: &scraper::Selector) let string = html .select(select) .next() - .ok_or(Error::missing_element(select))? + .ok_or_else(|| Error::missing_element(select))? .text() .collect(); Ok(string) } -crate fn parse_id<'a>(a: &'a Element) -> Result { +crate fn parse_id(a: &Element) -> Result { let href = a.attr("href").ok_or_else(|| Error::invalid_content("href on link", None))?; let last = href .split('/') diff --git a/src/logic/character.rs b/src/logic/character.rs index ab55632..032128f 100644 --- a/src/logic/character.rs +++ b/src/logic/character.rs @@ -135,7 +135,7 @@ fn parse_rcg(html: &Html) -> Result<(Race, Clan, Gender)> { fn parse_guardian(html: &Html) -> Result { let guardian_str = plain_parse(&html, &*PROFILE_GUARDIAN)?; guardian_str - .split(",") + .split(',') .next() .ok_or_else(|| Error::invalid_content("first part of guardian", Some(&guardian_str))) .and_then(|x| Guardian::from_str(&x) @@ -206,7 +206,7 @@ fn parse_jobs(html: &Html) -> Result> { Ok(jobs) } -fn parse_job<'a>(elem: ElementRef<'a>) -> Result<(Job, JobInfo)> { +fn parse_job(elem: ElementRef) -> Result<(Job, JobInfo)> { let job = crate::logic::plain_parse_elem(elem, &*CLASS_NAME) .and_then(|x| Job::parse(&x).ok_or_else(|| Error::invalid_content("valid job", Some(&x))))?; diff --git a/src/logic/free_company.rs b/src/logic/free_company.rs index 70abd12..3b06cb2 100644 --- a/src/logic/free_company.rs +++ b/src/logic/free_company.rs @@ -97,11 +97,11 @@ fn parse_pvp_rank(html: &Html, select: &scraper::Selector) -> Result let rank_str = plain_parse(html, select)?; let rank = rank_str - .split(":") + .split(':') .nth(1) .ok_or_else(|| Error::invalid_content("colon-separated text", Some(&rank_str))) .and_then(|x| x - .split(" ") + .split(' ') .next() .ok_or_else(|| Error::invalid_content("space-separated text", Some(&rank_str))))?; @@ -119,16 +119,16 @@ fn parse_formed(html: &Html) -> Result> { let script = html .select(&*FC_FORMED) .next() - .ok_or(Error::missing_element(&*FC_FORMED))? + .ok_or_else(|| Error::missing_element(&*FC_FORMED))? .inner_html(); let timestamp = script .split("strftime(") .nth(1) - .ok_or(Error::invalid_content("strftime call", Some(&script)))? - .split(",") + .ok_or_else(|| Error::invalid_content("strftime call", Some(&script)))? + .split(',') .next() - .ok_or(Error::invalid_content("comma-separated strftime call", Some(&script)))?; + .ok_or_else(|| Error::invalid_content("comma-separated strftime call", Some(&script)))?; let timestamp: i64 = timestamp.parse().map_err(Error::InvalidNumber)?; let utc = Local.timestamp(timestamp, 0).with_timezone(&Utc); @@ -154,7 +154,6 @@ fn parse_estate(html: &Html) -> Result> { fn parse_crest(html: &Html) -> Result> { html.select(&*FC_CREST) - .into_iter() .filter_map(|x| x.value().attr("src")) .map(|x| Url::parse(x).map_err(Error::InvalidUrl)) .collect() @@ -173,7 +172,7 @@ fn parse_grand_company(html: &Html) -> Result { fn parse_reputation(html: &Html) -> Result> { let mut reps = BTreeMap::new(); - for elem in html.select(&*FC_REPUTATION).into_iter() { + for elem in html.select(&*FC_REPUTATION) { let name: String = elem .select(&*FC_REPUTATION_NAME) .next() diff --git a/src/logic/linkshell.rs b/src/logic/linkshell.rs index 73e012f..c1f5f51 100644 --- a/src/logic/linkshell.rs +++ b/src/logic/linkshell.rs @@ -63,7 +63,7 @@ fn parse_members(html_str: &str) -> Result> { let results: Vec = html .select(&*ITEM_ENTRY) - .map(|x| parse_single(x)) + .map(parse_single) .collect::>()?; Ok(Paginated { @@ -72,8 +72,7 @@ fn parse_members(html_str: &str) -> Result> { }) } - -fn parse_single<'a>(html: ElementRef<'a>) -> Result { +fn parse_single(html: ElementRef) -> Result { let character = super::search::character::parse_single(html)?; let role = parse_role(html)?; @@ -84,7 +83,7 @@ fn parse_single<'a>(html: ElementRef<'a>) -> Result { }) } -fn parse_role<'a>(html: ElementRef<'a>) -> Result> { +fn parse_role(html: ElementRef) -> Result> { let role = match html.select(&*ITEM_ROLE).next() { Some(r) => r, None => return Ok(None), @@ -104,6 +103,7 @@ mod test { }; use super::parse; + use lazy_static::lazy_static; use ffxiv_types::World; lazy_static! { diff --git a/src/logic/search.rs b/src/logic/search.rs index f76a0b0..b3818c4 100644 --- a/src/logic/search.rs +++ b/src/logic/search.rs @@ -66,7 +66,7 @@ crate fn parse_pagination(html: &Html) -> Result { }) } -crate fn parse_no_results<'a>(html: &Html) -> bool { +crate fn parse_no_results(html: &Html) -> bool { html.select(&*NO_RESULTS) .next() .map(|x| x.text().collect::() == "Your search yielded no results.") diff --git a/src/logic/search/character.rs b/src/logic/search/character.rs index 7602f3d..11a287c 100644 --- a/src/logic/search/character.rs +++ b/src/logic/search/character.rs @@ -41,7 +41,7 @@ pub fn parse(html: &str) -> Result> { let results: Vec = html .select(&*ITEM_ENTRY) - .map(|x| parse_single(x)) + .map(parse_single) .collect::>()?; Ok(Paginated { @@ -50,7 +50,7 @@ pub fn parse(html: &str) -> Result> { }) } -crate fn parse_single<'a>(html: ElementRef<'a>) -> Result { +crate fn parse_single(html: ElementRef) -> Result { let id = parse_id(html)?; let name = plain_parse(html, &*ITEM_NAME)?; @@ -72,7 +72,7 @@ crate fn parse_single<'a>(html: ElementRef<'a>) -> Result { }) } -fn parse_id<'a>(html: ElementRef<'a>) -> Result { +fn parse_id(html: ElementRef) -> Result { let e = html .select(&*ITEM_ID) .next() @@ -80,13 +80,13 @@ fn parse_id<'a>(html: ElementRef<'a>) -> Result { crate::logic::parse_id(e.value()) } -fn parse_world<'a>(html: ElementRef<'a>) -> Result { +fn parse_world(html: ElementRef) -> Result { let world_str = plain_parse(html, &*ITEM_WORLD)?; World::from_str(&world_str) .map_err(|_| Error::invalid_content("valid world", Some(&world_str))) } -fn parse_free_company_id<'a>(html: ElementRef<'a>) -> Result> { +fn parse_free_company_id(html: ElementRef) -> Result> { let elem = match html .select(&*ITEM_FREE_COMPANY) .next() @@ -97,7 +97,7 @@ fn parse_free_company_id<'a>(html: ElementRef<'a>) -> Result> { crate::logic::parse_id(elem.value()).map(Some) } -fn parse_grand_company<'a>(html: ElementRef<'a>) -> Result> { +fn parse_grand_company(html: ElementRef) -> Result> { let text = html .select(&*ITEM_GRAND_COMPANY) .next() @@ -109,7 +109,7 @@ fn parse_grand_company<'a>(html: ElementRef<'a>) -> Result(html: ElementRef<'a>) -> Result { +fn parse_face(html: ElementRef) -> Result { let face_elem = html .select(&*ITEM_FACE) .next() diff --git a/src/logic/search/free_company.rs b/src/logic/search/free_company.rs index 3dc5dc7..5dc1959 100644 --- a/src/logic/search/free_company.rs +++ b/src/logic/search/free_company.rs @@ -47,7 +47,7 @@ pub fn parse(s: &str) -> Result> { let results: Vec = html .select(&*ITEM_ENTRY) - .map(|x| parse_single(x)) + .map(parse_single) .collect::>()?; Ok(Paginated { @@ -56,7 +56,7 @@ pub fn parse(s: &str) -> Result> { }) } -fn parse_single<'a>(html: ElementRef<'a>) -> Result { +fn parse_single(html: ElementRef) -> Result { let id = parse_id(html)?; let grand_company = parse_grand_company(html)?; let name = plain_parse(html, &*ITEM_NAME)?; @@ -82,7 +82,7 @@ fn parse_single<'a>(html: ElementRef<'a>) -> Result { }) } -fn parse_id<'a>(html: ElementRef<'a>) -> Result { +fn parse_id(html: ElementRef) -> Result { let e = html .select(&*ITEM_ID) .next() @@ -90,31 +90,31 @@ fn parse_id<'a>(html: ElementRef<'a>) -> Result { crate::logic::parse_id(e.value()) } -fn parse_grand_company<'a>(html: ElementRef<'a>) -> Result { +fn parse_grand_company(html: ElementRef) -> Result { let gc_str = plain_parse(html, &*ITEM_GRAND_COMPANY)?; GrandCompany::parse(&gc_str) .ok_or_else(|| Error::invalid_content("valid grand company", Some(&gc_str))) } -fn parse_world<'a>(html: ElementRef<'a>) -> Result { +fn parse_world(html: ElementRef) -> Result { let world_str = plain_parse(html, &*ITEM_WORLD)?; World::from_str(&world_str) .map_err(|_| Error::invalid_content("valid world", Some(&world_str))) } -fn parse_crest<'a>(html: ElementRef<'a>) -> Result> { +fn parse_crest(html: ElementRef) -> Result> { html.select(&*ITEM_CREST) .filter_map(|x| x.value().attr("src")) .map(|x| Url::from_str(x).map_err(Error::InvalidUrl)) .collect() } -fn parse_active_members<'a>(html: ElementRef<'a>) -> Result { +fn parse_active_members(html: ElementRef) -> Result { plain_parse(html, &*ITEM_ACTIVE_MEMBERS) .and_then(|x| x.parse().map_err(Error::InvalidNumber)) } -fn parse_estate_built<'a>(html: ElementRef<'a>) -> Result { +fn parse_estate_built(html: ElementRef) -> Result { let estate_built = plain_parse(html, &*ITEM_ESTATE_BUILT)?; let built = match estate_built.as_str() { "Estate Built" => true, @@ -125,20 +125,20 @@ fn parse_estate_built<'a>(html: ElementRef<'a>) -> Result { Ok(built) } -fn parse_formed<'a>(html: ElementRef<'a>) -> Result> { +fn parse_formed(html: ElementRef) -> Result> { let script = html .select(&*ITEM_FORMED) .next() - .ok_or(Error::missing_element(&*ITEM_FORMED))? + .ok_or_else(|| Error::missing_element(&*ITEM_FORMED))? .inner_html(); let timestamp = script .split("strftime(") .nth(1) - .ok_or(Error::invalid_content("strftime call", Some(&script)))? - .split(",") + .ok_or_else(|| Error::invalid_content("strftime call", Some(&script)))? + .split(',') .next() - .ok_or(Error::invalid_content("comma-separated strftime call", Some(&script)))?; + .ok_or_else(|| Error::invalid_content("comma-separated strftime call", Some(&script)))?; let timestamp: i64 = timestamp.parse().map_err(Error::InvalidNumber)?; let utc = Local.timestamp(timestamp, 0).with_timezone(&Utc); @@ -146,7 +146,7 @@ fn parse_formed<'a>(html: ElementRef<'a>) -> Result> { Ok(utc) } -fn parse_active<'a>(html: ElementRef<'a>) -> Result { +fn parse_active(html: ElementRef) -> Result { plain_parse(html, &*ITEM_ACTIVE) .and_then(|x| x .split(": ") @@ -157,7 +157,7 @@ fn parse_active<'a>(html: ElementRef<'a>) -> Result { .ok_or_else(|| Error::invalid_content("valid activity", Some(&x)))) } -fn parse_recruitment<'a>(html: ElementRef<'a>) -> Result { +fn parse_recruitment(html: ElementRef) -> Result { plain_parse(html, &*ITEM_RECRUITMENT) .and_then(|x| x .split(": ") diff --git a/src/logic/search/linkshell.rs b/src/logic/search/linkshell.rs index 480c191..117244e 100644 --- a/src/logic/search/linkshell.rs +++ b/src/logic/search/linkshell.rs @@ -36,7 +36,7 @@ pub fn parse(s: &str) -> Result> { let results: Vec = html .select(&*ITEM_ENTRY) - .map(|x| parse_single(x)) + .map(parse_single) .collect::>()?; Ok(Paginated { @@ -45,7 +45,7 @@ pub fn parse(s: &str) -> Result> { }) } -fn parse_single<'a>(html: ElementRef<'a>) -> Result { +fn parse_single(html: ElementRef) -> Result { let id = parse_id(html)?; let name = plain_parse(html, &*ITEM_NAME)?; let world = parse_world(html)?; @@ -59,7 +59,7 @@ fn parse_single<'a>(html: ElementRef<'a>) -> Result { }) } -fn parse_id<'a>(html: ElementRef<'a>) -> Result { +fn parse_id(html: ElementRef) -> Result { let e = html .select(&*ITEM_ID) .next() @@ -67,13 +67,13 @@ fn parse_id<'a>(html: ElementRef<'a>) -> Result { crate::logic::parse_id(e.value()) } -fn parse_world<'a>(html: ElementRef<'a>) -> Result { +fn parse_world(html: ElementRef) -> Result { let world_str = plain_parse(html, &*ITEM_WORLD)?; World::from_str(&world_str) .map_err(|_| Error::invalid_content("valid world", Some(&world_str))) } -fn parse_active_members<'a>(html: ElementRef<'a>) -> Result { +fn parse_active_members(html: ElementRef) -> Result { plain_parse(html, &*ITEM_ACTIVE_MEMBERS) .and_then(|x| x.parse().map_err(Error::InvalidNumber)) } diff --git a/src/models.rs b/src/models.rs index 7a6024c..2faafb4 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,7 +1,8 @@ macro_rules! ffxiv_enum { ($(#[$meta:meta])* $name:ident { $($variant:ident => $str_repr:expr),+$(,)? }) => { $(#[$meta])* - #[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)] + #[derive(Debug, PartialEq, Clone, Copy)] + #[cfg_attr(feature = "with_serde", derive(serde_derive::Serialize, serde_derive::Deserialize))] pub enum $name { $($variant,)+ } diff --git a/src/models/character.rs b/src/models/character.rs index 2cab3f9..34d17de 100644 --- a/src/models/character.rs +++ b/src/models/character.rs @@ -1,12 +1,12 @@ use super::GrandCompany; +#[cfg(feature = "with_serde")] use serde_derive::{Deserialize, Serialize}; use ffxiv_types::{World, Race, Clan, Guardian}; - use url::Url; use std::collections::BTreeMap; -#[derive(Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct Character { pub id: u64, @@ -22,26 +22,28 @@ pub struct Character { pub city_state: CityState, pub grand_company: Option, - #[serde(with = "crate::util::serde::opt_u64_str")] + #[cfg_attr(feature = "with_serde", serde(with = "crate::util::serde::opt_u64_str"))] pub free_company_id: Option, pub profile_text: String, pub jobs: BTreeMap, - #[serde(with = "url_serde")] + #[cfg_attr(feature = "with_serde", serde(with = "url_serde"))] pub face: Url, - #[serde(with = "url_serde")] + #[cfg_attr(feature = "with_serde", serde(with = "url_serde"))] pub portrait: Url, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[derive(Debug, PartialEq)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct GrandCompanyInfo { pub name: GrandCompany, pub rank: String, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct JobInfo { pub level: Option, pub experience: Option, diff --git a/src/models/free_company.rs b/src/models/free_company.rs index a4e6d6c..51f1f66 100644 --- a/src/models/free_company.rs +++ b/src/models/free_company.rs @@ -1,21 +1,21 @@ use super::GrandCompany; use chrono::{DateTime, Utc}; - use ffxiv_types::World; - +#[cfg(feature = "with_serde")] use serde_derive::{Deserialize, Serialize}; use url::Url; use std::collections::BTreeMap; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct FreeCompany { - #[serde(with = "crate::util::serde::u64_str")] + #[cfg_attr(feature = "with_serde", serde(with = "crate::util::serde::u64_str"))] pub id: u64, pub name: String, pub world: World, pub slogan: String, - #[serde(with = "crate::util::serde::multi_url")] + #[cfg_attr(feature = "with_serde", serde(with = "crate::util::serde::multi_url"))] pub crest: Vec, pub grand_company: GrandCompany, pub active_members: u16, @@ -26,13 +26,15 @@ pub struct FreeCompany { pub reputation: BTreeMap, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct PvpRankings { pub weekly: Option, pub monthly: Option, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct Estate { pub name: String, pub address: String, diff --git a/src/models/linkshell.rs b/src/models/linkshell.rs index 4f42c9f..1e5986e 100644 --- a/src/models/linkshell.rs +++ b/src/models/linkshell.rs @@ -1,10 +1,12 @@ use super::search::{Paginated, character::CharacterSearchItem}; use ffxiv_types::World; +#[cfg(feature = "with_serde")] use serde_derive::{Deserialize, Serialize}; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct Linkshell { - #[serde(with = "crate::util::serde::u64_str")] + #[cfg_attr(feature = "with_serde", serde(with = "crate::util::serde::u64_str"))] pub id: u64, pub name: String, pub world: World, @@ -12,9 +14,10 @@ pub struct Linkshell { pub members: Paginated, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct LinkshellMember { - #[serde(flatten)] + #[cfg_attr(feature = "with_serde", serde(flatten))] pub character: CharacterSearchItem, pub role: Option, } diff --git a/src/models/search.rs b/src/models/search.rs index c8f10cd..7727e8c 100644 --- a/src/models/search.rs +++ b/src/models/search.rs @@ -1,15 +1,19 @@ +#[cfg(feature = "with_serde")] use serde_derive::{Deserialize, Serialize}; + pub mod character; pub mod free_company; pub mod linkshell; -#[derive(Debug, Default, Serialize, Deserialize)] +#[derive(Debug, Default)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct Pagination { pub current_page: u64, pub total_pages: u64, pub total_results: u64, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct Paginated { pub pagination: Pagination, pub results: Vec, diff --git a/src/models/search/character.rs b/src/models/search/character.rs index 27d518c..40a712c 100644 --- a/src/models/search/character.rs +++ b/src/models/search/character.rs @@ -1,17 +1,19 @@ use crate::models::character::GrandCompanyInfo; use ffxiv_types::World; +#[cfg(feature = "with_serde")] use serde_derive::{Deserialize, Serialize}; use url::Url; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct CharacterSearchItem { pub id: u64, pub name: String, pub world: World, pub grand_company: Option, - #[serde(with = "crate::util::serde::opt_u64_str")] + #[cfg_attr(feature = "with_serde", serde(with = "crate::util::serde::opt_u64_str"))] pub free_company_id: Option, - #[serde(with = "url_serde")] + #[cfg_attr(feature = "with_serde", serde(with = "url_serde"))] pub face: Url, } diff --git a/src/models/search/free_company.rs b/src/models/search/free_company.rs index 6de578a..731fae5 100644 --- a/src/models/search/free_company.rs +++ b/src/models/search/free_company.rs @@ -1,18 +1,18 @@ use crate::models::GrandCompany; use chrono::{DateTime, Utc}; - use ffxiv_types::World; - +#[cfg(feature = "with_serde")] use serde_derive::{Deserialize, Serialize}; use url::Url; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct FreeCompanySearchItem { - #[serde(with = "crate::util::serde::u64_str")] + #[cfg_attr(feature = "with_serde", serde(with = "crate::util::serde::u64_str"))] pub id: u64, pub name: String, pub world: World, - #[serde(with = "crate::util::serde::multi_url")] + #[cfg_attr(feature = "with_serde", serde(with = "crate::util::serde::multi_url"))] pub crest: Vec, pub grand_company: GrandCompany, pub active_members: u16, diff --git a/src/models/search/linkshell.rs b/src/models/search/linkshell.rs index 8a48541..be9bc0f 100644 --- a/src/models/search/linkshell.rs +++ b/src/models/search/linkshell.rs @@ -1,8 +1,10 @@ use ffxiv_types::World; +#[cfg(feature = "with_serde")] use serde_derive::{Deserialize, Serialize}; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug)] +#[cfg_attr(feature = "with_serde", derive(Deserialize, Serialize))] pub struct LinkshellSearchItem { - #[serde(with = "crate::util::serde::u64_str")] + #[cfg_attr(feature = "with_serde", serde(with = "crate::util::serde::u64_str"))] pub id: u64, pub name: String, pub world: World, diff --git a/src/util.rs b/src/util.rs index 82b2d09..e79490b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1 +1 @@ -pub mod serde; +#[cfg(feature = "with_serde")] pub mod serde; diff --git a/src/util/serde/multi_url.rs b/src/util/serde/multi_url.rs index 691d686..1247b47 100644 --- a/src/util/serde/multi_url.rs +++ b/src/util/serde/multi_url.rs @@ -2,6 +2,7 @@ use serde::{Deserializer, Deserialize, Serializer, ser::SerializeSeq}; use url::Url; +#[allow(clippy::ptr_arg)] crate fn serialize(urls: &Vec, serializer: S) -> Result where S: Serializer, { @@ -16,5 +17,5 @@ crate fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> where D: Deserializer<'de> { let urls: Vec> = Vec::deserialize(deserializer)?; - Ok(urls.into_iter().map(|u| u.into_inner()).collect()) + Ok(urls.into_iter().map(url_serde::De::into_inner).collect()) } diff --git a/src/util/serde/u64_str.rs b/src/util/serde/u64_str.rs index 3296eaa..0e5c684 100644 --- a/src/util/serde/u64_str.rs +++ b/src/util/serde/u64_str.rs @@ -1,5 +1,6 @@ use serde::{Deserializer, Deserialize, Serializer, de::Unexpected}; +#[allow(clippy::trivially_copy_pass_by_ref)] crate fn serialize(u: &u64, serializer: S) -> Result where S: Serializer, {