xmpp-rs/parsers/src/rsm.rs

222 lines
6.8 KiB
Rust
Raw Normal View History

2017-04-29 22:14:34 +01: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-12-21 00:39:55 +01:00
use xso::{AsXml, FromXml};
use crate::ns;
2017-04-29 04:37:18 +01:00
/// Requests paging through a potentially big set of items (represented by an
/// UID).
2024-12-21 00:39:55 +01:00
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::RSM, name = "set")]
pub struct SetQuery {
/// Limit the number of items, or use the recipient’s defaults if None.
2024-12-21 00:39:55 +01:00
#[xml(extract(default, fields(text(type_ = usize))))]
pub max: Option<usize>,
/// The UID after which to give results, or if None it is the element
/// “before” the first item, effectively an index of negative one.
2024-12-21 00:39:55 +01:00
#[xml(extract(default, fields(text(type_ = String))))]
2017-04-29 04:37:18 +01:00
pub after: Option<String>,
/// The UID before which to give results, or if None it starts with the
/// last page of the full set.
2024-12-21 00:39:55 +01:00
#[xml(extract(default, fields(text(type_ = String))))]
2017-04-29 04:37:18 +01:00
pub before: Option<String>,
/// Numerical index of the page (deprecated).
2024-12-21 00:39:55 +01:00
#[xml(extract(default, fields(text(type_ = usize))))]
2017-04-29 04:37:18 +01:00
pub index: Option<usize>,
}
/// The first item of the page.
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::RSM, name = "first")]
pub struct First {
/// The position of the [first item](#structfield.item) in the full set
/// (which may be approximate).
#[xml(attribute(default))]
pub index: Option<usize>,
/// The UID of the first item of the page.
#[xml(text)]
pub item: String,
}
/// Describes the paging result of a [query](struct.SetQuery.html).
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::RSM, name = "set")]
pub struct SetResult {
/// The first item of the page.
#[xml(child(default))]
pub first: Option<First>,
/// The UID of the last item of the page.
#[xml(extract(default, fields(text(type_ = String))))]
pub last: Option<String>,
/// How many items there are in the full set (which may be approximate).
#[xml(extract(default, fields(text(type_ = usize))))]
pub count: Option<usize>,
}
2017-04-29 04:37:18 +01:00
#[cfg(test)]
mod tests {
2017-05-06 20:14:45 +01:00
use super::*;
use minidom::Element;
use xso::error::{Error, FromElementError};
2017-04-29 04:37:18 +01:00
2018-10-28 13:10:48 +01:00
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(SetQuery, 40);
assert_size!(SetResult, 40);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(SetQuery, 80);
assert_size!(SetResult, 80);
}
2017-04-29 04:37:18 +01:00
#[test]
fn test_simple() {
2018-12-18 15:32:05 +01:00
let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>"
.parse()
.unwrap();
let set = SetQuery::try_from(elem).unwrap();
assert_eq!(set.max, None);
2017-04-29 04:37:18 +01:00
assert_eq!(set.after, None);
assert_eq!(set.before, None);
assert_eq!(set.index, None);
2018-12-18 15:32:05 +01:00
let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>"
.parse()
.unwrap();
let set = SetResult::try_from(elem).unwrap();
2017-04-29 04:37:18 +01:00
match set.first {
Some(_) => panic!(),
None => (),
}
assert_eq!(set.last, None);
assert_eq!(set.count, None);
2017-04-29 04:37:18 +01:00
}
#[test]
fn test_unknown() {
2018-12-18 15:32:05 +01:00
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>"
.parse()
.unwrap();
let error = SetQuery::try_from(elem.clone()).unwrap_err();
let returned_elem = match error {
FromElementError::Mismatch(elem) => elem,
_ => panic!(),
};
assert_eq!(elem, returned_elem);
2018-12-18 15:32:05 +01:00
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>"
.parse()
.unwrap();
let error = SetResult::try_from(elem.clone()).unwrap_err();
let returned_elem = match error {
FromElementError::Mismatch(elem) => elem,
2017-04-29 04:37:18 +01:00
_ => panic!(),
};
assert_eq!(elem, returned_elem);
2017-04-29 04:37:18 +01:00
}
#[test]
fn test_invalid_child() {
2018-12-18 15:32:05 +01:00
let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'><coucou/></set>"
.parse()
.unwrap();
let error = SetQuery::try_from(elem).unwrap_err();
let message = match error {
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
2024-12-21 00:39:55 +01:00
assert_eq!(message, "Unknown child in SetQuery element.");
2018-12-18 15:32:05 +01:00
let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'><coucou/></set>"
.parse()
.unwrap();
let error = SetResult::try_from(elem).unwrap_err();
2017-04-29 04:37:18 +01:00
let message = match error {
FromElementError::Invalid(Error::Other(string)) => string,
2017-04-29 04:37:18 +01:00
_ => panic!(),
};
assert_eq!(message, "Unknown child in SetResult element.");
2017-04-29 04:37:18 +01:00
}
#[test]
fn test_serialise() {
2018-12-18 15:32:05 +01:00
let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>"
.parse()
.unwrap();
let rsm = SetQuery {
max: None,
2017-04-29 04:37:18 +01:00
after: None,
before: None,
index: None,
};
let elem2 = rsm.into();
assert_eq!(elem, elem2);
2018-12-18 15:32:05 +01:00
let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>"
.parse()
.unwrap();
let rsm = SetResult {
2017-04-29 04:37:18 +01:00
first: None,
last: None,
count: None,
2017-04-29 04:37:18 +01:00
};
let elem2 = rsm.into();
2017-04-29 04:37:18 +01:00
assert_eq!(elem, elem2);
}
2017-05-21 15:41:16 +01:00
2024-12-21 00:39:55 +01:00
// TODO: This test is only ignored because <before/> and <before></before> aren’t equal in
// minidom, let’s fix that instead!
2024-02-06 00:49:37 +01:00
#[test]
2024-12-21 00:39:55 +01:00
#[ignore]
2024-02-06 00:49:37 +01:00
fn test_serialise_empty_before() {
let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'><before/></set>"
.parse()
.unwrap();
let rsm = SetQuery {
max: None,
after: None,
before: Some("".into()),
index: None,
};
let elem2 = rsm.into();
assert_eq!(elem, elem2);
}
2017-05-21 15:41:16 +01:00
#[test]
fn test_first_index() {
2018-12-18 15:32:05 +01:00
let elem: Element =
"<set xmlns='http://jabber.org/protocol/rsm'><first index='4'>coucou</first></set>"
.parse()
.unwrap();
let elem1 = elem.clone();
let set = SetResult::try_from(elem).unwrap();
let first = set.first.unwrap();
assert_eq!(first.item, "coucou");
assert_eq!(first.index, Some(4));
2017-05-21 15:41:16 +01:00
let set2 = SetResult {
first: Some(First {
item: String::from("coucou"),
index: Some(4),
}),
2017-05-21 15:41:16 +01:00
last: None,
count: None,
2017-05-21 15:41:16 +01:00
};
let elem2 = set2.into();
assert_eq!(elem1, elem2);
2017-05-21 15:41:16 +01:00
}
2017-04-29 04:37:18 +01:00
}