Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 13 additions & 87 deletions src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use {
},
itertools::Itertools,
serde::{
de::{self, MapAccess, Visitor},
de::{self, Visitor},
Deserialize, Deserializer,
},
std::{borrow::Borrow, pin::Pin},
std::{borrow::Borrow, pin::Pin, collections::HashMap},
};

/// Chunk size used for iterators performing requests
Expand Down Expand Up @@ -48,11 +48,21 @@ pub struct PostPreview {
pub url: Option<String>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct PostSampleAlternates {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should probably be singular: PostSampleAlternate

#[serde(rename = "type")]
pub stype: String,
pub height: u64,
pub width: u64,
pub urls: Vec<Option<String>>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct PostSample {
pub width: u64,
pub height: u64,
pub url: Option<String>,
pub alternates: HashMap<String, PostSampleAlternates>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
Expand Down Expand Up @@ -115,8 +125,7 @@ pub struct Post {
pub updated_at: Option<DateTime<Utc>>,
pub file: PostFile,
pub preview: PostPreview,
#[serde(deserialize_with = "PostSample::from_json")]
pub sample: Option<PostSample>,
pub sample: PostSample,
pub score: PostScore,
pub tags: PostTags,
pub locked_tags: Vec<String>,
Expand Down Expand Up @@ -169,89 +178,6 @@ where
de.deserialize_any(NullableBoolVisitor)
}

impl PostSample {
fn from_json<'de, D>(de: D) -> Result<Option<PostSample>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(field_identifier, rename_all = "lowercase")]
enum Field {
Has,
Width,
Height,
Url,
}

struct PostSampleVisitor;

impl<'de> Visitor<'de> for PostSampleVisitor {
type Value = Option<PostSample>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("struct PostSample")
}

fn visit_map<V>(self, mut map: V) -> Result<Option<PostSample>, V::Error>
where
V: MapAccess<'de>,
{
let mut has = None;
let mut width = None;
let mut height = None;
let mut url = 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()?);
}
}
}

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 }))
}
}
}

const FIELDS: &'static [&'static str] = &["has", "width", "height", "url"];
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 {
Expand Down