JIDs now have typed and stringy methods for node/domain/resource access
Jid now has typed with_resource and stringy with_resource_str Jid now has is_full, is_bare
This commit is contained in:
parent
199b3ae7ae
commit
4266368a98
8 changed files with 177 additions and 39 deletions
|
|
@ -1,10 +1,8 @@
|
|||
use stringprep::{nameprep, nodeprep, resourceprep};
|
||||
|
||||
use crate::Error;
|
||||
use std::fmt;
|
||||
|
||||
/// The [`NodePart`] is the optional part before the (optional) `@` in any [`Jid`], whether [`BareJid`] or [`FullJid`].
|
||||
#[derive(Clone, Debug, PartialEq, Hash, PartialOrd)]
|
||||
pub struct NodePart(pub(crate) String);
|
||||
use crate::Error;
|
||||
|
||||
fn length_check(len: usize, error_empty: Error, error_too_long: Error) -> Result<(), Error> {
|
||||
if len == 0 {
|
||||
|
|
@ -16,6 +14,10 @@ fn length_check(len: usize, error_empty: Error, error_too_long: Error) -> Result
|
|||
}
|
||||
}
|
||||
|
||||
/// The [`NodePart`] is the optional part before the (optional) `@` in any [`Jid`], whether [`BareJid`] or [`FullJid`].
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct NodePart(pub(crate) String);
|
||||
|
||||
impl NodePart {
|
||||
/// Build a new [`NodePart`] from a string slice. Will fail in case of stringprep validation error.
|
||||
pub fn new(s: &str) -> Result<NodePart, Error> {
|
||||
|
|
@ -23,10 +25,20 @@ impl NodePart {
|
|||
length_check(node.len(), Error::NodeEmpty, Error::NodeTooLong)?;
|
||||
Ok(NodePart(node.to_string()))
|
||||
}
|
||||
|
||||
pub(crate) fn new_unchecked(s: &str) -> NodePart {
|
||||
NodePart(s.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for NodePart {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// The [`DomainPart`] is the part between the (optional) `@` and the (optional) `/` in any [`Jid`], whether [`BareJid`] or [`FullJid`].
|
||||
#[derive(Clone, Debug, PartialEq, Hash, PartialOrd)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct DomainPart(pub(crate) String);
|
||||
|
||||
impl DomainPart {
|
||||
|
|
@ -36,10 +48,20 @@ impl DomainPart {
|
|||
length_check(domain.len(), Error::DomainEmpty, Error::DomainTooLong)?;
|
||||
Ok(DomainPart(domain.to_string()))
|
||||
}
|
||||
|
||||
pub(crate) fn new_unchecked(s: &str) -> DomainPart {
|
||||
DomainPart(s.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for DomainPart {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// The [`ResourcePart`] is the optional part after the `/` in a [`Jid`]. It is mandatory in [`FullJid`].
|
||||
#[derive(Clone, Debug, PartialEq, Hash, PartialOrd)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct ResourcePart(pub(crate) String);
|
||||
|
||||
impl ResourcePart {
|
||||
|
|
@ -49,4 +71,14 @@ impl ResourcePart {
|
|||
length_check(resource.len(), Error::ResourceEmpty, Error::ResourceTooLong)?;
|
||||
Ok(ResourcePart(resource.to_string()))
|
||||
}
|
||||
|
||||
pub(crate) fn new_unchecked(s: &str) -> ResourcePart {
|
||||
ResourcePart(s.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ResourcePart {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue