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