Offer {Resource,Node,Domain}Ref on Jid API

This provides a non-copying API, which is generally favourable. The
other accessors were removed, because the intent was to provide this
"most sensible" API via the "default" (i.e. shortest, most concisely
named) functions.
This commit is contained in:
Jonas Schäfer 2024-03-03 16:15:04 +01:00
parent 45f1567ff2
commit 7fce1146e0
10 changed files with 86 additions and 133 deletions

View file

@ -1,7 +1,20 @@
Version xxx, release xxx: Version xxx, release xxx:
* Breaking:
- With the addition of `str`-like types for `DomainPart`, `NodePart` and
`ResourcePart`, the functions on `Jid`, `BareJid` and `FullJid` which
return the respective types have been changed to return references
instead. Use `ToOwned::to_owned` to obtain a copy or `.as_str()` to
obtain a plain `str` reference.
- The `node_str`, `domain_str` and `resource_str` functions returning str
references have been removed from the JID types. Use `.as_str()` or
`.map(|x| x.as_str())` on the corresponding `node`/`domain`/`resource`
functions instead.
* Additions: * Additions:
- Add optional quote support. Implement quote::ToTokens for Jid, FullJid - Add optional quote support. Implement quote::ToTokens for Jid, FullJid
and BareJid. and BareJid.
- `str`-like reference types have been added for `DomainPart`, `NodePart`
and `ResourcePart`, called `DomainRef`, `NodeRef` and `ResourceRef`
respectively.
Version 0.10.0, release 2023-08-17: Version 0.10.0, release 2023-08-17:
* Breaking * Breaking

View file

@ -17,6 +17,8 @@ use std::borrow::Cow;
use std::str::FromStr; use std::str::FromStr;
use stringprep::{nameprep, nodeprep, resourceprep}; use stringprep::{nameprep, nodeprep, resourceprep};
use crate::parts::{DomainRef, NodeRef, ResourceRef};
fn length_check(len: usize, error_empty: Error, error_too_long: Error) -> Result<(), Error> { fn length_check(len: usize, error_empty: Error, error_too_long: Error) -> Result<(), Error> {
if len == 0 { if len == 0 {
Err(error_empty) Err(error_empty)
@ -107,36 +109,36 @@ impl InnerJid {
}) })
} }
pub(crate) fn node(&self) -> Option<&str> { pub(crate) fn node(&self) -> Option<&NodeRef> {
self.at.map(|at| { self.at.map(|at| {
let at = u16::from(at) as usize; let at = u16::from(at) as usize;
&self.normalized[..at] NodeRef::from_str_unchecked(&self.normalized[..at])
}) })
} }
pub(crate) fn domain(&self) -> &str { pub(crate) fn domain(&self) -> &DomainRef {
match (self.at, self.slash) { match (self.at, self.slash) {
(Some(at), Some(slash)) => { (Some(at), Some(slash)) => {
let at = u16::from(at) as usize; let at = u16::from(at) as usize;
let slash = u16::from(slash) as usize; let slash = u16::from(slash) as usize;
&self.normalized[at + 1..slash] DomainRef::from_str_unchecked(&self.normalized[at + 1..slash])
} }
(Some(at), None) => { (Some(at), None) => {
let at = u16::from(at) as usize; let at = u16::from(at) as usize;
&self.normalized[at + 1..] DomainRef::from_str_unchecked(&self.normalized[at + 1..])
} }
(None, Some(slash)) => { (None, Some(slash)) => {
let slash = u16::from(slash) as usize; let slash = u16::from(slash) as usize;
&self.normalized[..slash] DomainRef::from_str_unchecked(&self.normalized[..slash])
} }
(None, None) => &self.normalized, (None, None) => DomainRef::from_str_unchecked(&self.normalized),
} }
} }
pub(crate) fn resource(&self) -> Option<&str> { pub(crate) fn resource(&self) -> Option<&ResourceRef> {
self.slash.map(|slash| { self.slash.map(|slash| {
let slash = u16::from(slash) as usize; let slash = u16::from(slash) as usize;
&self.normalized[slash + 1..] ResourceRef::from_str_unchecked(&self.normalized[slash + 1..])
}) })
} }
} }

View file

@ -106,9 +106,9 @@ impl Jid {
/// # fn main() -> Result<(), Error> { /// # fn main() -> Result<(), Error> {
/// let jid = Jid::new("node@domain/resource")?; /// let jid = Jid::new("node@domain/resource")?;
/// ///
/// assert_eq!(jid.node_str(), Some("node")); /// assert_eq!(jid.node().map(|x| x.as_str()), Some("node"));
/// assert_eq!(jid.domain_str(), "domain"); /// assert_eq!(jid.domain().as_str(), "domain");
/// assert_eq!(jid.resource_str(), Some("resource")); /// assert_eq!(jid.resource().map(|x| x.as_str()), Some("resource"));
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
@ -132,9 +132,9 @@ impl Jid {
/// already been parsed and stringprepped into [`NodePart`], [`DomainPart`], and [`ResourcePart`]. /// already been parsed and stringprepped into [`NodePart`], [`DomainPart`], and [`ResourcePart`].
/// This method allocates and does not consume the typed parts. /// This method allocates and does not consume the typed parts.
pub fn from_parts( pub fn from_parts(
node: Option<&NodePart>, node: Option<&NodeRef>,
domain: &DomainPart, domain: &DomainRef,
resource: Option<&ResourcePart>, resource: Option<&ResourceRef>,
) -> Jid { ) -> Jid {
if let Some(resource) = resource { if let Some(resource) = resource {
Jid::Full(FullJid::from_parts(node, domain, resource)) Jid::Full(FullJid::from_parts(node, domain, resource))
@ -143,51 +143,23 @@ impl Jid {
} }
} }
/// The optional node part of the JID, as a [`NodePart`] /// The optional node part of the JID as reference.
pub fn node(&self) -> Option<NodePart> { pub fn node(&self) -> Option<&NodeRef> {
match self {
Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => {
inner.node().map(NodePart::new_unchecked)
}
}
}
/// The optional node part of the JID, as a stringy reference
pub fn node_str(&self) -> Option<&str> {
match self { match self {
Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => inner.node(), Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => inner.node(),
} }
} }
/// The domain part of the JID, as a [`DomainPart`] /// The domain part of the JID as reference
pub fn domain(&self) -> DomainPart { pub fn domain(&self) -> &DomainRef {
match self {
Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => {
DomainPart::new_unchecked(inner.domain())
}
}
}
/// The domain part of the JID, as a stringy reference
pub fn domain_str(&self) -> &str {
match self { match self {
Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => inner.domain(), Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => inner.domain(),
} }
} }
/// The optional resource part of the JID, as a [`ResourcePart`]. It is guaranteed to be present
/// when the JID is a Full variant, which you can check with [`Jid::is_full`].
pub fn resource(&self) -> Option<ResourcePart> {
match self {
Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => {
inner.resource().map(ResourcePart::new_unchecked)
}
}
}
/// The optional resource of the Jabber ID. It is guaranteed to be present when the JID is /// The optional resource of the Jabber ID. It is guaranteed to be present when the JID is
/// a Full variant, which you can check with [`Jid::is_full`]. /// a Full variant, which you can check with [`Jid::is_full`].
pub fn resource_str(&self) -> Option<&str> { pub fn resource(&self) -> Option<&ResourceRef> {
match self { match self {
Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => inner.resource(), Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => inner.resource(),
} }
@ -415,9 +387,9 @@ impl FullJid {
/// # fn main() -> Result<(), Error> { /// # fn main() -> Result<(), Error> {
/// let jid = FullJid::new("node@domain/resource")?; /// let jid = FullJid::new("node@domain/resource")?;
/// ///
/// assert_eq!(jid.node_str(), Some("node")); /// assert_eq!(jid.node().map(|x| x.as_str()), Some("node"));
/// assert_eq!(jid.domain_str(), "domain"); /// assert_eq!(jid.domain().as_str(), "domain");
/// assert_eq!(jid.resource_str(), "resource"); /// assert_eq!(jid.resource().as_str(), "resource");
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
@ -439,22 +411,27 @@ impl FullJid {
/// already been parsed and stringprepped into [`NodePart`], [`DomainPart`], and [`ResourcePart`]. /// already been parsed and stringprepped into [`NodePart`], [`DomainPart`], and [`ResourcePart`].
/// This method allocates and does not consume the typed parts. /// This method allocates and does not consume the typed parts.
pub fn from_parts( pub fn from_parts(
node: Option<&NodePart>, node: Option<&NodeRef>,
domain: &DomainPart, domain: &DomainRef,
resource: &ResourcePart, resource: &ResourceRef,
) -> FullJid { ) -> FullJid {
let (at, slash, normalized) = if let Some(node) = node { let (at, slash, normalized) = if let Some(node) = node {
// Parts are never empty so len > 0 for NonZeroU16::new is always Some // Parts are never empty so len > 0 for NonZeroU16::new is always Some
( (
NonZeroU16::new(node.0.len() as u16), NonZeroU16::new(node.len() as u16),
NonZeroU16::new((node.0.len() + 1 + domain.0.len()) as u16), NonZeroU16::new((node.len() + 1 + domain.len()) as u16),
format!("{}@{}/{}", node.0, domain.0, resource.0), format!(
"{}@{}/{}",
node.as_str(),
domain.as_str(),
resource.as_str()
),
) )
} else { } else {
( (
None, None,
NonZeroU16::new(domain.0.len() as u16), NonZeroU16::new(domain.len() as u16),
format!("{}/{}", domain.0, resource.0), format!("{}/{}", domain.as_str(), resource.as_str()),
) )
}; };
@ -467,34 +444,18 @@ impl FullJid {
FullJid { inner } FullJid { inner }
} }
/// The optional node part of the JID, as a [`NodePart`] /// The optional node part of the JID as reference.
pub fn node(&self) -> Option<NodePart> { pub fn node(&self) -> Option<&NodeRef> {
self.node_str().map(NodePart::new_unchecked)
}
/// The optional node part of the JID, as a stringy reference
pub fn node_str(&self) -> Option<&str> {
self.inner.node() self.inner.node()
} }
/// The domain part of the JID, as a [`DomainPart`] /// The domain part of the JID as reference
pub fn domain(&self) -> DomainPart { pub fn domain(&self) -> &DomainRef {
DomainPart::new_unchecked(self.domain_str())
}
/// The domain part of the JID, as a stringy reference
pub fn domain_str(&self) -> &str {
self.inner.domain() self.inner.domain()
} }
/// The optional resource part of the JID, as a [`ResourcePart`]. Since this is a full JID it
/// is always present.
pub fn resource(&self) -> ResourcePart {
ResourcePart::new_unchecked(self.resource_str())
}
/// The optional resource of the Jabber ID. Since this is a full JID it is always present. /// The optional resource of the Jabber ID. Since this is a full JID it is always present.
pub fn resource_str(&self) -> &str { pub fn resource(&self) -> &ResourceRef {
self.inner.resource().unwrap() self.inner.resource().unwrap()
} }
@ -542,8 +503,8 @@ impl BareJid {
/// # fn main() -> Result<(), Error> { /// # fn main() -> Result<(), Error> {
/// let jid = BareJid::new("node@domain")?; /// let jid = BareJid::new("node@domain")?;
/// ///
/// assert_eq!(jid.node_str(), Some("node")); /// assert_eq!(jid.node().map(|x| x.as_str()), Some("node"));
/// assert_eq!(jid.domain_str(), "domain"); /// assert_eq!(jid.domain().as_str(), "domain");
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
@ -564,15 +525,15 @@ impl BareJid {
/// Build a [`BareJid`] from typed parts. This method cannot fail because it uses parts that have /// Build a [`BareJid`] from typed parts. This method cannot fail because it uses parts that have
/// already been parsed and stringprepped into [`NodePart`] and [`DomainPart`]. This method allocates /// already been parsed and stringprepped into [`NodePart`] and [`DomainPart`]. This method allocates
/// and does not consume the typed parts. /// and does not consume the typed parts.
pub fn from_parts(node: Option<&NodePart>, domain: &DomainPart) -> BareJid { pub fn from_parts(node: Option<&NodeRef>, domain: &DomainRef) -> BareJid {
let (at, normalized) = if let Some(node) = node { let (at, normalized) = if let Some(node) = node {
// Parts are never empty so len > 0 for NonZeroU16::new is always Some // Parts are never empty so len > 0 for NonZeroU16::new is always Some
( (
NonZeroU16::new(node.0.len() as u16), NonZeroU16::new(node.len() as u16),
format!("{}@{}", node.0, domain.0), format!("{}@{}", node.as_str(), domain.as_str()),
) )
} else { } else {
(None, domain.0.clone()) (None, domain.to_string())
}; };
let inner = InnerJid { let inner = InnerJid {
@ -584,23 +545,13 @@ impl BareJid {
BareJid { inner } BareJid { inner }
} }
/// The optional node part of the JID, as a [`NodePart`] /// The optional node part of the JID as reference.
pub fn node(&self) -> Option<NodePart> { pub fn node(&self) -> Option<&NodeRef> {
self.node_str().map(NodePart::new_unchecked)
}
/// The optional node part of the JID, as a stringy reference
pub fn node_str(&self) -> Option<&str> {
self.inner.node() self.inner.node()
} }
/// The domain part of the JID, as a [`DomainPart`] /// The domain part of the JID as reference
pub fn domain(&self) -> DomainPart { pub fn domain(&self) -> &DomainRef {
DomainPart::new_unchecked(self.domain_str())
}
/// The domain part of the JID, as a stringy reference
pub fn domain_str(&self) -> &str {
self.inner.domain() self.inner.domain()
} }
@ -616,11 +567,11 @@ impl BareJid {
/// let bare = BareJid::new("node@domain").unwrap(); /// let bare = BareJid::new("node@domain").unwrap();
/// let full = bare.with_resource(&resource); /// let full = bare.with_resource(&resource);
/// ///
/// assert_eq!(full.node_str(), Some("node")); /// assert_eq!(full.node().map(|x| x.as_str()), Some("node"));
/// assert_eq!(full.domain_str(), "domain"); /// assert_eq!(full.domain().as_str(), "domain");
/// assert_eq!(full.resource_str(), "resource"); /// assert_eq!(full.resource().as_str(), "resource");
/// ``` /// ```
pub fn with_resource(&self, resource: &ResourcePart) -> FullJid { pub fn with_resource(&self, resource: &ResourceRef) -> FullJid {
let slash = NonZeroU16::new(self.inner.normalized.len() as u16); let slash = NonZeroU16::new(self.inner.normalized.len() as u16);
let normalized = format!("{}/{resource}", self.inner.normalized); let normalized = format!("{}/{resource}", self.inner.normalized);
let inner = InnerJid { let inner = InnerJid {
@ -643,9 +594,9 @@ impl BareJid {
/// let bare = BareJid::new("node@domain").unwrap(); /// let bare = BareJid::new("node@domain").unwrap();
/// let full = bare.with_resource_str("resource").unwrap(); /// let full = bare.with_resource_str("resource").unwrap();
/// ///
/// assert_eq!(full.node_str(), Some("node")); /// assert_eq!(full.node().map(|x| x.as_str()), Some("node"));
/// assert_eq!(full.domain_str(), "domain"); /// assert_eq!(full.domain().as_str(), "domain");
/// assert_eq!(full.resource_str(), "resource"); /// assert_eq!(full.resource().as_str(), "resource");
/// ``` /// ```
pub fn with_resource_str(&self, resource: &str) -> Result<FullJid, Error> { pub fn with_resource_str(&self, resource: &str) -> Result<FullJid, Error> {
let resource = ResourcePart::new(resource)?; let resource = ResourcePart::new(resource)?;
@ -796,30 +747,21 @@ mod tests {
fn node_from_jid() { fn node_from_jid() {
let jid = Jid::new("a@b.c/d").unwrap(); let jid = Jid::new("a@b.c/d").unwrap();
assert_eq!(jid.node_str(), Some("a"),); assert_eq!(jid.node().map(|x| x.as_str()), Some("a"),);
assert_eq!(jid.node(), Some(NodePart::new("a").unwrap().into_owned()));
} }
#[test] #[test]
fn domain_from_jid() { fn domain_from_jid() {
let jid = Jid::new("a@b.c").unwrap(); let jid = Jid::new("a@b.c").unwrap();
assert_eq!(jid.domain_str(), "b.c"); assert_eq!(jid.domain().as_str(), "b.c");
assert_eq!(jid.domain(), DomainPart::new("b.c").unwrap().into_owned());
} }
#[test] #[test]
fn resource_from_jid() { fn resource_from_jid() {
let jid = Jid::new("a@b.c/d").unwrap(); let jid = Jid::new("a@b.c/d").unwrap();
assert_eq!(jid.resource_str(), Some("d"),); assert_eq!(jid.resource().map(|x| x.as_str()), Some("d"),);
assert_eq!(
jid.resource(),
Some(ResourcePart::new("d").unwrap().into_owned())
);
} }
#[test] #[test]

View file

@ -77,10 +77,6 @@ macro_rules! def_part_types {
pub fn into_inner(self) -> String { pub fn into_inner(self) -> String {
self.0 self.0
} }
pub(crate) fn new_unchecked(s: &str) -> Self {
$name(s.to_string())
}
} }
impl FromStr for $name { impl FromStr for $name {
@ -163,7 +159,7 @@ macro_rules! def_part_types {
pub struct $borrowed(pub(crate) str); pub struct $borrowed(pub(crate) str);
impl $borrowed { impl $borrowed {
fn from_str_unchecked(s: &str) -> &Self { pub(crate) fn from_str_unchecked(s: &str) -> &Self {
// SAFETY: repr(transparent) thing can be transmuted to/from // SAFETY: repr(transparent) thing can be transmuted to/from
// its inner. // its inner.
unsafe { std::mem::transmute(s) } unsafe { std::mem::transmute(s) }

View file

@ -15,8 +15,8 @@ pub async fn bind<S: AsyncRead + AsyncWrite + Unpin>(
if stream.stream_features.can_bind() { if stream.stream_features.can_bind() {
let resource = stream let resource = stream
.jid .jid
.resource_str() .resource()
.and_then(|resource| Some(resource.to_owned())); .and_then(|resource| Some(resource.to_string()));
let iq = Iq::from_set(BIND_REQ_ID, BindQuery::new(resource)); let iq = Iq::from_set(BIND_REQ_ID, BindQuery::new(resource));
stream.send_stanza(iq).await?; stream.send_stanza(iq).await?;

View file

@ -13,7 +13,7 @@ pub async fn client_login<C: ServerConnector>(
jid: Jid, jid: Jid,
password: String, password: String,
) -> Result<XMPPStream<C::Stream>, Error> { ) -> Result<XMPPStream<C::Stream>, Error> {
let username = jid.node_str().unwrap(); let username = jid.node().unwrap().as_str();
let password = password; let password = password;
let xmpp_stream = server.connect(&jid, ns::JABBER_CLIENT).await?; let xmpp_stream = server.connect(&jid, ns::JABBER_CLIENT).await?;

View file

@ -64,7 +64,7 @@ impl ServerConnector for ServerConfig {
// TCP connection // TCP connection
let tcp_stream = match self { let tcp_stream = match self {
ServerConfig::UseSrv => { ServerConfig::UseSrv => {
connect_with_srv(jid.domain_str(), "_xmpp-client._tcp", 5222).await? connect_with_srv(jid.domain().as_str(), "_xmpp-client._tcp", 5222).await?
} }
ServerConfig::Manual { host, port } => connect_to_host(host.as_str(), *port).await?, ServerConfig::Manual { host, port } => connect_to_host(host.as_str(), *port).await?,
}; };
@ -126,7 +126,7 @@ async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>( async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
xmpp_stream: XMPPStream<S>, xmpp_stream: XMPPStream<S>,
) -> Result<TlsStream<S>, Error> { ) -> Result<TlsStream<S>, Error> {
let domain = xmpp_stream.jid.domain_str().to_owned(); let domain = xmpp_stream.jid.domain().to_string();
let domain = ServerName::try_from(domain.as_str())?; let domain = ServerName::try_from(domain.as_str())?;
let stream = xmpp_stream.into_inner(); let stream = xmpp_stream.into_inner();
let mut root_store = RootCertStore::empty(); let mut root_store = RootCertStore::empty();

View file

@ -15,7 +15,7 @@ pub async fn start<S: AsyncRead + AsyncWrite + Unpin>(
ns: String, ns: String,
) -> Result<XMPPStream<S>, Error> { ) -> Result<XMPPStream<S>, Error> {
let attrs = [ let attrs = [
("to".to_owned(), jid.domain_str().to_owned()), ("to".to_owned(), jid.domain().to_string()),
("version".to_owned(), "1.0".to_owned()), ("version".to_owned(), "1.0".to_owned()),
("xmlns".to_owned(), ns.clone()), ("xmlns".to_owned(), ns.clone()),
("xmlns:stream".to_owned(), ns::STREAM.to_owned()), ("xmlns:stream".to_owned(), ns::STREAM.to_owned()),

View file

@ -39,7 +39,7 @@ pub async fn handle_message_chat<C: ServerConnector>(
Jid::Full(full) => Event::RoomPrivateMessage( Jid::Full(full) => Event::RoomPrivateMessage(
message.id.clone(), message.id.clone(),
full.to_bare(), full.to_bare(),
full.resource_str().to_owned(), full.resource().to_string(),
body.clone(), body.clone(),
time_info.clone(), time_info.clone(),
), ),

View file

@ -21,7 +21,7 @@ pub async fn handle_message_group_chat<C: ServerConnector>(
if let Some((_lang, subject)) = message.get_best_subject(langs.clone()) { if let Some((_lang, subject)) = message.get_best_subject(langs.clone()) {
events.push(Event::RoomSubject( events.push(Event::RoomSubject(
from.to_bare(), from.to_bare(),
from.resource_str().map(String::from), from.resource().map(|x| x.to_string()),
subject.0.clone(), subject.0.clone(),
time_info.clone(), time_info.clone(),
)); ));
@ -32,7 +32,7 @@ pub async fn handle_message_group_chat<C: ServerConnector>(
Jid::Full(full) => Event::RoomMessage( Jid::Full(full) => Event::RoomMessage(
message.id.clone(), message.id.clone(),
from.to_bare(), from.to_bare(),
full.resource_str().to_owned(), full.resource().to_string(),
body.clone(), body.clone(),
time_info, time_info,
), ),