From 8ac460224060a888778e7f84671c448d1cc747b9 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Mon, 26 Jun 2017 19:49:16 +0100 Subject: [PATCH] presence: Add a simpler way to create a Presence. --- src/presence.rs | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/presence.rs b/src/presence.rs index d04a3d2e..c85f25be 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -198,6 +198,21 @@ pub struct Presence { pub payloads: Vec, } +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 for Presence { type Error = Error; @@ -310,16 +325,7 @@ mod tests { #[test] fn test_serialise() { let elem: Element = "".parse().unwrap(); - let presence = Presence { - from: None, - to: None, - id: None, - type_: Type::Unavailable, - show: Show::None, - statuses: BTreeMap::new(), - priority: 0i8, - payloads: vec!(), - }; + let presence = Presence::new(Type::Unavailable); let elem2 = presence.into(); assert_eq!(elem, elem2); } @@ -446,18 +452,8 @@ mod tests { #[test] fn test_serialise_status() { let status = Status::from("Hello world!"); - let mut statuses = BTreeMap::new(); - 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 mut presence = Presence::new(Type::Unavailable); + presence.statuses.insert(String::from(""), status); let elem: Element = presence.into(); assert!(elem.is("presence", ns::JABBER_CLIENT)); assert!(elem.children().collect::>()[0].is("status", ns::JABBER_CLIENT));