From b1044b8559c4d72fb2c4ec3aadbf4e7cdbcf1d35 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 23 Dec 2019 13:12:11 +0100 Subject: Replace hand implementation of Deserialize, Serialize with derive() Signed-off-by: Matthias Beyer --- headers/src/header_components/disposition.rs | 52 +++------------------------- 1 file changed, 5 insertions(+), 47 deletions(-) diff --git a/headers/src/header_components/disposition.rs b/headers/src/header_components/disposition.rs index 575f900..34c7b1c 100644 --- a/headers/src/header_components/disposition.rs +++ b/headers/src/header_components/disposition.rs @@ -34,13 +34,17 @@ struct DispositionParameters(FileMeta); /// Represents what kind of disposition is used (Inline/Attachment) #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum DispositionKind { /// Display the body "inline". /// /// This disposition is mainly used to add some additional content /// and then refers to it through its cid (e.g. in a html mail). + #[cfg_attr(feature = "serde", serde(rename = "inline"))] Inline, + /// Display the body as an attachment to of the mail. + #[cfg_attr(feature = "serde", serde(rename = "attachment"))] Attachment } @@ -78,52 +82,6 @@ impl Disposition { } -#[cfg(feature="serde")] -impl Serialize for DispositionKind { - fn serialize(&self, serializer: S) -> Result - where S: Serializer - { - match self { - &DispositionKind::Inline => - serializer.serialize_str("inline"), - &DispositionKind::Attachment => - serializer.serialize_str("attachment") - } - } -} - -#[cfg(feature="serde")] -impl<'de> Deserialize<'de> for DispositionKind { - fn deserialize(deserializer: D) -> Result - where D: Deserializer<'de> - { - struct Visitor; - impl<'de> ::serde::de::Visitor<'de> for Visitor { - type Value = DispositionKind; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("\"inline\" or \"attachment\"") - } - - fn visit_str(self, value: &str) -> Result - where E: ::serde::de::Error, - { - if value.eq_ignore_ascii_case("inline") { - Ok(DispositionKind::Inline) - } else if value.eq_ignore_ascii_case("attachment") { - Ok(DispositionKind::Attachment) - } else { - Err(E::custom(format!( - "unknown disposition: {:?}", value - ))) - } - } - } - - deserializer.deserialize_str(Visitor) - } -} - /// This try from is for usability only, it is /// generally recommendet to use Disposition::inline()/::attachment() /// as it is type safe / compiler time checked, while this one @@ -305,4 +263,4 @@ mod test { assert_deserialize::(); assert_deserialize::(); } -} \ No newline at end of file +} -- cgit v1.2.3