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 xso::{FromXml, IntoXml};
 8
 9use crate::iq::{IqGetPayload, IqResultPayload};
10use crate::ns;
11
12/// Represents a query for the software version a remote entity is using.
13///
14/// It should only be used in an `<iq type='get'/>`, as it can only
15/// represent the request, and not a result.
16#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
17#[xml(namespace = ns::VERSION, name = "query")]
18pub struct VersionQuery;
19
20impl IqGetPayload for VersionQuery {}
21
22generate_element!(
23    /// Represents the answer about the software version we are using.
24    ///
25    /// It should only be used in an `<iq type='result'/>`, as it can only
26    /// represent the result, and not a request.
27    VersionResult, "query", VERSION,
28    children: [
29        /// The name of this client.
30        name: Required<String> = ("name", VERSION) => String,
31
32        /// The version of this client.
33        version: Required<String> = ("version", VERSION) => String,
34
35        /// The OS this client is running on.
36        os: Option<String> = ("os", VERSION) => String
37    ]
38);
39
40impl IqResultPayload for VersionResult {}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use crate::Element;
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}