jid: Add serde support behind feature

Maxime β€œpep” Buquet created

Signed-off-by: Maxime β€œpep” Buquet <pep@bouah.net>

Change summary

jid-rs/Cargo.toml       | 1 +
jid-rs/src/lib.rs       | 6 ++++++
tokio-xmpp/Cargo.toml   | 3 +++
xmpp-parsers/Cargo.toml | 1 +
xmpp-rs/Cargo.toml      | 1 +
5 files changed, 12 insertions(+)

Detailed changes

jid-rs/Cargo.toml πŸ”—

@@ -20,3 +20,4 @@ gitlab = { repository = "xmpp-rs/xmpp-rs" }
 
 [dependencies]
 minidom = { version = "0.12", optional = true }
+serde = { version = "1.0", features = ["derive"], optional = true }

jid-rs/src/lib.rs πŸ”—

@@ -19,6 +19,9 @@ use std::error::Error as StdError;
 use std::fmt;
 use std::str::FromStr;
 
+#[cfg(feature = "serde")]
+use serde::{Deserialize, Serialize};
+
 /// An error that signifies that a `Jid` cannot be parsed from a string.
 #[derive(Debug, Clone, PartialEq, Eq)]
 pub enum JidParseError {
@@ -54,6 +57,7 @@ impl fmt::Display for JidParseError {
 }
 
 /// An enum representing a Jabber ID. It can be either a `FullJid` or a `BareJid`.
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub enum Jid {
     /// Bare Jid
@@ -155,6 +159,7 @@ impl TryFrom<Jid> 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.
@@ -174,6 +179,7 @@ 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.

tokio-xmpp/Cargo.toml πŸ”—

@@ -25,3 +25,6 @@ trust-dns-resolver = "0.19"
 trust-dns-proto = "0.19"
 xml5ever = "0.16"
 xmpp-parsers = "0.17"
+
+[features]
+serde = ["xmpp-parsers/serde"]

xmpp-parsers/Cargo.toml πŸ”—

@@ -29,6 +29,7 @@ chrono = "0.4.5"
 component = []
 # Disable validation of unknown attributes.
 disable-validation = []
+serde = ["jid/serde"]
 
 [package.metadata.docs.rs]
 rustdoc-args = [ "--sort-modules-by-appearance", "-Zunstable-options" ]

xmpp-rs/Cargo.toml πŸ”—

@@ -26,3 +26,4 @@ env_logger = "0.7"
 [features]
 default = ["avatars"]
 avatars = []
+serde = ["tokio-xmpp/serde", "xmpp-parsers/serde"]