From 8bf643a84f9955ffaa4fce861306515af9ae1c1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Fri, 1 May 2020 02:26:58 +0200 Subject: [PATCH] jid: implement custom (De)Serializer for a prettier visual MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- jid-rs/src/lib.rs | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/jid-rs/src/lib.rs b/jid-rs/src/lib.rs index 11e13ae..aa9e294 100644 --- a/jid-rs/src/lib.rs +++ b/jid-rs/src/lib.rs @@ -20,7 +20,7 @@ use std::fmt; use std::str::FromStr; #[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; /// An error that signifies that a `Jid` cannot be parsed from a string. #[derive(Debug, Clone, PartialEq, Eq)] @@ -159,7 +159,6 @@ impl TryFrom for FullJid { /// /// Unlike a `BareJid`, it always contains a resource, and should only be used when you are certain /// there is no case where a resource can be missing. Otherwise, use a `Jid` enum. -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, PartialEq, Eq, Hash)] pub struct FullJid { /// The node part of the Jabber ID, if it exists, else None. @@ -179,7 +178,6 @@ pub struct FullJid { /// /// Unlike a `FullJid`, it can’t contain a resource, and should only be used when you are certain /// there is no case where a resource can be set. Otherwise, use a `Jid` enum. -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, PartialEq, Eq, Hash)] pub struct BareJid { /// The node part of the Jabber ID, if it exists, else None. @@ -259,6 +257,26 @@ impl fmt::Display for BareJid { } } +#[cfg(feature = "serde")] +impl Serialize for FullJid { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(String::from(self).as_str()) + } +} + +#[cfg(feature = "serde")] +impl Serialize for BareJid { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(String::from(self).as_str()) + } +} + enum ParserState { Node, Domain, @@ -350,6 +368,28 @@ impl FromStr for FullJid { } } +#[cfg(feature = "serde")] +impl<'de> Deserialize<'de> for FullJid { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + FullJid::from_str(&s).map_err(de::Error::custom) + } +} + +#[cfg(feature = "serde")] +impl<'de> Deserialize<'de> for BareJid { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + BareJid::from_str(&s).map_err(de::Error::custom) + } +} + impl FullJid { /// Constructs a full Jabber ID containing all three components. ///