Add a WebSocket <open/> parser.

Emmanuel Gil Peyrot created

Change summary

src/lib.rs       |  3 ++
src/ns.rs        |  3 ++
src/websocket.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 76 insertions(+)

Detailed changes

src/lib.rs 🔗

@@ -59,6 +59,9 @@ pub mod stanza_error;
 /// RFC 6121: Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence
 pub mod roster;
 
+/// RFC 7395: An Extensible Messaging and Presence Protocol (XMPP) Subprotocol for WebSocket
+pub mod websocket;
+
 /// XEP-0004: Data Forms
 pub mod data_forms;
 

src/ns.rs 🔗

@@ -13,6 +13,9 @@ pub const XMPP_STANZAS: &str = "urn:ietf:params:xml:ns:xmpp-stanzas";
 /// RFC 6121: Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence
 pub const ROSTER: &str = "jabber:iq:roster";
 
+/// RFC 7395: An Extensible Messaging and Presence Protocol (XMPP) Subprotocol for WebSocket
+pub const WEBSOCKET: &str = "urn:ietf:params:xml:ns:xmpp-framing";
+
 /// XEP-0004: Data Forms
 pub const DATA_FORMS: &str = "jabber:x:data";
 

src/websocket.rs 🔗

@@ -0,0 +1,70 @@
+// Copyright (c) 2018 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 try_from::TryFrom;
+
+use minidom::Element;
+use jid::Jid;
+use error::Error;
+use ns;
+
+generate_element_with_only_attributes!(Open, "open", ns::WEBSOCKET, [
+    from: Option<Jid> = "from" => optional,
+    to: Option<Jid> = "to" => optional,
+    id: Option<String> = "id" => optional,
+    version: Option<String> = "version" => optional,
+    xml_lang: Option<String> = "xml:lang" => optional,
+]);
+
+impl Open {
+    pub fn new(to: Jid) -> Open {
+        Open {
+            from: None,
+            to: Some(to),
+            id: None,
+            version: Some(String::from("1.0")),
+            xml_lang: None,
+        }
+    }
+
+    pub fn with_from(mut self, from: Jid) -> Open {
+        self.from = Some(from);
+        self
+    }
+
+    pub fn with_id(mut self, id: String) -> Open {
+        self.id = Some(id);
+        self
+    }
+
+    pub fn with_lang(mut self, xml_lang: String) -> Open {
+        self.xml_lang = Some(xml_lang);
+        self
+    }
+
+    pub fn is_version(&self, version: &str) -> bool {
+        match self.version {
+            None => false,
+            Some(ref self_version) => self_version == &String::from(version),
+        }
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_simple() {
+        let elem: Element = "<open xmlns='urn:ietf:params:xml:ns:xmpp-framing'/>".parse().unwrap();
+        let open = Open::try_from(elem).unwrap();
+        assert_eq!(open.from, None);
+        assert_eq!(open.to, None);
+        assert_eq!(open.id, None);
+        assert_eq!(open.version, None);
+        assert_eq!(open.xml_lang, None);
+    }
+}