xmpp-rs/parsers/src/version.rs

91 lines
2.7 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/.
2024-07-09 15:01:42 +00:00
use xso::{AsXml, FromXml};
2017-07-29 10:45:45 +00:00
use crate::iq::{IqGetPayload, IqResultPayload};
use crate::ns;
/// 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.
2024-07-09 15:01:42 +00:00
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::VERSION, name = "query")]
pub struct VersionQuery;
impl IqGetPayload for VersionQuery {}
/// 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.
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::VERSION, name = "query")]
pub struct VersionResult {
/// The name of this client.
#[xml(extract(fields(text)))]
pub name: String,
/// The version of this client.
#[xml(extract(fields(text)))]
pub version: String,
/// The OS this client is running on.
#[xml(extract(default, fields(text(type_ = String))))]
pub os: Option<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 minidom::Element;
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
}