Add a status parser.

Emmanuel Gil Peyrot created

Change summary

src/lib.rs    |  5 ++
src/status.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+), 1 deletion(-)

Detailed changes

src/lib.rs 🔗

@@ -21,8 +21,11 @@ pub mod ns;
 pub mod message;
 /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
 pub mod presence;
-/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
+
+/// RFC 6121: Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence
 pub mod body;
+/// RFC 6121: Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence
+pub mod status;
 
 /// XEP-0004: Data Forms
 pub mod data_forms;

src/status.rs 🔗

@@ -0,0 +1,80 @@
+use minidom::Element;
+
+use error::Error;
+
+use ns;
+
+pub type Status = String;
+
+pub fn parse_status(root: &Element) -> Result<Status, Error> {
+    // TODO: also support components and servers.
+    if !root.is("status", ns::JABBER_CLIENT) {
+        return Err(Error::ParseError("This is not a status element."));
+    }
+    for _ in root.children() {
+        return Err(Error::ParseError("Unknown child in status element."));
+    }
+    Ok(root.text())
+}
+
+pub fn serialise(status: &Status) -> Element {
+    Element::builder("status")
+            .ns(ns::JABBER_CLIENT)
+            .append(status.to_owned())
+            .build()
+}
+
+#[cfg(test)]
+mod tests {
+    use minidom::Element;
+    use error::Error;
+    use status;
+    use ns;
+
+    #[test]
+    fn test_simple() {
+        let elem: Element = "<status xmlns='jabber:client'/>".parse().unwrap();
+        status::parse_status(&elem).unwrap();
+    }
+
+    #[test]
+    fn test_invalid() {
+        let elem: Element = "<status xmlns='jabber:server'/>".parse().unwrap();
+        let error = status::parse_status(&elem).unwrap_err();
+        let message = match error {
+            Error::ParseError(string) => string,
+            _ => panic!(),
+        };
+        assert_eq!(message, "This is not a status element.");
+    }
+
+    #[test]
+    fn test_invalid_child() {
+        let elem: Element = "<status xmlns='jabber:client'><coucou/></status>".parse().unwrap();
+        let error = status::parse_status(&elem).unwrap_err();
+        let message = match error {
+            Error::ParseError(string) => string,
+            _ => panic!(),
+        };
+        assert_eq!(message, "Unknown child in status element.");
+    }
+
+    #[test]
+    #[ignore]
+    fn test_invalid_attribute() {
+        let elem: Element = "<status xmlns='jabber:client' coucou=''/>".parse().unwrap();
+        let error = status::parse_status(&elem).unwrap_err();
+        let message = match error {
+            Error::ParseError(string) => string,
+            _ => panic!(),
+        };
+        assert_eq!(message, "Unknown attribute in status element.");
+    }
+
+    #[test]
+    fn test_serialise() {
+        let status = status::Status::from("Hello world!");
+        let elem = status::serialise(&status);
+        assert!(elem.is("status", ns::JABBER_CLIENT));
+    }
+}