refactor: add deserialisation to all models

This commit is contained in:
Anna 2018-09-03 17:14:48 -04:00
parent 2a03b46696
commit 1775e7a7e2
6 changed files with 35 additions and 18 deletions

View File

@ -6,6 +6,7 @@ use crate::{
use scraper::Html; use scraper::Html;
pub mod character; pub mod character;
pub mod free_company;
selectors!( selectors!(
PAGINATION_TOTAL => ".parts__total"; PAGINATION_TOTAL => ".parts__total";

View File

@ -1,7 +1,7 @@
macro_rules! ffxiv_enum { macro_rules! ffxiv_enum {
($(#[$meta:meta])* $name:ident { $($variant:ident => $str_repr:expr),+$(,)? }) => { ($(#[$meta:meta])* $name:ident { $($variant:ident => $str_repr:expr),+$(,)? }) => {
$(#[$meta])* $(#[$meta])*
#[derive(Debug, Serialize)] #[derive(Debug, Serialize, Deserialize)]
pub enum $name { pub enum $name {
$($variant,)+ $($variant,)+
} }

View File

@ -2,7 +2,7 @@ use super::GrandCompany;
use ffxiv_types::{World, Race, Clan, Guardian}; use ffxiv_types::{World, Race, Clan, Guardian};
#[derive(Debug, Serialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Character { pub struct Character {
pub id: u64, pub id: u64,
@ -23,7 +23,7 @@ pub struct Character {
pub profile_text: String, pub profile_text: String,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct GrandCompanyInfo { pub struct GrandCompanyInfo {
pub grand_company: GrandCompany, pub grand_company: GrandCompany,
pub rank: String, pub rank: String,

View File

@ -8,13 +8,13 @@ use url::Url;
use std::collections::BTreeMap; use std::collections::BTreeMap;
#[derive(Debug, Serialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct FreeCompany { pub struct FreeCompany {
pub id: u64, pub id: u64,
pub name: String, pub name: String,
pub world: World, pub world: World,
pub slogan: String, pub slogan: String,
#[serde(serialize_with = "multi_url")] #[serde(with = "multi_url")]
pub crest: Vec<Url>, pub crest: Vec<Url>,
pub grand_company: GrandCompany, pub grand_company: GrandCompany,
pub active_members: u16, pub active_members: u16,
@ -25,27 +25,42 @@ pub struct FreeCompany {
pub reputation: BTreeMap<GrandCompany, u8>, pub reputation: BTreeMap<GrandCompany, u8>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct PvpRankings { pub struct PvpRankings {
pub weekly: Option<u64>, pub weekly: Option<u64>,
pub monthly: Option<u64>, pub monthly: Option<u64>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Estate { pub struct Estate {
pub name: String, pub name: String,
pub address: String, pub address: String,
pub greeting: String, pub greeting: String,
} }
fn multi_url<S>(urls: &Vec<Url>, serializer: S) -> Result<S::Ok, S::Error> mod multi_url {
where S: serde::Serializer, use serde::{Deserializer, Deserialize, Serializer, ser::SerializeSeq};
{
use serde::ser::SerializeSeq;
let mut seq = serializer.serialize_seq(Some(urls.len()))?; use url::Url;
for url in urls {
seq.serialize_element(&url_serde::Ser::new(url))?; crate fn serialize<S>(urls: &Vec<Url>, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,
{
let mut seq = serializer.serialize_seq(Some(urls.len()))?;
for url in urls {
seq.serialize_element(&url_serde::Ser::new(url))?;
}
seq.end()
}
crate fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Url>, D::Error>
where D: Deserializer<'de>
{
#[derive(Deserialize)]
struct Wrapper(#[serde(with = "url_serde")] Url);
let urls = Vec::deserialize(deserializer)?;
Ok(urls.into_iter().map(|Wrapper(u)| u).collect())
} }
seq.end()
} }

View File

@ -1,13 +1,14 @@
pub mod character; pub mod character;
pub mod free_company;
#[derive(Debug, Serialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Pagination { pub struct Pagination {
pub current_page: u64, pub current_page: u64,
pub total_pages: u64, pub total_pages: u64,
pub total_results: u64, pub total_results: u64,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Paginated<T> { pub struct Paginated<T> {
pub pagination: Pagination, pub pagination: Pagination,
pub results: Vec<T>, pub results: Vec<T>,

View File

@ -4,7 +4,7 @@ use ffxiv_types::World;
use url::Url; use url::Url;
#[derive(Debug, Serialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct CharacterSearchItem { pub struct CharacterSearchItem {
pub id: u64, pub id: u64,
pub name: String, pub name: String,