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, "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 crate::compare_elements::NamespaceAwareCompare;
45
46    #[cfg(target_pointer_width = "32")]
47    #[test]
48    fn test_size() {
49        assert_size!(VersionQuery, 0);
50        assert_size!(VersionResult, 36);
51    }
52
53    #[cfg(target_pointer_width = "64")]
54    #[test]
55    fn test_size() {
56        assert_size!(VersionQuery, 0);
57        assert_size!(VersionResult, 72);
58    }
59
60    #[test]
61    fn simple() {
62        let elem: Element = "<query xmlns='jabber:iq:version'><name>xmpp-rs</name><version>0.3.0</version></query>".parse().unwrap();
63        let version = VersionResult::try_from(elem).unwrap();
64        assert_eq!(version.name, String::from("xmpp-rs"));
65        assert_eq!(version.version, String::from("0.3.0"));
66        assert_eq!(version.os, None);
67    }
68
69    #[test]
70    fn serialisation() {
71        let version = VersionResult {
72            name: String::from("xmpp-rs"),
73            version: String::from("0.3.0"),
74            os: None,
75        };
76        let elem1 = Element::from(version);
77        let elem2: Element = "<query xmlns='jabber:iq:version'><name>xmpp-rs</name><version>0.3.0</version></query>".parse().unwrap();
78        println!("{:?}", elem1);
79        assert!(elem1.compare_to(&elem2));
80    }
81}