version.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 iq::{IqGetPayload, IqResultPayload};
 8
 9generate_empty_element!(
10    /// Represents a query for the software version a remote entity is using.
11    ///
12    /// It should only be used in an `<iq type='get'/>`, as it can only
13    /// represent the request, and not a result.
14    VersionQuery, "query", VERSION
15);
16
17impl IqGetPayload for VersionQuery {}
18
19generate_element!(
20    /// Represents the answer about the software version we are using.
21    ///
22    /// It should only be used in an `<iq type='result'/>`, as it can only
23    /// represent the result, and not a request.
24    VersionResult, "query", VERSION,
25    children: [
26        /// The name of this client.
27        name: Required<String> = ("name", VERSION) => String,
28
29        /// The version of this client.
30        version: Required<String> = ("version", VERSION) => String,
31
32        /// The OS this client is running on.
33        os: Option<String> = ("os", VERSION) => String
34    ]
35);
36
37impl IqResultPayload for VersionResult {}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use try_from::TryFrom;
43    use minidom::Element;
44    use compare_elements::NamespaceAwareCompare;
45
46    #[test]
47    fn test_size() {
48        assert_size!(VersionQuery, 0);
49        assert_size!(VersionResult, 72);
50    }
51
52    #[test]
53    fn simple() {
54        let elem: Element = "<query xmlns='jabber:iq:version'><name>xmpp-rs</name><version>0.3.0</version></query>".parse().unwrap();
55        let version = VersionResult::try_from(elem).unwrap();
56        assert_eq!(version.name, String::from("xmpp-rs"));
57        assert_eq!(version.version, String::from("0.3.0"));
58        assert_eq!(version.os, None);
59    }
60
61    #[test]
62    fn serialisation() {
63        let version = VersionResult {
64            name: String::from("xmpp-rs"),
65            version: String::from("0.3.0"),
66            os: None,
67        };
68        let elem1 = Element::from(version);
69        let elem2: Element = "<query xmlns='jabber:iq:version'><name>xmpp-rs</name><version>0.3.0</version></query>".parse().unwrap();
70        println!("{:?}", elem1);
71        assert!(elem1.compare_to(&elem2));
72    }
73}