From bcf9dc9e36fc485a1b58332aee9ee8ea1d7200d0 Mon Sep 17 00:00:00 2001 From: Joakim Soderlund Date: Sun, 3 Mar 2024 19:04:18 +0100 Subject: [PATCH] Derive deserialization of enum variants for index --- src/archive/story.rs | 68 +++++--------------------------------------- 1 file changed, 7 insertions(+), 61 deletions(-) diff --git a/src/archive/story.rs b/src/archive/story.rs index c40d1cc..171915a 100644 --- a/src/archive/story.rs +++ b/src/archive/story.rs @@ -126,15 +126,18 @@ pub struct Color { pub blue: u8, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Deserialize)] +#[serde(rename_all = "snake_case")] pub enum CompletionStatus { Cancelled, Complete, + #[serde(alias = "on hiatus")] Hiatus, Incomplete, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Deserialize)] +#[serde(rename_all = "snake_case")] pub enum ContentRating { Everyone, Mature, @@ -150,7 +153,8 @@ pub struct CoverImage { pub thumbnail: String, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Deserialize)] +#[serde(rename_all = "snake_case")] pub enum Status { ApproveQueue, NotVisible, @@ -236,61 +240,3 @@ impl<'de> Deserialize<'de> for Color { } } } - -impl CompletionStatus { - const FIELDS: &'static [&'static str] = - &["cancelled", "complete", "hiatus", "incomplete"]; -} - -impl<'de> Deserialize<'de> for CompletionStatus { - fn deserialize(d: D) -> Result - where - D: Deserializer<'de>, - { - match String::deserialize(d)?.as_ref() { - "cancelled" => Ok(CompletionStatus::Cancelled), - "complete" => Ok(CompletionStatus::Complete), - "hiatus" | "on hiatus" => Ok(CompletionStatus::Hiatus), - "incomplete" => Ok(CompletionStatus::Incomplete), - value => Err(Error::unknown_field(value, Self::FIELDS)), - } - } -} - -impl ContentRating { - const FIELDS: &'static [&'static str] = &["everyone", "mature", "teen"]; -} - -impl<'de> Deserialize<'de> for ContentRating { - fn deserialize(d: D) -> Result - where - D: Deserializer<'de>, - { - match String::deserialize(d)?.as_ref() { - "everyone" => Ok(ContentRating::Everyone), - "mature" => Ok(ContentRating::Mature), - "teen" => Ok(ContentRating::Teen), - value => Err(Error::unknown_field(value, Self::FIELDS)), - } - } -} - -impl Status { - const FIELDS: &'static [&'static str] = - &["approve_queue", "not_visible", "post_queue", "visible"]; -} - -impl<'de> Deserialize<'de> for Status { - fn deserialize(d: D) -> Result - where - D: Deserializer<'de>, - { - match String::deserialize(d)?.as_ref() { - "approve_queue" => Ok(Status::ApproveQueue), - "not_visible" => Ok(Status::NotVisible), - "post_queue" => Ok(Status::PostQueue), - "visible" => Ok(Status::Visible), - value => Err(Error::unknown_field(value, Self::FIELDS)), - } - } -}