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