xmpp-rs/xmpp-parsers/src/version.rs

89 lines
2.6 KiB
Rust
Raw Normal View History

2017-07-29 10:45:45 +00:00
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::iq::{IqGetPayload, IqResultPayload};
2017-07-29 10:45:45 +00:00
generate_empty_element!(
/// Represents a query for the software version a remote entity is using.
///
/// It should only be used in an `<iq type='get'/>`, as it can only
/// represent the request, and not a result.
2018-12-18 14:32:05 +00:00
VersionQuery,
"query",
VERSION
);
impl IqGetPayload for VersionQuery {}
generate_element!(
/// Represents the answer about the software version we are using.
///
/// It should only be used in an `<iq type='result'/>`, as it can only
/// represent the result, and not a request.
VersionResult, "query", VERSION,
children: [
/// The name of this client.
name: Required<String> = ("name", VERSION) => String,
/// The version of this client.
version: Required<String> = ("version", VERSION) => String,
/// The OS this client is running on.
os: Option<String> = ("os", VERSION) => String
]
);
2017-07-29 10:45:45 +00:00
impl IqResultPayload for VersionResult {}
2017-07-29 10:45:45 +00:00
#[cfg(test)]
mod tests {
use super::*;
use crate::Element;
use std::convert::TryFrom;
2017-07-29 10:45:45 +00:00
2018-10-28 12:10:48 +00:00
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(VersionQuery, 0);
assert_size!(VersionResult, 36);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(VersionQuery, 0);
assert_size!(VersionResult, 72);
}
2017-07-29 10:45:45 +00:00
#[test]
2018-07-31 22:44:27 +00:00
fn simple() {
2018-12-18 14:32:05 +00:00
let elem: Element =
"<query xmlns='jabber:iq:version'><name>xmpp-rs</name><version>0.3.0</version></query>"
.parse()
.unwrap();
2018-07-31 22:44:27 +00:00
let version = VersionResult::try_from(elem).unwrap();
2017-07-29 10:45:45 +00:00
assert_eq!(version.name, String::from("xmpp-rs"));
assert_eq!(version.version, String::from("0.3.0"));
assert_eq!(version.os, None);
}
2018-05-28 14:23:23 +00:00
#[test]
fn serialisation() {
2018-07-31 22:44:27 +00:00
let version = VersionResult {
2018-05-28 14:23:23 +00:00
name: String::from("xmpp-rs"),
version: String::from("0.3.0"),
os: None,
};
let elem1 = Element::from(version);
2018-12-18 14:32:05 +00:00
let elem2: Element =
"<query xmlns='jabber:iq:version'><name>xmpp-rs</name><version>0.3.0</version></query>"
.parse()
.unwrap();
2018-05-28 14:23:23 +00:00
println!("{:?}", elem1);
assert_eq!(elem1, elem2);
2018-05-28 14:23:23 +00:00
}
2017-07-29 10:45:45 +00:00
}