1use minidom::Element;
2
3use error::Error;
4use super::MessagePayload;
5
6// TODO: also support components and servers.
7use ns::JABBER_CLIENT_NS;
8
9#[derive(Debug)]
10pub struct Body {
11 body: String,
12}
13
14impl MessagePayload for Body {}
15
16pub fn parse_body(root: &Element) -> Result<Body, Error> {
17 if !root.is("body", JABBER_CLIENT_NS) {
18 return Err(Error::ParseError("This is not a body element."));
19 }
20 for _ in root.children() {
21 return Err(Error::ParseError("Unknown child in body element."));
22 }
23 Ok(Body { body: root.text() })
24}
25
26#[cfg(test)]
27mod tests {
28 use minidom::Element;
29 use error::Error;
30 use chatstates;
31
32 #[test]
33 fn test_simple() {
34 let elem: Element = "<active xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
35 chatstates::parse_chatstate(&elem).unwrap();
36 }
37
38 #[test]
39 fn test_invalid() {
40 let elem: Element = "<coucou xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
41 let error = chatstates::parse_chatstate(&elem).unwrap_err();
42 let message = match error {
43 Error::ParseError(string) => string,
44 _ => panic!(),
45 };
46 assert_eq!(message, "Unknown chatstate element.");
47 }
48
49 #[test]
50 fn test_invalid_child() {
51 let elem: Element = "<gone xmlns='http://jabber.org/protocol/chatstates'><coucou/></gone>".parse().unwrap();
52 let error = chatstates::parse_chatstate(&elem).unwrap_err();
53 let message = match error {
54 Error::ParseError(string) => string,
55 _ => panic!(),
56 };
57 assert_eq!(message, "Unknown child in chatstate element.");
58 }
59
60 #[test]
61 #[ignore]
62 fn test_invalid_attribute() {
63 let elem: Element = "<inactive xmlns='http://jabber.org/protocol/chatstates' coucou=''/>".parse().unwrap();
64 let error = chatstates::parse_chatstate(&elem).unwrap_err();
65 let message = match error {
66 Error::ParseError(string) => string,
67 _ => panic!(),
68 };
69 assert_eq!(message, "Unknown attribute in chatstate element.");
70 }
71}