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