1use minidom::Element;
2
3use error::Error;
4
5use ns;
6
7#[derive(Debug, Clone)]
8pub struct Attention;
9
10pub fn parse_attention(root: &Element) -> Result<Attention, Error> {
11 if !root.is("attention", ns::ATTENTION) {
12 return Err(Error::ParseError("This is not an attention element."));
13 }
14 for _ in root.children() {
15 return Err(Error::ParseError("Unknown child in attention element."));
16 }
17 Ok(Attention)
18}
19
20pub fn serialise(_: &Attention) -> Element {
21 Element::builder("attention")
22 .ns(ns::ATTENTION)
23 .build()
24}
25
26#[cfg(test)]
27mod tests {
28 use minidom::Element;
29 use error::Error;
30 use attention;
31
32 #[test]
33 fn test_simple() {
34 let elem: Element = "<attention xmlns='urn:xmpp:attention:0'/>".parse().unwrap();
35 attention::parse_attention(&elem).unwrap();
36 }
37
38 #[test]
39 fn test_invalid_child() {
40 let elem: Element = "<attention xmlns='urn:xmpp:attention:0'><coucou/></attention>".parse().unwrap();
41 let error = attention::parse_attention(&elem).unwrap_err();
42 let message = match error {
43 Error::ParseError(string) => string,
44 _ => panic!(),
45 };
46 assert_eq!(message, "Unknown child in attention element.");
47 }
48
49 #[test]
50 fn test_serialise() {
51 let elem: Element = "<attention xmlns='urn:xmpp:attention:0'/>".parse().unwrap();
52 let attention = attention::Attention;
53 let elem2 = attention::serialise(&attention);
54 assert_eq!(elem, elem2);
55 }
56}