Don't implement Into<BareJid> for FullJid/Jid. Add bare method instead (closes #71)

This commit is contained in:
xmppftw 2023-06-01 12:32:03 +02:00 committed by xmpp ftw
parent c8dcf5e7a7
commit ca7f151cc7
3 changed files with 23 additions and 24 deletions

View file

@ -2,8 +2,10 @@ Unreleased
* Breaking
- serde: Jid is now using untagged enum representation (#66)
- remove BareJid From<Jid> and From<FullJid> 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

View file

@ -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<String> {
match self {
@ -148,15 +156,6 @@ impl Jid {
}
}
impl From<Jid> for BareJid {
fn from(jid: Jid) -> BareJid {
match jid {
Jid::Full(full) => full.into(),
Jid::Bare(bare) => bare,
}
}
}
impl TryFrom<Jid> for FullJid {
type Error = JidParseError;
@ -279,15 +278,6 @@ impl From<&BareJid> for String {
}
}
impl From<FullJid> 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]

View file

@ -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)
}
_ => (),