From 750562cd966c3267ef58b663ab846a1526371d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 20 Jul 2019 19:01:25 +0200 Subject: [PATCH] add getters for node and domain on Jid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/lib.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 2401792b..62847c59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,6 +92,22 @@ impl From for Jid { } } +impl Jid { + /// The node part of the Jabber ID, if it exists, else None. + pub fn node(self) -> Option { + match self { + Jid::Bare(BareJid { node, .. }) | Jid::Full(FullJid { node, .. }) => node, + } + } + + /// The domain of the Jabber ID. + pub fn domain(self) -> String { + match self { + Jid::Bare(BareJid { domain, .. }) | Jid::Full(FullJid { domain, .. }) => domain, + } + } +} + /// A struct representing a full Jabber ID. /// /// A full Jabber ID is composed of 3 components, of which one is optional: @@ -639,6 +655,22 @@ mod tests { ); } + #[test] + fn node_from_jid() { + assert_eq!( + Jid::Full(FullJid::new("a", "b.c", "d")).node(), + Some(String::from("a")), + ); + } + + #[test] + fn domain_from_jid() { + assert_eq!( + Jid::Bare(BareJid::new("a", "b.c")).domain(), + String::from("b.c"), + ); + } + #[test] fn serialise() { assert_eq!(