diff --git a/jid/CHANGELOG.md b/jid/CHANGELOG.md index a252435..4344a3e 100644 --- a/jid/CHANGELOG.md +++ b/jid/CHANGELOG.md @@ -2,8 +2,10 @@ Unreleased * Breaking - serde: Jid is now using untagged enum representation (#66) + - remove BareJid From and From for correctness reasons (#71) * Additions - From<&Jid> is now implemented for String (#69) + - Jid::bare and FullJid::bare extract BareJid from Jid/FullJid (#71) Version 0.9.3, release 2022-03-07: * Updates diff --git a/jid/src/lib.rs b/jid/src/lib.rs index fa89863..c93675c 100644 --- a/jid/src/lib.rs +++ b/jid/src/lib.rs @@ -133,6 +133,14 @@ impl fmt::Display for Jid { } impl Jid { + /// Extracts the [`BareJid`] from the Jid + pub fn bare(&self) -> BareJid { + match self { + Self::Bare(jid) => jid.clone(), + Self::Full(jid) => jid.bare(), + } + } + /// The node part of the Jabber ID, if it exists, else None. pub fn node(self) -> Option { match self { @@ -148,15 +156,6 @@ impl Jid { } } -impl From for BareJid { - fn from(jid: Jid) -> BareJid { - match jid { - Jid::Full(full) => full.into(), - Jid::Bare(bare) => bare, - } - } -} - impl TryFrom for FullJid { type Error = JidParseError; @@ -279,15 +278,6 @@ impl From<&BareJid> for String { } } -impl From for BareJid { - fn from(full: FullJid) -> BareJid { - BareJid { - node: full.node, - domain: full.domain, - } - } -} - impl fmt::Debug for FullJid { fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(fmt, "FullJID({})", self) @@ -486,6 +476,14 @@ impl FullJid { } } + /// Extracts the [`BareJid`] from the full JID + pub fn bare(&self) -> BareJid { + BareJid { + node: self.node.clone(), + domain: self.domain.clone(), + } + } + /// Constructs a new Jabber ID from an existing one, with the node swapped out with a new one. /// /// # Examples @@ -806,7 +804,7 @@ mod tests { #[test] fn full_to_bare_jid() { - let bare: BareJid = FullJid::new("a", "b.c", "d").into(); + let bare: BareJid = FullJid::new("a", "b.c", "d").bare(); assert_eq!(bare, BareJid::new("a", "b.c")); } @@ -844,8 +842,8 @@ mod tests { FullJid::try_from(Jid::Bare(bare.clone())), Err(JidParseError::NoResource), ); - assert_eq!(BareJid::from(Jid::Full(full.clone())), bare.clone(),); - assert_eq!(BareJid::from(Jid::Bare(bare.clone())), bare,); + assert_eq!(Jid::Full(full.clone()).bare(), bare.clone(),); + assert_eq!(Jid::Bare(bare.clone()).bare(), bare,); } #[test] diff --git a/xmpp/src/lib.rs b/xmpp/src/lib.rs index 7df2300..56adf7f 100644 --- a/xmpp/src/lib.rs +++ b/xmpp/src/lib.rs @@ -348,7 +348,7 @@ impl Agent { let event = match from.clone() { Jid::Full(full) => Event::RoomMessage( message.id.clone(), - from.clone().into(), + from.bare(), full.resource, body.clone(), ), @@ -359,8 +359,7 @@ impl Agent { events.push(event) } MessageType::Chat | MessageType::Normal => { - let event = - Event::ChatMessage(message.id.clone(), from.clone().into(), body.clone()); + let event = Event::ChatMessage(message.id.clone(), from.bare(), body.clone()); events.push(event) } _ => (),