message_correct.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 try_from::TryFrom;
 8
 9use minidom::Element;
10
11use error::Error;
12
13use ns;
14
15#[derive(Debug, Clone)]
16pub struct Replace {
17    pub id: String,
18}
19
20impl TryFrom<Element> for Replace {
21    type Err = Error;
22
23    fn try_from(elem: Element) -> Result<Replace, Error> {
24        if !elem.is("replace", ns::MESSAGE_CORRECT) {
25            return Err(Error::ParseError("This is not a replace element."));
26        }
27        for _ in elem.children() {
28            return Err(Error::ParseError("Unknown child in replace element."));
29        }
30        for (attr, _) in elem.attrs() {
31            if attr != "id" {
32                return Err(Error::ParseError("Unknown attribute in replace element."));
33            }
34        }
35        let id = get_attr!(elem, "id", required);
36        Ok(Replace { id })
37    }
38}
39
40impl From<Replace> for Element {
41    fn from(replace: Replace) -> Element {
42        Element::builder("replace")
43                .ns(ns::MESSAGE_CORRECT)
44                .attr("id", replace.id)
45                .build()
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_simple() {
55        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".parse().unwrap();
56        Replace::try_from(elem).unwrap();
57    }
58
59    #[test]
60    fn test_invalid_attribute() {
61        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' coucou=''/>".parse().unwrap();
62        let error = Replace::try_from(elem).unwrap_err();
63        let message = match error {
64            Error::ParseError(string) => string,
65            _ => panic!(),
66        };
67        assert_eq!(message, "Unknown attribute in replace element.");
68    }
69
70    #[test]
71    fn test_invalid_child() {
72        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'><coucou/></replace>".parse().unwrap();
73        let error = Replace::try_from(elem).unwrap_err();
74        let message = match error {
75            Error::ParseError(string) => string,
76            _ => panic!(),
77        };
78        assert_eq!(message, "Unknown child in replace element.");
79    }
80
81    #[test]
82    fn test_invalid_id() {
83        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
84        let error = Replace::try_from(elem).unwrap_err();
85        let message = match error {
86            Error::ParseError(string) => string,
87            _ => panic!(),
88        };
89        assert_eq!(message, "Required attribute 'id' missing.");
90    }
91
92    #[test]
93    fn test_serialise() {
94        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".parse().unwrap();
95        let replace = Replace { id: String::from("coucou") };
96        let elem2 = replace.into();
97        assert_eq!(elem, elem2);
98    }
99}