From cdf80c897993a1ce0e1f959a01c09a01aae21df6 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 6 Sep 2019 15:00:14 +0200 Subject: [PATCH] presence: Simplify constructors. --- ChangeLog | 2 ++ src/presence.rs | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index ed5aae0..372ebdd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,8 @@ Version NEXT: DATE Emmanuel Gil Peyrot * New parsers/serialisers: - OpenPGP for XMPP (XEP-0373) + * Breaking changes: + - Presence constructors now take Into and assume Some. Version 0.15.0: 2019-09-06 Emmanuel Gil Peyrot diff --git a/src/presence.rs b/src/presence.rs index 4aad162..14ae559 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -194,21 +194,21 @@ impl Presence { /// Set the emitter of this presence, this should only be useful for /// servers and components, as clients can only send presences from their /// own resource (which is implicit). - pub fn with_from(mut self, from: Option) -> Presence { - self.from = from; + pub fn with_from>(mut self, from: J) -> Presence { + self.from = Some(from.into()); self } /// Set the recipient of this presence, this is only useful for directed /// presences. - pub fn with_to(mut self, to: Option) -> Presence { - self.to = to; + pub fn with_to>(mut self, to: J) -> Presence { + self.to = Some(to.into()); self } /// Set the identifier for this presence. - pub fn with_id(mut self, id: Option) -> Presence { - self.id = id; + pub fn with_id(mut self, id: String) -> Presence { + self.id = Some(id); self } @@ -345,6 +345,7 @@ impl From for Element { mod tests { use super::*; use crate::util::compare_elements::NamespaceAwareCompare; + use jid::{BareJid, FullJid}; #[cfg(target_pointer_width = "32")] #[test] @@ -634,4 +635,31 @@ mod tests { assert!(priority.is("priority", ns::DEFAULT_NS)); assert_eq!(priority.text(), "42"); } + + #[test] + fn presence_with_to() { + let presence = Presence::new(Type::None); + let elem: Element = presence.into(); + assert_eq!(elem.attr("to"), None); + + let presence = Presence::new(Type::None) + .with_to(Jid::Bare(BareJid::domain("localhost"))); + let elem: Element = presence.into(); + assert_eq!(elem.attr("to"), Some("localhost")); + + let presence = Presence::new(Type::None) + .with_to(BareJid::domain("localhost")); + let elem: Element = presence.into(); + assert_eq!(elem.attr("to"), Some("localhost")); + + let presence = Presence::new(Type::None) + .with_to(Jid::Full(FullJid::new("test", "localhost", "coucou"))); + let elem: Element = presence.into(); + assert_eq!(elem.attr("to"), Some("test@localhost/coucou")); + + let presence = Presence::new(Type::None) + .with_to(FullJid::new("test", "localhost", "coucou")); + let elem: Element = presence.into(); + assert_eq!(elem.attr("to"), Some("test@localhost/coucou")); + } }