xmpp-rs/src/rsm.rs

260 lines
9.1 KiB
Rust
Raw Normal View History

2017-04-29 21:14:34 +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/.
#![deny(missing_docs)]
use try_from::TryFrom;
2017-05-06 19:14:45 +00:00
2017-04-29 03:37:18 +00:00
use minidom::Element;
use error::Error;
use ns;
/// Represents paging through a list of items, represented by an UID.
2017-04-29 03:37:18 +00:00
#[derive(Debug, Clone)]
pub struct SetQuery {
/// Limit the number of items, or use the recipients defaults if None.
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.
2017-04-29 03:37:18 +00: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.
2017-04-29 03:37:18 +00:00
pub before: Option<String>,
/// Numerical index of the page (deprecated).
2017-04-29 03:37:18 +00:00
pub index: Option<usize>,
}
impl TryFrom<Element> for SetQuery {
type Err = Error;
2017-05-06 19:14:45 +00:00
fn try_from(elem: Element) -> Result<SetQuery, Error> {
2018-05-14 14:30:28 +00:00
check_self!(elem, "set", RSM, "RSM set");
let mut set = SetQuery {
max: None,
after: None,
before: None,
index: None,
};
2017-05-06 19:14:45 +00:00
for child in elem.children() {
if child.is("max", ns::RSM) {
if set.max.is_some() {
return Err(Error::ParseError("Set cant have more than one max."));
}
set.max = Some(child.text().parse()?);
} else if child.is("after", ns::RSM) {
if set.after.is_some() {
2017-05-06 19:14:45 +00:00
return Err(Error::ParseError("Set cant have more than one after."));
}
set.after = Some(child.text());
2017-05-06 19:14:45 +00:00
} else if child.is("before", ns::RSM) {
if set.before.is_some() {
2017-05-06 19:14:45 +00:00
return Err(Error::ParseError("Set cant have more than one before."));
}
set.before = Some(child.text());
} else if child.is("index", ns::RSM) {
if set.index.is_some() {
return Err(Error::ParseError("Set cant have more than one index."));
2017-05-06 19:14:45 +00:00
}
set.index = Some(child.text().parse()?);
} else {
return Err(Error::ParseError("Unknown child in set element."));
}
}
Ok(set)
}
}
impl From<SetQuery> for Element {
fn from(set: SetQuery) -> Element {
Element::builder("set")
.ns(ns::RSM)
.append(set.max.map(|max| Element::builder("max").ns(ns::RSM).append(format!("{}", max)).build()))
.append(set.after.map(|after| Element::builder("after").ns(ns::RSM).append(after).build()))
.append(set.before.map(|before| Element::builder("before").ns(ns::RSM).append(before).build()))
.append(set.index.map(|index| Element::builder("index").ns(ns::RSM).append(format!("{}", index)).build()))
.build()
}
}
/// Represents paging through a list of items, represented by an UID.
#[derive(Debug, Clone)]
pub struct SetResult {
/// The UID of the first item of the page.
pub first: Option<String>,
/// The position of the [first item](#structfield.first) in the full set
/// (which may be approximate).
pub first_index: Option<usize>,
/// The UID of the last item of the page.
pub last: Option<String>,
/// How many items there are in the full set (which may be approximate).
pub count: Option<usize>,
}
impl TryFrom<Element> for SetResult {
type Err = Error;
fn try_from(elem: Element) -> Result<SetResult, Error> {
check_self!(elem, "set", RSM, "RSM set");
let mut set = SetResult {
first: None,
first_index: None,
last: None,
count: None,
};
for child in elem.children() {
if child.is("first", ns::RSM) {
if set.first.is_some() {
2017-05-06 19:14:45 +00:00
return Err(Error::ParseError("Set cant have more than one first."));
}
set.first_index = get_attr!(child, "index", optional);
set.first = Some(child.text());
2017-05-06 19:14:45 +00:00
} else if child.is("last", ns::RSM) {
if set.last.is_some() {
2017-05-06 19:14:45 +00:00
return Err(Error::ParseError("Set cant have more than one last."));
}
set.last = Some(child.text());
} else if child.is("count", ns::RSM) {
if set.count.is_some() {
return Err(Error::ParseError("Set cant have more than one count."));
2017-05-06 19:14:45 +00:00
}
set.count = Some(child.text().parse()?);
2017-05-06 19:14:45 +00:00
} else {
return Err(Error::ParseError("Unknown child in set element."));
2017-04-29 03:37:18 +00:00
}
}
Ok(set)
2017-04-29 03:37:18 +00:00
}
}
impl From<SetResult> for Element {
fn from(set: SetResult) -> Element {
2017-10-31 22:21:50 +00:00
let first = set.first.clone()
.map(|first| Element::builder("first")
.ns(ns::RSM)
.attr("index", set.first_index)
.append(first)
.build());
Element::builder("set")
.ns(ns::RSM)
.append(first)
.append(set.last.map(|last| Element::builder("last").ns(ns::RSM).append(last).build()))
.append(set.count.map(|count| Element::builder("count").ns(ns::RSM).append(format!("{}", count)).build()))
2017-10-31 22:21:50 +00:00
.build()
2017-04-29 03:37:18 +00:00
}
}
#[cfg(test)]
mod tests {
2017-05-06 19:14:45 +00:00
use super::*;
use compare_elements::NamespaceAwareCompare;
2017-04-29 03:37:18 +00:00
#[test]
fn test_simple() {
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 03:37:18 +00:00
assert_eq!(set.after, None);
assert_eq!(set.before, None);
assert_eq!(set.index, None);
let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>".parse().unwrap();
let set = SetResult::try_from(elem).unwrap();
2017-04-29 03:37:18 +00:00
match set.first {
Some(_) => panic!(),
None => (),
}
assert_eq!(set.last, None);
assert_eq!(set.count, None);
2017-04-29 03:37:18 +00:00
}
#[test]
fn test_unknown() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
let error = SetQuery::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "This is not a RSM set element.");
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
let error = SetResult::try_from(elem).unwrap_err();
2017-04-29 03:37:18 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "This is not a RSM set element.");
2017-04-29 03:37:18 +00:00
}
#[test]
fn test_invalid_child() {
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 {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in set element.");
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 03:37:18 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in set element.");
}
#[test]
fn test_serialise() {
let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>".parse().unwrap();
let rsm = SetQuery {
max: None,
2017-04-29 03:37:18 +00:00
after: None,
before: None,
index: None,
};
let elem2 = rsm.into();
assert_eq!(elem, elem2);
let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>".parse().unwrap();
let rsm = SetResult {
2017-04-29 03:37:18 +00:00
first: None,
first_index: None,
2017-04-29 03:37:18 +00:00
last: None,
count: None,
2017-04-29 03:37:18 +00:00
};
let elem2 = rsm.into();
2017-04-29 03:37:18 +00:00
assert_eq!(elem, elem2);
}
2017-05-21 14:41:16 +00:00
#[test]
fn test_first_index() {
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();
2017-05-21 14:41:16 +00:00
assert_eq!(set.first, Some(String::from("coucou")));
assert_eq!(set.first_index, Some(4));
let set2 = SetResult {
2017-05-21 14:41:16 +00:00
first: Some(String::from("coucou")),
first_index: Some(4),
last: None,
count: None,
2017-05-21 14:41:16 +00:00
};
let elem2 = set2.into();
assert!(elem1.compare_to(&elem2));
2017-05-21 14:41:16 +00:00
}
2017-04-29 03:37:18 +00:00
}