From b0672fc67d2f182fd6b28d806ced9ba86af484e9 Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Thu, 17 Dec 2020 18:09:01 -0700 Subject: [PATCH 1/4] Updated post.rs to add Alternatives field in latest API update --- src/post.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/post.rs b/src/post.rs index 15c8301..e75f2dd 100644 --- a/src/post.rs +++ b/src/post.rs @@ -11,7 +11,7 @@ use { de::{self, MapAccess, Visitor}, Deserialize, Deserializer, }, - std::{borrow::Borrow, pin::Pin}, + std::{borrow::Borrow, pin::Pin, collections::HashMap}, }; /// Chunk size used for iterators performing requests @@ -53,6 +53,7 @@ pub struct PostSample { pub width: u64, pub height: u64, pub url: Option, + pub alternatives: Option>, } #[derive(Debug, PartialEq, Eq, Deserialize)] @@ -181,6 +182,7 @@ impl PostSample { Width, Height, Url, + Alternatives } struct PostSampleVisitor; @@ -200,6 +202,7 @@ impl PostSample { let mut width = None; let mut height = None; let mut url = None; + let mut alternatives = None; while let Some(key) = map.next_key()? { match key { @@ -229,6 +232,13 @@ impl PostSample { return Err(de::Error::duplicate_field("url")); } + url = Some(map.next_value()?); + } + Field::Alternatives => { + if alternatives.is_some() { + return Err(de::Error::duplicate_field("alternatives")); + } + url = Some(map.next_value()?); } } @@ -242,12 +252,12 @@ impl PostSample { if let Some(true) = has { Ok(None) } else { - Ok(Some(PostSample { width, height, url })) + Ok(Some(PostSample { width, height, url, alternatives })) } } } - const FIELDS: &'static [&'static str] = &["has", "width", "height", "url"]; + const FIELDS: &'static [&'static str] = &["has", "width", "height", "url", "alternatives"]; de.deserialize_struct("PostSample", FIELDS, PostSampleVisitor) } } From be5ae5185ee6fd03a7b32e4d776b2a5f27185fcb Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Thu, 17 Dec 2020 18:13:32 -0700 Subject: [PATCH 2/4] Rename field to correct name (alternatives => alternates) --- src/post.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/post.rs b/src/post.rs index e75f2dd..ad670e0 100644 --- a/src/post.rs +++ b/src/post.rs @@ -53,7 +53,7 @@ pub struct PostSample { pub width: u64, pub height: u64, pub url: Option, - pub alternatives: Option>, + pub alternates: Option>, } #[derive(Debug, PartialEq, Eq, Deserialize)] @@ -182,7 +182,7 @@ impl PostSample { Width, Height, Url, - Alternatives + Alternates } struct PostSampleVisitor; @@ -202,7 +202,7 @@ impl PostSample { let mut width = None; let mut height = None; let mut url = None; - let mut alternatives = None; + let mut alternates = None; while let Some(key) = map.next_key()? { match key { @@ -234,12 +234,12 @@ impl PostSample { url = Some(map.next_value()?); } - Field::Alternatives => { - if alternatives.is_some() { - return Err(de::Error::duplicate_field("alternatives")); + Field::Alternates => { + if alternates.is_some() { + return Err(de::Error::duplicate_field("alternates")); } - url = Some(map.next_value()?); + alternates = Some(map.next_value()?); } } } @@ -252,12 +252,12 @@ impl PostSample { if let Some(true) = has { Ok(None) } else { - Ok(Some(PostSample { width, height, url, alternatives })) + Ok(Some(PostSample { width, height, url, alternates })) } } } - const FIELDS: &'static [&'static str] = &["has", "width", "height", "url", "alternatives"]; + const FIELDS: &'static [&'static str] = &["has", "width", "height", "url", "alternates"]; de.deserialize_struct("PostSample", FIELDS, PostSampleVisitor) } } From 766b17ba9da32e0422470c1e2b598f40afaa7322 Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Thu, 17 Dec 2020 18:25:04 -0700 Subject: [PATCH 3/4] Add PostSampleAlternates struct --- src/post.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/post.rs b/src/post.rs index ad670e0..d2ec6fc 100644 --- a/src/post.rs +++ b/src/post.rs @@ -48,12 +48,21 @@ pub struct PostPreview { pub url: Option, } +#[derive(Debug, PartialEq, Eq, Deserialize)] +pub struct PostSampleAlternates { + #[serde(rename = "type")] + pub stype: String, + pub height: u64, + pub width: u64, + pub urls: Vec, +} + #[derive(Debug, PartialEq, Eq, Deserialize)] pub struct PostSample { pub width: u64, pub height: u64, pub url: Option, - pub alternates: Option>, + pub alternates: Option>, } #[derive(Debug, PartialEq, Eq, Deserialize)] From 6440ca6b9beb6f22de05321bc1ff8c7537eea7f6 Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Tue, 23 Feb 2021 23:19:50 -0700 Subject: [PATCH 4/4] Update PostSample and PostSampleAlternates to catch null in urls in alternates Author: Zevaryx --- src/post.rs | 101 +++------------------------------------------------- 1 file changed, 4 insertions(+), 97 deletions(-) diff --git a/src/post.rs b/src/post.rs index d2ec6fc..1f05d27 100644 --- a/src/post.rs +++ b/src/post.rs @@ -8,7 +8,7 @@ use { }, itertools::Itertools, serde::{ - de::{self, MapAccess, Visitor}, + de::{self, Visitor}, Deserialize, Deserializer, }, std::{borrow::Borrow, pin::Pin, collections::HashMap}, @@ -54,7 +54,7 @@ pub struct PostSampleAlternates { pub stype: String, pub height: u64, pub width: u64, - pub urls: Vec, + pub urls: Vec>, } #[derive(Debug, PartialEq, Eq, Deserialize)] @@ -62,7 +62,7 @@ pub struct PostSample { pub width: u64, pub height: u64, pub url: Option, - pub alternates: Option>, + pub alternates: HashMap, } #[derive(Debug, PartialEq, Eq, Deserialize)] @@ -125,8 +125,7 @@ pub struct Post { pub updated_at: Option>, pub file: PostFile, pub preview: PostPreview, - #[serde(deserialize_with = "PostSample::from_json")] - pub sample: Option, + pub sample: PostSample, pub score: PostScore, pub tags: PostTags, pub locked_tags: Vec, @@ -179,98 +178,6 @@ where de.deserialize_any(NullableBoolVisitor) } -impl PostSample { - fn from_json<'de, D>(de: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - #[derive(Deserialize)] - #[serde(field_identifier, rename_all = "lowercase")] - enum Field { - Has, - Width, - Height, - Url, - Alternates - } - - struct PostSampleVisitor; - - impl<'de> Visitor<'de> for PostSampleVisitor { - type Value = Option; - - fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - formatter.write_str("struct PostSample") - } - - fn visit_map(self, mut map: V) -> Result, V::Error> - where - V: MapAccess<'de>, - { - let mut has = None; - let mut width = None; - let mut height = None; - let mut url = None; - let mut alternates = None; - - while let Some(key) = map.next_key()? { - match key { - Field::Has => { - if has.is_some() { - return Err(de::Error::duplicate_field("has")); - } - - has = Some(map.next_value()?); - } - Field::Width => { - if width.is_some() { - return Err(de::Error::duplicate_field("width")); - } - - width = Some(map.next_value()?); - } - Field::Height => { - if height.is_some() { - return Err(de::Error::duplicate_field("height")); - } - - height = Some(map.next_value()?); - } - Field::Url => { - if url.is_some() { - return Err(de::Error::duplicate_field("url")); - } - - url = Some(map.next_value()?); - } - Field::Alternates => { - if alternates.is_some() { - return Err(de::Error::duplicate_field("alternates")); - } - - alternates = Some(map.next_value()?); - } - } - } - - let has = has.ok_or_else(|| de::Error::missing_field("has"))?; - let width = width.ok_or_else(|| de::Error::missing_field("width"))?; - let height = height.ok_or_else(|| de::Error::missing_field("height"))?; - let url = url.ok_or_else(|| de::Error::missing_field("url"))?; - - if let Some(true) = has { - Ok(None) - } else { - Ok(Some(PostSample { width, height, url, alternates })) - } - } - } - - const FIELDS: &'static [&'static str] = &["has", "width", "height", "url", "alternates"]; - de.deserialize_struct("PostSample", FIELDS, PostSampleVisitor) - } -} - /// A search query. Contains information about the tags used and an URL encoded version of the tags. #[derive(Debug, PartialEq, Clone)] pub struct Query {