presence: Add a simpler way to create a Presence.

This commit is contained in:
Emmanuel Gil Peyrot 2017-06-26 19:49:16 +01:00
parent ad1d4adcec
commit 8ac4602240

View file

@ -198,6 +198,21 @@ pub struct Presence {
pub payloads: Vec<Element>, pub payloads: Vec<Element>,
} }
impl Presence {
pub fn new(type_: Type) -> Presence {
Presence {
from: None,
to: None,
id: None,
type_: type_,
show: Show::None,
statuses: BTreeMap::new(),
priority: 0i8,
payloads: vec!(),
}
}
}
impl TryFrom<Element> for Presence { impl TryFrom<Element> for Presence {
type Error = Error; type Error = Error;
@ -310,16 +325,7 @@ mod tests {
#[test] #[test]
fn test_serialise() { fn test_serialise() {
let elem: Element = "<presence xmlns='jabber:client' type='unavailable'/>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client' type='unavailable'/>".parse().unwrap();
let presence = Presence { let presence = Presence::new(Type::Unavailable);
from: None,
to: None,
id: None,
type_: Type::Unavailable,
show: Show::None,
statuses: BTreeMap::new(),
priority: 0i8,
payloads: vec!(),
};
let elem2 = presence.into(); let elem2 = presence.into();
assert_eq!(elem, elem2); assert_eq!(elem, elem2);
} }
@ -446,18 +452,8 @@ mod tests {
#[test] #[test]
fn test_serialise_status() { fn test_serialise_status() {
let status = Status::from("Hello world!"); let status = Status::from("Hello world!");
let mut statuses = BTreeMap::new(); let mut presence = Presence::new(Type::Unavailable);
statuses.insert(String::from(""), status); presence.statuses.insert(String::from(""), status);
let presence = Presence {
from: None,
to: None,
id: None,
type_: Type::Unavailable,
show: Show::None,
statuses: statuses,
priority: 0i8,
payloads: vec!(),
};
let elem: Element = presence.into(); let elem: Element = presence.into();
assert!(elem.is("presence", ns::JABBER_CLIENT)); assert!(elem.is("presence", ns::JABBER_CLIENT));
assert!(elem.children().collect::<Vec<_>>()[0].is("status", ns::JABBER_CLIENT)); assert!(elem.children().collect::<Vec<_>>()[0].is("status", ns::JABBER_CLIENT));