Add a new JID Prep parser (XEP-0328).

Emmanuel Gil Peyrot created

Change summary

ChangeLog           |  1 
doap.xml            |  8 ++++++
src/jid_prep.rs     | 56 +++++++++++++++++++++++++++++++++++++++++++++++
src/lib.rs          |  3 ++
src/ns.rs           |  3 ++
src/util/helpers.rs | 15 ++++++++++++
6 files changed, 86 insertions(+)

Detailed changes

ChangeLog 🔗

@@ -2,6 +2,7 @@ Version NEXT:
 DATE  Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
     * New parsers/serialisers:
         - Client Certificate Management for SASL EXTERNAL (XEP-0257)
+        - JID Prep (XEP-0328)
         - Client State Indication (XEP-0352)
         - OpenPGP for XMPP (XEP-0373)
         - Anonymous unique occupant identifiers for MUCs (XEP-0421)

doap.xml 🔗

@@ -400,6 +400,14 @@
             <xmpp:since>0.13.0</xmpp:since>
         </xmpp:SupportedXep>
     </implements>
+    <implements>
+        <xmpp:SupportedXep>
+            <xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0328.html"/>
+            <xmpp:status>complete</xmpp:status>
+            <xmpp:version>0.1</xmpp:version>
+            <xmpp:since>NEXT</xmpp:since>
+        </xmpp:SupportedXep>
+    </implements>
     <implements>
         <xmpp:SupportedXep>
             <xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0352.html"/>

src/jid_prep.rs 🔗

@@ -0,0 +1,56 @@
+// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+use crate::iq::{IqGetPayload, IqResultPayload};
+use crate::util::helpers::{PlainText, JidCodec};
+use jid::Jid;
+
+generate_element!(
+    /// TODO
+    JidPrepQuery, "jid", JID_PREP,
+    text: (
+        /// TODO
+        data: PlainText<Option<String>>
+    )
+);
+
+impl IqGetPayload for JidPrepQuery {}
+
+generate_element!(
+    /// TODO
+    JidPrepResponse, "jid", JID_PREP,
+    text: (
+        /// TODO
+        jid: JidCodec<Jid>
+    )
+);
+
+impl IqResultPayload for JidPrepResponse {}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use minidom::Element;
+    use std::convert::TryFrom;
+    use std::str::FromStr;
+
+    #[test]
+    fn test_size() {
+        assert_size!(JidPrepQuery, 24);
+        assert_size!(JidPrepResponse, 80);
+    }
+
+    #[test]
+    fn simple() {
+        let elem: Element = "<jid xmlns='urn:xmpp:jidprep:0'>ROMeo@montague.lit/orchard</jid>".parse().unwrap();
+        let query = JidPrepQuery::try_from(elem).unwrap();
+        assert_eq!(query.data.unwrap(), "ROMeo@montague.lit/orchard");
+
+        let elem: Element = "<jid xmlns='urn:xmpp:jidprep:0'>romeo@montague.lit/orchard</jid>".parse().unwrap();
+        let response = JidPrepResponse::try_from(elem).unwrap();
+        assert_eq!(response.jid, Jid::from_str("romeo@montague.lit/orchard").unwrap());
+    }
+}

src/lib.rs 🔗

@@ -180,6 +180,9 @@ pub mod idle;
 /// XEP-0320: Use of DTLS-SRTP in Jingle Sessions
 pub mod jingle_dtls_srtp;
 
+/// XEP-0328: JID Prep
+pub mod jid_prep;
+
 /// XEP-0352: Client State Indication
 pub mod csi;
 

src/ns.rs 🔗

@@ -185,6 +185,9 @@ pub const IDLE: &str = "urn:xmpp:idle:1";
 /// XEP-0320: Use of DTLS-SRTP in Jingle Sessions
 pub const JINGLE_DTLS: &str = "urn:xmpp:jingle:apps:dtls:0";
 
+/// XEP-0328: JID Prep
+pub const JID_PREP: &str = "urn:xmpp:jidprep:0";
+
 /// XEP-0352: Client State Indication
 pub const CSI: &str = "urn:xmpp:csi:0";
 

src/util/helpers.rs 🔗

@@ -5,6 +5,8 @@
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 use crate::util::error::Error;
+use jid::Jid;
+use std::str::FromStr;
 
 /// Codec for plain text content.
 pub struct PlainText;
@@ -89,3 +91,16 @@ impl ColonSeparatedHex {
         Some(bytes.join(":"))
     }
 }
+
+/// Codec for a JID.
+pub struct JidCodec;
+
+impl JidCodec {
+    pub fn decode(s: &str) -> Result<Jid, Error> {
+        Ok(Jid::from_str(s)?)
+    }
+
+    pub fn encode(jid: &Jid) -> Option<String> {
+        Some(jid.to_string())
+    }
+}