diff --git a/src/lib.rs b/src/lib.rs index ba1f650..2536aaa 100644 --- a/src/lib.rs +++ b/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; diff --git a/src/status.rs b/src/status.rs new file mode 100644 index 0000000..6412ffc --- /dev/null +++ b/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 { + // 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 = "".parse().unwrap(); + status::parse_status(&elem).unwrap(); + } + + #[test] + fn test_invalid() { + let elem: Element = "".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 = "".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 = "".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)); + } +}