2023-06-21 11:52:31 +00:00
|
|
|
use stringprep::{nameprep, nodeprep, resourceprep};
|
|
|
|
|
2023-06-21 16:30:25 +00:00
|
|
|
use std::fmt;
|
2023-06-21 11:52:31 +00:00
|
|
|
|
2023-06-21 16:30:25 +00:00
|
|
|
use crate::Error;
|
2023-06-21 11:52:31 +00:00
|
|
|
|
|
|
|
fn length_check(len: usize, error_empty: Error, error_too_long: Error) -> Result<(), Error> {
|
|
|
|
if len == 0 {
|
|
|
|
Err(error_empty)
|
|
|
|
} else if len > 1023 {
|
|
|
|
Err(error_too_long)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 11:52:18 +00:00
|
|
|
/// The [`NodePart`] is the optional part before the (optional) `@` in any [`Jid`][crate::Jid],
|
|
|
|
/// whether [`BareJid`][crate::BareJid] or [`FullJid`][crate::FullJid].
|
2023-06-21 16:30:25 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
|
|
|
pub struct NodePart(pub(crate) String);
|
|
|
|
|
2023-06-21 11:52:31 +00:00
|
|
|
impl NodePart {
|
2023-12-16 11:52:18 +00:00
|
|
|
/// Build a new [`NodePart`] from a string slice. Will fail in case of stringprep validation
|
|
|
|
/// error.
|
2023-06-21 11:52:31 +00:00
|
|
|
pub fn new(s: &str) -> Result<NodePart, Error> {
|
|
|
|
let node = nodeprep(s).map_err(|_| Error::NodePrep)?;
|
|
|
|
length_check(node.len(), Error::NodeEmpty, Error::NodeTooLong)?;
|
|
|
|
Ok(NodePart(node.to_string()))
|
|
|
|
}
|
2023-06-21 16:30:25 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2023-06-21 11:52:31 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 11:52:18 +00:00
|
|
|
/// The [`DomainPart`] is the part between the (optional) `@` and the (optional) `/` in any
|
|
|
|
/// [`Jid`][crate::Jid], whether [`BareJid`][crate::BareJid] or [`FullJid`][crate::FullJid].
|
2023-06-21 16:30:25 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
2023-06-21 11:52:31 +00:00
|
|
|
pub struct DomainPart(pub(crate) String);
|
|
|
|
|
|
|
|
impl DomainPart {
|
2023-12-16 11:52:18 +00:00
|
|
|
/// Build a new [`DomainPart`] from a string slice. Will fail in case of stringprep validation
|
|
|
|
/// error.
|
2023-06-21 11:52:31 +00:00
|
|
|
pub fn new(s: &str) -> Result<DomainPart, Error> {
|
|
|
|
let domain = nameprep(s).map_err(|_| Error::NamePrep)?;
|
|
|
|
length_check(domain.len(), Error::DomainEmpty, Error::DomainTooLong)?;
|
|
|
|
Ok(DomainPart(domain.to_string()))
|
|
|
|
}
|
2023-06-21 16:30:25 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2023-06-21 11:52:31 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 11:52:18 +00:00
|
|
|
/// The [`ResourcePart`] is the optional part after the `/` in a [`Jid`][crate::Jid]. It is
|
|
|
|
/// mandatory in [`FullJid`][crate::FullJid].
|
2023-06-21 16:30:25 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
2023-06-21 11:52:31 +00:00
|
|
|
pub struct ResourcePart(pub(crate) String);
|
|
|
|
|
|
|
|
impl ResourcePart {
|
2023-12-16 11:52:18 +00:00
|
|
|
/// Build a new [`ResourcePart`] from a string slice. Will fail in case of stringprep
|
|
|
|
/// validation error.
|
2023-06-21 11:52:31 +00:00
|
|
|
pub fn new(s: &str) -> Result<ResourcePart, Error> {
|
|
|
|
let resource = resourceprep(s).map_err(|_| Error::ResourcePrep)?;
|
|
|
|
length_check(resource.len(), Error::ResourceEmpty, Error::ResourceTooLong)?;
|
|
|
|
Ok(ResourcePart(resource.to_string()))
|
|
|
|
}
|
2023-06-21 16:30:25 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2023-06-21 11:52:31 +00:00
|
|
|
}
|