From ef079048074eae27a086b16f871fb6249d3a39f0 Mon Sep 17 00:00:00 2001 From: lumi Date: Mon, 27 Feb 2017 15:30:39 +0100 Subject: [PATCH] add with_node, with_domain and with_resource to Jid --- src/jid.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/jid.rs b/src/jid.rs index b277aa3..2436d39 100644 --- a/src/jid.rs +++ b/src/jid.rs @@ -217,6 +217,79 @@ impl Jid { resource: Some(resource.into()), } } + + /// Constructs a new Jabber ID from an existing one, with the node swapped out with a new one. + /// + /// # Examples + /// + /// ``` + /// use xmpp::jid::Jid; + /// + /// let jid = Jid::domain("domain"); + /// + /// assert_eq!(jid.node, None); + /// + /// let new_jid = jid.with_node("node"); + /// + /// assert_eq!(new_jid.node, Some("node".to_owned())); + /// ``` + pub fn with_node(&self, node: S) -> Jid + where S: Into { + Jid { + node: Some(node.into()), + domain: self.domain.clone(), + resource: self.resource.clone(), + } + } + + /// Constructs a new Jabber ID from an existing one, with the domain swapped out with a new one. + /// + /// # Examples + /// + /// ``` + /// use xmpp::jid::Jid; + /// + /// let jid = Jid::domain("domain"); + /// + /// assert_eq!(jid.domain, "domain"); + /// + /// let new_jid = jid.with_domain("new_domain"); + /// + /// assert_eq!(new_jid.domain, "new_domain"); + /// ``` + pub fn with_domain(&self, domain: S) -> Jid + where S: Into { + Jid { + node: self.node.clone(), + domain: domain.into(), + resource: self.resource.clone(), + } + } + + /// Constructs a new Jabber ID from an existing one, with the resource swapped out with a new one. + /// + /// # Examples + /// + /// ``` + /// use xmpp::jid::Jid; + /// + /// let jid = Jid::domain("domain"); + /// + /// assert_eq!(jid.resource, None); + /// + /// let new_jid = jid.with_resource("resource"); + /// + /// assert_eq!(new_jid.resource, Some("resource".to_owned())); + /// ``` + pub fn with_resource(&self, resource: S) -> Jid + where S: Into { + Jid { + node: self.node.clone(), + domain: self.domain.clone(), + resource: Some(resource.into()), + } + } + } #[cfg(test)]