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/.
|
|
|
|
|
|
2017-07-20 19:03:15 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Set {
|
|
|
|
|
pub after: Option<String>,
|
|
|
|
|
pub before: Option<String>,
|
|
|
|
|
pub count: Option<usize>,
|
2017-05-21 14:30:22 +00:00
|
|
|
|
pub first: Option<String>,
|
|
|
|
|
pub first_index: Option<usize>,
|
2017-04-29 03:37:18 +00:00
|
|
|
|
pub index: Option<usize>,
|
|
|
|
|
pub last: Option<String>,
|
|
|
|
|
pub max: Option<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
impl TryFrom<Element> for Set {
|
2017-07-20 19:03:15 +00:00
|
|
|
|
type Err = Error;
|
2017-05-06 19:14:45 +00:00
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
fn try_from(elem: Element) -> Result<Set, Error> {
|
2017-05-06 19:14:45 +00:00
|
|
|
|
if !elem.is("set", ns::RSM) {
|
|
|
|
|
return Err(Error::ParseError("This is not a RSM element."));
|
|
|
|
|
}
|
2017-05-21 14:30:22 +00:00
|
|
|
|
let mut set = Set {
|
|
|
|
|
after: None,
|
|
|
|
|
before: None,
|
|
|
|
|
count: None,
|
|
|
|
|
first: None,
|
|
|
|
|
first_index: None,
|
|
|
|
|
index: None,
|
|
|
|
|
last: None,
|
|
|
|
|
max: None,
|
|
|
|
|
};
|
2017-05-06 19:14:45 +00:00
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if child.is("after", ns::RSM) {
|
2017-05-21 14:30:22 +00:00
|
|
|
|
if set.after.is_some() {
|
2017-05-06 19:14:45 +00:00
|
|
|
|
return Err(Error::ParseError("Set can’t have more than one after."));
|
|
|
|
|
}
|
2017-05-21 14:30:22 +00:00
|
|
|
|
set.after = Some(child.text());
|
2017-05-06 19:14:45 +00:00
|
|
|
|
} else if child.is("before", ns::RSM) {
|
2017-05-21 14:30:22 +00:00
|
|
|
|
if set.before.is_some() {
|
2017-05-06 19:14:45 +00:00
|
|
|
|
return Err(Error::ParseError("Set can’t have more than one before."));
|
|
|
|
|
}
|
2017-05-21 14:30:22 +00:00
|
|
|
|
set.before = Some(child.text());
|
2017-05-06 19:14:45 +00:00
|
|
|
|
} else if child.is("count", ns::RSM) {
|
2017-05-21 14:30:22 +00:00
|
|
|
|
if set.count.is_some() {
|
2017-05-06 19:14:45 +00:00
|
|
|
|
return Err(Error::ParseError("Set can’t have more than one count."));
|
|
|
|
|
}
|
2017-05-21 14:30:22 +00:00
|
|
|
|
set.count = Some(child.text().parse()?);
|
2017-05-06 19:14:45 +00:00
|
|
|
|
} else if child.is("first", ns::RSM) {
|
2017-05-21 14:30:22 +00:00
|
|
|
|
if set.first.is_some() {
|
2017-05-06 19:14:45 +00:00
|
|
|
|
return Err(Error::ParseError("Set can’t have more than one first."));
|
|
|
|
|
}
|
2017-05-21 15:08:25 +00:00
|
|
|
|
set.first_index = get_attr!(child, "index", optional);
|
2017-05-21 14:30:22 +00:00
|
|
|
|
set.first = Some(child.text());
|
2017-05-06 19:14:45 +00:00
|
|
|
|
} else if child.is("index", ns::RSM) {
|
2017-05-21 14:30:22 +00:00
|
|
|
|
if set.index.is_some() {
|
2017-05-06 19:14:45 +00:00
|
|
|
|
return Err(Error::ParseError("Set can’t have more than one index."));
|
|
|
|
|
}
|
2017-05-21 14:30:22 +00:00
|
|
|
|
set.index = Some(child.text().parse()?);
|
2017-05-06 19:14:45 +00:00
|
|
|
|
} else if child.is("last", ns::RSM) {
|
2017-05-21 14:30:22 +00:00
|
|
|
|
if set.last.is_some() {
|
2017-05-06 19:14:45 +00:00
|
|
|
|
return Err(Error::ParseError("Set can’t have more than one last."));
|
|
|
|
|
}
|
2017-05-21 14:30:22 +00:00
|
|
|
|
set.last = Some(child.text());
|
2017-05-06 19:14:45 +00:00
|
|
|
|
} else if child.is("max", ns::RSM) {
|
2017-05-21 14:30:22 +00:00
|
|
|
|
if set.max.is_some() {
|
2017-05-06 19:14:45 +00:00
|
|
|
|
return Err(Error::ParseError("Set can’t have more than one max."));
|
|
|
|
|
}
|
2017-05-21 14:30:22 +00:00
|
|
|
|
set.max = 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
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-21 14:30:22 +00:00
|
|
|
|
Ok(set)
|
2017-04-29 03:37:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
|
impl From<Set> for Element {
|
|
|
|
|
fn from(set: Set) -> Element {
|
2017-05-06 19:14:45 +00:00
|
|
|
|
let mut elem = Element::builder("set")
|
|
|
|
|
.ns(ns::RSM)
|
|
|
|
|
.build();
|
2017-07-20 19:36:13 +00:00
|
|
|
|
if set.after.is_some() {
|
|
|
|
|
elem.append_child(Element::builder("after").ns(ns::RSM).append(set.after).build());
|
2017-05-06 19:14:45 +00:00
|
|
|
|
}
|
2017-07-20 19:36:13 +00:00
|
|
|
|
if set.before.is_some() {
|
|
|
|
|
elem.append_child(Element::builder("before").ns(ns::RSM).append(set.before).build());
|
2017-05-06 19:14:45 +00:00
|
|
|
|
}
|
2017-07-29 02:03:45 +00:00
|
|
|
|
if let Some(count) = set.count {
|
|
|
|
|
elem.append_child(Element::builder("count").ns(ns::RSM).append(format!("{}", count)).build());
|
2017-05-06 19:14:45 +00:00
|
|
|
|
}
|
2017-07-20 19:36:13 +00:00
|
|
|
|
if set.first.is_some() {
|
2017-05-06 19:14:45 +00:00
|
|
|
|
elem.append_child(Element::builder("first")
|
|
|
|
|
.ns(ns::RSM)
|
2017-07-20 19:36:13 +00:00
|
|
|
|
.attr("index", set.first_index)
|
|
|
|
|
.append(set.first).build());
|
2017-05-06 19:14:45 +00:00
|
|
|
|
}
|
2017-07-29 02:03:45 +00:00
|
|
|
|
if let Some(index) = set.index {
|
|
|
|
|
elem.append_child(Element::builder("index").ns(ns::RSM).append(format!("{}", index)).build());
|
2017-05-06 19:14:45 +00:00
|
|
|
|
}
|
2017-07-20 19:36:13 +00:00
|
|
|
|
if set.last.is_some() {
|
|
|
|
|
elem.append_child(Element::builder("last").ns(ns::RSM).append(set.last).build());
|
2017-05-06 19:14:45 +00:00
|
|
|
|
}
|
2017-07-29 02:03:45 +00:00
|
|
|
|
if let Some(max) = set.max {
|
|
|
|
|
elem.append_child(Element::builder("max").ns(ns::RSM).append(format!("{}", max)).build());
|
2017-05-06 19:14:45 +00:00
|
|
|
|
}
|
|
|
|
|
elem
|
2017-04-29 03:37:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2017-05-06 19:14:45 +00:00
|
|
|
|
use super::*;
|
2017-08-18 23:04:18 +00:00
|
|
|
|
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();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let set = Set::try_from(elem).unwrap();
|
2017-04-29 03:37:18 +00:00
|
|
|
|
assert_eq!(set.after, None);
|
|
|
|
|
assert_eq!(set.before, None);
|
|
|
|
|
assert_eq!(set.count, None);
|
|
|
|
|
match set.first {
|
|
|
|
|
Some(_) => panic!(),
|
|
|
|
|
None => (),
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(set.index, None);
|
|
|
|
|
assert_eq!(set.last, None);
|
|
|
|
|
assert_eq!(set.max, None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_unknown() {
|
|
|
|
|
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Set::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 element.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_child() {
|
|
|
|
|
let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'><coucou/></set>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Set::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();
|
2017-05-06 19:14:45 +00:00
|
|
|
|
let rsm = Set {
|
2017-04-29 03:37:18 +00:00
|
|
|
|
after: None,
|
|
|
|
|
before: None,
|
|
|
|
|
count: None,
|
|
|
|
|
first: None,
|
2017-05-21 14:30:22 +00:00
|
|
|
|
first_index: None,
|
2017-04-29 03:37:18 +00:00
|
|
|
|
index: None,
|
|
|
|
|
last: None,
|
|
|
|
|
max: None,
|
|
|
|
|
};
|
2017-05-23 22:31:33 +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();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem1 = elem.clone();
|
|
|
|
|
let set = Set::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 = Set {
|
|
|
|
|
after: None,
|
|
|
|
|
before: None,
|
|
|
|
|
count: None,
|
|
|
|
|
first: Some(String::from("coucou")),
|
|
|
|
|
first_index: Some(4),
|
|
|
|
|
index: None,
|
|
|
|
|
last: None,
|
|
|
|
|
max: None,
|
|
|
|
|
};
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem2 = set2.into();
|
2017-08-18 23:04:18 +00:00
|
|
|
|
assert!(elem1.compare_to(&elem2));
|
2017-05-21 14:41:16 +00:00
|
|
|
|
}
|
2017-04-29 03:37:18 +00:00
|
|
|
|
}
|