mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
Add more helpers on Jid to convert to Bare/FullJid
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
1638288644
commit
0138a62957
1 changed files with 44 additions and 1 deletions
45
src/lib.rs
45
src/lib.rs
|
@ -14,7 +14,7 @@
|
|||
//!
|
||||
//! For usage, check the documentation on the `Jid` struct.
|
||||
|
||||
use std::convert::Into;
|
||||
use std::convert::{Into, TryFrom};
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
|
@ -121,6 +121,26 @@ 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;
|
||||
|
||||
fn try_from(jid: Jid) -> Result<Self, Self::Error> {
|
||||
match jid {
|
||||
Jid::Full(full) => Ok(full),
|
||||
Jid::Bare(_) => Err(JidParseError::NoResource),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A struct representing a full Jabber ID.
|
||||
///
|
||||
/// A full Jabber ID is composed of 3 components, of which one is optional:
|
||||
|
@ -684,6 +704,29 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jid_to_full_bare() {
|
||||
let full = FullJid::new("a", "b.c", "d");
|
||||
let bare = BareJid::new("a", "b.c");
|
||||
|
||||
assert_eq!(
|
||||
FullJid::try_from(Jid::Full(full.clone())),
|
||||
Ok(full.clone()),
|
||||
);
|
||||
assert_eq!(
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialise() {
|
||||
assert_eq!(
|
||||
|
|
Loading…
Reference in a new issue