summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2019-12-23 13:12:11 +0100
committerMatthias Beyer <mail@beyermatthias.de>2019-12-23 13:12:11 +0100
commitb1044b8559c4d72fb2c4ec3aadbf4e7cdbcf1d35 (patch)
treedb123b6b32bfc084f6d0619b1daa361865a4deae
parent71ea078d4eb5d857680a69b01f7427ea43e2f5a2 (diff)
Replace hand implementation of Deserialize, Serialize with derive()disposition-serde
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--headers/src/header_components/disposition.rs52
1 files 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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- 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<D>(deserializer: D) -> Result<Self, D::Error>
- 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<E>(self, value: &str) -> Result<Self::Value, E>
- 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::<DispositionKind>();
assert_deserialize::<DispositionParameters>();
}
-} \ No newline at end of file
+}