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
 7#![deny(missing_docs)]
 8
 9use iq::{IqGetPayload, IqResultPayload};
10
11generate_empty_element!(
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    VersionQuery, "query", 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 try_from::TryFrom;
45    use minidom::Element;
46    use compare_elements::NamespaceAwareCompare;
47
48    #[test]
49    fn test_simple() {
50        let elem: Element = "<query xmlns='jabber:iq:version'><name>xmpp-rs</name><version>0.3.0</version></query>".parse().unwrap();
51        let version = Version::try_from(elem).unwrap();
52        assert_eq!(version.name, String::from("xmpp-rs"));
53        assert_eq!(version.version, String::from("0.3.0"));
54        assert_eq!(version.os, None);
55    }
56
57    #[test]
58    fn serialisation() {
59        let version = Version {
60            name: String::from("xmpp-rs"),
61            version: String::from("0.3.0"),
62            os: None,
63        };
64        let elem1 = Element::from(version);
65        let elem2: Element = "<query xmlns='jabber:iq:version'><name>xmpp-rs</name><version>0.3.0</version></query>".parse().unwrap();
66        println!("{:?}", elem1);
67        assert!(elem1.compare_to(&elem2));
68    }
69}