attention.rs

 1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 2//
 3// This Source Code Form is subject to the terms of the Mozilla Public
 4// License, v. 2.0. If a copy of the MPL was not distributed with this
 5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
 6
 7use minidom::Element;
 8
 9use error::Error;
10
11use ns;
12
13#[derive(Debug, Clone)]
14pub struct Attention;
15
16pub fn parse_attention(root: &Element) -> Result<Attention, Error> {
17    if !root.is("attention", ns::ATTENTION) {
18        return Err(Error::ParseError("This is not an attention element."));
19    }
20    for _ in root.children() {
21        return Err(Error::ParseError("Unknown child in attention element."));
22    }
23    Ok(Attention)
24}
25
26pub fn serialise(_: &Attention) -> Element {
27    Element::builder("attention")
28            .ns(ns::ATTENTION)
29            .build()
30}
31
32#[cfg(test)]
33mod tests {
34    use minidom::Element;
35    use error::Error;
36    use attention;
37
38    #[test]
39    fn test_simple() {
40        let elem: Element = "<attention xmlns='urn:xmpp:attention:0'/>".parse().unwrap();
41        attention::parse_attention(&elem).unwrap();
42    }
43
44    #[test]
45    fn test_invalid_child() {
46        let elem: Element = "<attention xmlns='urn:xmpp:attention:0'><coucou/></attention>".parse().unwrap();
47        let error = attention::parse_attention(&elem).unwrap_err();
48        let message = match error {
49            Error::ParseError(string) => string,
50            _ => panic!(),
51        };
52        assert_eq!(message, "Unknown child in attention element.");
53    }
54
55    #[test]
56    fn test_serialise() {
57        let elem: Element = "<attention xmlns='urn:xmpp:attention:0'/>".parse().unwrap();
58        let attention = attention::Attention;
59        let elem2 = attention::serialise(&attention);
60        assert_eq!(elem, elem2);
61    }
62}