add getters for node and domain on Jid

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2019-07-20 19:01:25 +02:00
parent 36b51171ce
commit 750562cd96
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -92,6 +92,22 @@ impl From<FullJid> for Jid {
}
}
impl Jid {
/// The node part of the Jabber ID, if it exists, else None.
pub fn node(self) -> Option<String> {
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!(