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