status.rs

 1use minidom::Element;
 2
 3use error::Error;
 4
 5use ns;
 6
 7pub type Status = String;
 8
 9pub fn parse_status(root: &Element) -> Result<Status, Error> {
10    // TODO: also support components and servers.
11    if !root.is("status", ns::JABBER_CLIENT) {
12        return Err(Error::ParseError("This is not a status element."));
13    }
14    for _ in root.children() {
15        return Err(Error::ParseError("Unknown child in status element."));
16    }
17    Ok(root.text())
18}
19
20pub fn serialise(status: &Status) -> Element {
21    Element::builder("status")
22            .ns(ns::JABBER_CLIENT)
23            .append(status.to_owned())
24            .build()
25}
26
27#[cfg(test)]
28mod tests {
29    use minidom::Element;
30    use error::Error;
31    use status;
32    use ns;
33
34    #[test]
35    fn test_simple() {
36        let elem: Element = "<status xmlns='jabber:client'/>".parse().unwrap();
37        status::parse_status(&elem).unwrap();
38    }
39
40    #[test]
41    fn test_invalid() {
42        let elem: Element = "<status xmlns='jabber:server'/>".parse().unwrap();
43        let error = status::parse_status(&elem).unwrap_err();
44        let message = match error {
45            Error::ParseError(string) => string,
46            _ => panic!(),
47        };
48        assert_eq!(message, "This is not a status element.");
49    }
50
51    #[test]
52    fn test_invalid_child() {
53        let elem: Element = "<status xmlns='jabber:client'><coucou/></status>".parse().unwrap();
54        let error = status::parse_status(&elem).unwrap_err();
55        let message = match error {
56            Error::ParseError(string) => string,
57            _ => panic!(),
58        };
59        assert_eq!(message, "Unknown child in status element.");
60    }
61
62    #[test]
63    #[ignore]
64    fn test_invalid_attribute() {
65        let elem: Element = "<status xmlns='jabber:client' coucou=''/>".parse().unwrap();
66        let error = status::parse_status(&elem).unwrap_err();
67        let message = match error {
68            Error::ParseError(string) => string,
69            _ => panic!(),
70        };
71        assert_eq!(message, "Unknown attribute in status element.");
72    }
73
74    #[test]
75    fn test_serialise() {
76        let status = status::Status::from("Hello world!");
77        let elem = status::serialise(&status);
78        assert!(elem.is("status", ns::JABBER_CLIENT));
79    }
80}