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
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 =
63 "<query xmlns='jabber:iq:version'><name>xmpp-rs</name><version>0.3.0</version></query>"
64 .parse()
65 .unwrap();
66 let version = VersionResult::try_from(elem).unwrap();
67 assert_eq!(version.name, String::from("xmpp-rs"));
68 assert_eq!(version.version, String::from("0.3.0"));
69 assert_eq!(version.os, None);
70 }
71
72 #[test]
73 fn serialisation() {
74 let version = VersionResult {
75 name: String::from("xmpp-rs"),
76 version: String::from("0.3.0"),
77 os: None,
78 };
79 let elem1 = Element::from(version);
80 let elem2: Element =
81 "<query xmlns='jabber:iq:version'><name>xmpp-rs</name><version>0.3.0</version></query>"
82 .parse()
83 .unwrap();
84 println!("{:?}", elem1);
85 assert_eq!(elem1, elem2);
86 }
87}