From b93aa199ef59931d4c3e232ba6535b6493f8ddbb Mon Sep 17 00:00:00 2001 From: Sam Denty Date: Tue, 2 Feb 2021 17:30:35 +0000 Subject: [PATCH] implement deserialize --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/icon.rs | 18 ++++++++++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bb55564..f98dde8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1527,7 +1527,7 @@ checksum = "fa8f3741c7372e75519bd9346068370c9cdaabcc1f9599cbcf2a2719352286b7" [[package]] name = "site_icons" -version = "0.1.3" +version = "0.1.4" dependencies = [ "byteorder", "clap", diff --git a/Cargo.toml b/Cargo.toml index 0f9ceb0..5c86c02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "site_icons" -version = "0.1.3" +version = "0.1.4" authors = ["Sam Denty "] edition = "2018" license = "GPL-3.0" diff --git a/src/icon.rs b/src/icon.rs index ee00165..2b37a88 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -3,10 +3,11 @@ use serde::Serialize; use std::{ cmp::Ordering, fmt::{self, Display}, + str::FromStr, }; use url::Url; -#[derive(Debug, Serialize, Clone, PartialOrd, PartialEq, Ord, Eq)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialOrd, PartialEq, Ord, Eq)] pub enum IconKind { SiteLogo, SiteFavicon, @@ -23,7 +24,20 @@ impl Display for IconKind { } } -#[derive(Debug, Serialize, PartialEq, Eq)] +impl FromStr for IconKind { + type Err = String; + + fn from_str(kind: &str) -> Result { + match kind { + "site_logo" => Ok(IconKind::SiteLogo), + "app_icon" => Ok(IconKind::AppIcon), + "site_favicon" => Ok(IconKind::SiteFavicon), + _ => Err("unknown icon kind!".into()), + } + } +} + +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct Icon { pub url: Url, #[serde(with = "serde_with::rust::display_fromstr")]