2019-06-10 23:09:20 +02:00
|
|
|
|
// Copyright (c) 2017, 2018 lumi <lumi@pew.im>
|
|
|
|
|
// Copyright (c) 2017, 2018, 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
|
// Copyright (c) 2017, 2018, 2019 Maxime “pep” Buquet <pep@bouah.net>
|
|
|
|
|
// Copyright (c) 2017, 2018 Astro <astro@spaceboyz.net>
|
|
|
|
|
// Copyright (c) 2017 Bastien Orivel <eijebong@bananium.fr>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
2017-02-28 12:38:00 +01:00
|
|
|
|
#![deny(missing_docs)]
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
//! Represents XMPP addresses, also known as JabberIDs (JIDs) for the [XMPP](https://xmpp.org/)
|
|
|
|
|
//! protocol. A [`Jid`] can have between one and three parts in the form `node@domain/resource`:
|
|
|
|
|
//! - the (optional) node part designates a specific account/service on a server, for example
|
|
|
|
|
//! `username@server.com`
|
|
|
|
|
//! - the domain part designates a server, for example `irc.jabberfr.org`
|
|
|
|
|
//! - the (optional) resource part designates a more specific client, such as a participant in a
|
|
|
|
|
//! groupchat (`jabberfr@chat.jabberfr.org/user`) or a specific client device associated with an
|
|
|
|
|
//! account (`user@example.com/dino`)
|
2017-02-27 16:42:09 +01:00
|
|
|
|
//!
|
2023-06-20 20:07:26 +02:00
|
|
|
|
//! The [`Jid`] enum can be one of two variants, containing a more specific type:
|
|
|
|
|
//! - [`BareJid`] (`Jid::Bare` variant): a JID without a resource
|
|
|
|
|
//! - [`FullJid`] (`Jid::Full` variant): a JID with a resource
|
|
|
|
|
//!
|
|
|
|
|
//! Jids as per the XMPP protocol only ever contain valid UTF-8. However, creating any form of Jid
|
|
|
|
|
//! can fail in one of the following cases:
|
|
|
|
|
//! - wrong syntax: creating a Jid with an empty (yet declared) node or resource part, such as
|
|
|
|
|
//! `@example.com` or `user@example.com/`
|
|
|
|
|
//! - stringprep error: some characters were invalid according to the stringprep algorithm, such as
|
|
|
|
|
//! mixing left-to-write and right-to-left characters
|
2017-02-27 15:35:57 +01:00
|
|
|
|
|
2023-06-20 12:49:20 +02:00
|
|
|
|
use core::num::NonZeroU16;
|
|
|
|
|
use std::convert::TryFrom;
|
2018-12-18 16:40:44 +01:00
|
|
|
|
use std::fmt;
|
2017-02-27 15:35:57 +01:00
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
2020-04-30 23:19:06 +02:00
|
|
|
|
#[cfg(feature = "serde")]
|
2020-05-01 02:26:58 +02:00
|
|
|
|
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
2020-04-30 23:19:06 +02:00
|
|
|
|
|
2023-06-19 23:54:08 +02:00
|
|
|
|
mod error;
|
2023-06-20 12:51:37 +02:00
|
|
|
|
pub use crate::error::Error;
|
2019-09-05 18:42:22 +02:00
|
|
|
|
|
2023-06-20 12:49:20 +02:00
|
|
|
|
mod inner;
|
|
|
|
|
use inner::InnerJid;
|
|
|
|
|
|
2023-06-21 13:52:31 +02:00
|
|
|
|
mod parts;
|
|
|
|
|
pub use parts::{DomainPart, NodePart, ResourcePart};
|
|
|
|
|
|
2019-04-22 00:52:02 +02:00
|
|
|
|
/// An enum representing a Jabber ID. It can be either a `FullJid` or a `BareJid`.
|
2020-04-30 23:19:06 +02:00
|
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
2023-05-28 19:54:51 +02:00
|
|
|
|
#[cfg_attr(feature = "serde", serde(untagged))]
|
2019-10-16 01:23:21 +02:00
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-04-22 00:52:02 +02:00
|
|
|
|
pub enum Jid {
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// Contains a [`BareJid`], without a resource part
|
2019-04-22 00:52:02 +02:00
|
|
|
|
Bare(BareJid),
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// Contains a [`FullJid`], with a resource part
|
2019-04-22 00:52:02 +02:00
|
|
|
|
Full(FullJid),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromStr for Jid {
|
2023-06-20 12:51:37 +02:00
|
|
|
|
type Err = Error;
|
2019-04-22 00:52:02 +02:00
|
|
|
|
|
2023-06-20 12:51:37 +02:00
|
|
|
|
fn from_str(s: &str) -> Result<Jid, Error> {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
Jid::new(s)
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-06 14:55:19 +02:00
|
|
|
|
impl From<BareJid> for Jid {
|
|
|
|
|
fn from(bare_jid: BareJid) -> Jid {
|
|
|
|
|
Jid::Bare(bare_jid)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<FullJid> for Jid {
|
|
|
|
|
fn from(full_jid: FullJid) -> Jid {
|
|
|
|
|
Jid::Full(full_jid)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 15:17:51 +02:00
|
|
|
|
impl fmt::Display for Jid {
|
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
2023-06-20 17:37:17 +02:00
|
|
|
|
match self {
|
|
|
|
|
Jid::Bare(bare) => bare.fmt(fmt),
|
|
|
|
|
Jid::Full(full) => full.fmt(fmt),
|
|
|
|
|
}
|
2019-08-31 15:17:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-20 19:01:25 +02:00
|
|
|
|
impl Jid {
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// Constructs a Jabber ID from a string. This is of the form
|
|
|
|
|
/// `node`@`domain`/`resource`, where node and resource parts are optional.
|
2023-06-21 13:52:31 +02:00
|
|
|
|
/// If you want a non-fallible version, use [`Jid::from_parts`] instead.
|
2023-06-20 12:49:20 +02:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use jid::Jid;
|
2023-06-20 12:51:37 +02:00
|
|
|
|
/// # use jid::Error;
|
2023-06-20 12:49:20 +02:00
|
|
|
|
///
|
2023-06-20 12:51:37 +02:00
|
|
|
|
/// # fn main() -> Result<(), Error> {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
/// let jid = Jid::new("node@domain/resource")?;
|
|
|
|
|
///
|
2023-06-21 18:30:25 +02:00
|
|
|
|
/// assert_eq!(jid.node_str(), Some("node"));
|
|
|
|
|
/// assert_eq!(jid.domain_str(), "domain");
|
|
|
|
|
/// assert_eq!(jid.resource_str(), Some("resource"));
|
2023-06-20 12:49:20 +02:00
|
|
|
|
/// # Ok(())
|
|
|
|
|
/// # }
|
|
|
|
|
/// ```
|
2023-06-20 12:51:37 +02:00
|
|
|
|
pub fn new(s: &str) -> Result<Jid, Error> {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
let inner = InnerJid::new(s)?;
|
|
|
|
|
if inner.slash.is_some() {
|
|
|
|
|
Ok(Jid::Full(FullJid { inner }))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Jid::Bare(BareJid { inner }))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-30 14:48:32 +02:00
|
|
|
|
/// Returns the inner String of this JID.
|
|
|
|
|
pub fn into_inner(self) -> String {
|
|
|
|
|
match self {
|
|
|
|
|
Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => inner.normalized,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 13:52:31 +02:00
|
|
|
|
/// Build a [`Jid`] from typed parts. This method cannot fail because it uses parts that have
|
|
|
|
|
/// already been parsed and stringprepped into [`NodePart`], [`DomainPart`], and [`ResourcePart`].
|
|
|
|
|
/// This method allocates and does not consume the typed parts.
|
|
|
|
|
pub fn from_parts(
|
|
|
|
|
node: Option<&NodePart>,
|
|
|
|
|
domain: &DomainPart,
|
|
|
|
|
resource: Option<&ResourcePart>,
|
|
|
|
|
) -> Jid {
|
|
|
|
|
if let Some(resource) = resource {
|
|
|
|
|
Jid::Full(FullJid::from_parts(node, domain, resource))
|
|
|
|
|
} else {
|
|
|
|
|
Jid::Bare(BareJid::from_parts(node, domain))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 18:30:25 +02:00
|
|
|
|
/// The optional node part of the JID, as a [`NodePart`]
|
|
|
|
|
pub fn node(&self) -> Option<NodePart> {
|
|
|
|
|
match self {
|
|
|
|
|
Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => {
|
|
|
|
|
inner.node().map(|s| NodePart::new_unchecked(s))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The optional node part of the JID, as a stringy reference
|
|
|
|
|
pub fn node_str(&self) -> Option<&str> {
|
2019-07-20 19:01:25 +02:00
|
|
|
|
match self {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => inner.node(),
|
2019-07-20 19:01:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 18:30:25 +02:00
|
|
|
|
/// The domain part of the JID, as a [`DomainPart`]
|
|
|
|
|
pub fn domain(&self) -> DomainPart {
|
|
|
|
|
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 {
|
2019-07-20 19:01:25 +02:00
|
|
|
|
match self {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => inner.domain(),
|
2019-07-20 19:01:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 18:30:25 +02:00
|
|
|
|
/// 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(|s| ResourcePart::new_unchecked(s))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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`].
|
|
|
|
|
pub fn resource_str(&self) -> Option<&str> {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
match self {
|
|
|
|
|
Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => inner.resource(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// Allocate a new [`BareJid`] from this JID, discarding the resource.
|
2023-06-20 12:49:20 +02:00
|
|
|
|
pub fn to_bare(&self) -> BareJid {
|
|
|
|
|
match self {
|
|
|
|
|
Jid::Full(jid) => jid.to_bare(),
|
|
|
|
|
Jid::Bare(jid) => jid.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// Transforms this JID into a [`BareJid`], throwing away the resource.
|
2023-06-20 12:49:20 +02:00
|
|
|
|
pub fn into_bare(self) -> BareJid {
|
|
|
|
|
match self {
|
|
|
|
|
Jid::Full(jid) => jid.into_bare(),
|
|
|
|
|
Jid::Bare(jid) => jid,
|
2019-09-08 22:09:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-21 18:30:25 +02:00
|
|
|
|
|
|
|
|
|
/// Checks if the JID contains a [`FullJid`]
|
|
|
|
|
pub fn is_full(&self) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Full(_) => true,
|
|
|
|
|
Self::Bare(_) => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Checks if the JID contains a [`BareJid`]
|
|
|
|
|
pub fn is_bare(&self) -> bool {
|
|
|
|
|
!self.is_full()
|
|
|
|
|
}
|
2019-09-08 22:09:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<Jid> for FullJid {
|
2023-06-20 12:51:37 +02:00
|
|
|
|
type Error = Error;
|
2019-09-08 22:09:25 +02:00
|
|
|
|
|
|
|
|
|
fn try_from(jid: Jid) -> Result<Self, Self::Error> {
|
|
|
|
|
match jid {
|
|
|
|
|
Jid::Full(full) => Ok(full),
|
2023-06-20 17:58:16 +02:00
|
|
|
|
Jid::Bare(_) => Err(Error::ResourceMissingInFullJid),
|
2019-09-08 22:09:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 20:28:32 +01:00
|
|
|
|
impl PartialEq<Jid> for FullJid {
|
|
|
|
|
fn eq(&self, other: &Jid) -> bool {
|
|
|
|
|
match other {
|
|
|
|
|
Jid::Full(full) => self == full,
|
|
|
|
|
Jid::Bare(_) => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PartialEq<Jid> for BareJid {
|
|
|
|
|
fn eq(&self, other: &Jid) -> bool {
|
|
|
|
|
match other {
|
|
|
|
|
Jid::Full(_) => false,
|
|
|
|
|
Jid::Bare(bare) => self == bare,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PartialEq<FullJid> for Jid {
|
|
|
|
|
fn eq(&self, other: &FullJid) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
Jid::Full(full) => full == other,
|
|
|
|
|
Jid::Bare(_) => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PartialEq<BareJid> for Jid {
|
|
|
|
|
fn eq(&self, other: &BareJid) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
Jid::Full(_) => false,
|
|
|
|
|
Jid::Bare(bare) => bare == other,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// A struct representing a full Jabber ID, with a resource part.
|
2017-02-27 15:35:57 +01:00
|
|
|
|
///
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// A full JID is composed of 3 components, of which only the node is optional:
|
2017-02-27 15:35:57 +01:00
|
|
|
|
///
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// - the (optional) node part is the part before the (optional) `@`.
|
|
|
|
|
/// - the domain part is the mandatory part between the (optional) `@` and before the `/`.
|
|
|
|
|
/// - the resource part after the `/`.
|
2019-06-10 22:55:15 +02:00
|
|
|
|
///
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// Unlike a [`BareJid`], it always contains a resource, and should only be used when you are
|
|
|
|
|
/// certain there is no case where a resource can be missing. Otherwise, use a [`Jid`] or
|
|
|
|
|
/// [`BareJid`].
|
2017-05-27 21:45:00 +02:00
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2019-04-22 00:52:02 +02:00
|
|
|
|
pub struct FullJid {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
inner: InnerJid,
|
2017-02-27 15:35:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// A struct representing a bare Jabber ID, without a resource part.
|
2019-04-22 00:52:02 +02:00
|
|
|
|
///
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// A bare JID is composed of 2 components, of which only the node is optional:
|
|
|
|
|
/// - the (optional) node part is the part before the (optional) `@`.
|
|
|
|
|
/// - the domain part is the mandatory part between the (optional) `@` and before the `/`.
|
2019-06-10 22:55:15 +02:00
|
|
|
|
///
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// Unlike a [`FullJid`], it can’t contain a resource, and should only be used when you are certain
|
|
|
|
|
/// there is no case where a resource can be set. Otherwise, use a [`Jid`] or [`FullJid`].
|
2019-04-22 00:52:02 +02:00
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct BareJid {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
inner: InnerJid,
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Debug for FullJid {
|
2017-04-30 22:44:17 +02:00
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
2019-04-22 00:52:02 +02:00
|
|
|
|
write!(fmt, "FullJID({})", self)
|
2017-04-30 22:44:17 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-22 00:52:02 +02:00
|
|
|
|
impl fmt::Debug for BareJid {
|
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
|
write!(fmt, "BareJID({})", self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for FullJid {
|
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
2023-06-20 17:37:17 +02:00
|
|
|
|
fmt.write_str(&self.inner.normalized)
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for BareJid {
|
2017-02-27 15:35:57 +01:00
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
2023-06-20 17:37:17 +02:00
|
|
|
|
fmt.write_str(&self.inner.normalized)
|
2017-02-27 15:35:57 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-01 02:26:58 +02:00
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
|
impl Serialize for FullJid {
|
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
|
where
|
|
|
|
|
S: Serializer,
|
|
|
|
|
{
|
|
|
|
|
serializer.serialize_str(String::from(self).as_str())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
|
impl Serialize for BareJid {
|
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
|
where
|
|
|
|
|
S: Serializer,
|
|
|
|
|
{
|
|
|
|
|
serializer.serialize_str(String::from(self).as_str())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-22 00:52:02 +02:00
|
|
|
|
impl FromStr for FullJid {
|
2023-06-20 12:51:37 +02:00
|
|
|
|
type Err = Error;
|
2019-04-22 00:52:02 +02:00
|
|
|
|
|
2023-06-20 12:51:37 +02:00
|
|
|
|
fn from_str(s: &str) -> Result<FullJid, Error> {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
FullJid::new(s)
|
2017-02-27 15:35:57 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-01 02:26:58 +02:00
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
|
impl<'de> Deserialize<'de> for FullJid {
|
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
|
where
|
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
|
{
|
|
|
|
|
let s = String::deserialize(deserializer)?;
|
|
|
|
|
FullJid::from_str(&s).map_err(de::Error::custom)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
|
impl<'de> Deserialize<'de> for BareJid {
|
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
|
where
|
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
|
{
|
|
|
|
|
let s = String::deserialize(deserializer)?;
|
|
|
|
|
BareJid::from_str(&s).map_err(de::Error::custom)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-22 00:52:02 +02:00
|
|
|
|
impl FullJid {
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// Constructs a full Jabber ID containing all three components. This is of the form
|
|
|
|
|
/// `node@domain/resource`, where node part is optional.
|
2023-06-21 13:52:31 +02:00
|
|
|
|
/// If you want a non-fallible version, use [`FullJid::from_parts`] instead.
|
2017-02-27 15:35:57 +01:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2019-04-22 00:52:02 +02:00
|
|
|
|
/// use jid::FullJid;
|
2023-06-20 12:51:37 +02:00
|
|
|
|
/// # use jid::Error;
|
2017-02-27 15:35:57 +01:00
|
|
|
|
///
|
2023-06-20 12:51:37 +02:00
|
|
|
|
/// # fn main() -> Result<(), Error> {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
/// let jid = FullJid::new("node@domain/resource")?;
|
2017-02-27 15:35:57 +01:00
|
|
|
|
///
|
2023-06-20 12:49:20 +02:00
|
|
|
|
/// assert_eq!(jid.node(), Some("node"));
|
|
|
|
|
/// assert_eq!(jid.domain(), "domain");
|
|
|
|
|
/// assert_eq!(jid.resource(), "resource");
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
/// # }
|
2017-02-27 15:35:57 +01:00
|
|
|
|
/// ```
|
2023-06-20 12:51:37 +02:00
|
|
|
|
pub fn new(s: &str) -> Result<FullJid, Error> {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
let inner = InnerJid::new(s)?;
|
|
|
|
|
if inner.slash.is_some() {
|
|
|
|
|
Ok(FullJid { inner })
|
|
|
|
|
} else {
|
2023-06-20 17:58:16 +02:00
|
|
|
|
Err(Error::ResourceMissingInFullJid)
|
2017-02-27 15:35:57 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-30 14:48:32 +02:00
|
|
|
|
/// Returns the inner String of this JID.
|
|
|
|
|
pub fn into_inner(self) -> String {
|
|
|
|
|
self.inner.normalized
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 13:52:31 +02:00
|
|
|
|
/// Build a [`FullJid`] from typed parts. This method cannot fail because it uses parts that have
|
|
|
|
|
/// already been parsed and stringprepped into [`NodePart`], [`DomainPart`], and [`ResourcePart`].
|
|
|
|
|
/// This method allocates and does not consume the typed parts.
|
|
|
|
|
pub fn from_parts(
|
|
|
|
|
node: Option<&NodePart>,
|
|
|
|
|
domain: &DomainPart,
|
|
|
|
|
resource: &ResourcePart,
|
|
|
|
|
) -> FullJid {
|
|
|
|
|
let (at, slash, normalized) = if let Some(node) = node {
|
|
|
|
|
// Parts are never empty so len > 0 for NonZeroU16::new is always Some
|
|
|
|
|
(
|
|
|
|
|
NonZeroU16::new(node.0.len() as u16),
|
|
|
|
|
NonZeroU16::new((node.0.len() + 1 + domain.0.len()) as u16),
|
|
|
|
|
format!("{}@{}/{}", node.0, domain.0, resource.0),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
(
|
|
|
|
|
None,
|
|
|
|
|
NonZeroU16::new(domain.0.len() as u16),
|
|
|
|
|
format!("{}/{}", domain.0, resource.0),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let inner = InnerJid {
|
|
|
|
|
normalized,
|
|
|
|
|
at,
|
|
|
|
|
slash,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FullJid { inner }
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// The optional node part of the JID.
|
2023-06-20 12:49:20 +02:00
|
|
|
|
pub fn node(&self) -> Option<&str> {
|
|
|
|
|
self.inner.node()
|
2017-02-27 15:35:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// The domain part of the JID.
|
2023-06-20 12:49:20 +02:00
|
|
|
|
pub fn domain(&self) -> &str {
|
|
|
|
|
self.inner.domain()
|
2017-06-12 18:40:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// The optional resource of the Jabber ID. Since this is a full JID it is always present.
|
2023-06-20 12:49:20 +02:00
|
|
|
|
pub fn resource(&self) -> &str {
|
|
|
|
|
self.inner.resource().unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// Allocate a new [`BareJid`] from this full JID, discarding the resource.
|
2023-06-20 12:49:20 +02:00
|
|
|
|
pub fn to_bare(&self) -> BareJid {
|
|
|
|
|
let slash = self.inner.slash.unwrap().get() as usize;
|
|
|
|
|
let normalized = self.inner.normalized[..slash].to_string();
|
|
|
|
|
let inner = InnerJid {
|
|
|
|
|
normalized,
|
|
|
|
|
at: self.inner.at,
|
|
|
|
|
slash: None,
|
|
|
|
|
};
|
|
|
|
|
BareJid { inner }
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// Transforms this full JID into a [`BareJid`], discarding the resource.
|
2023-06-20 12:49:20 +02:00
|
|
|
|
pub fn into_bare(mut self) -> BareJid {
|
|
|
|
|
let slash = self.inner.slash.unwrap().get() as usize;
|
|
|
|
|
self.inner.normalized.truncate(slash);
|
|
|
|
|
self.inner.normalized.shrink_to_fit();
|
|
|
|
|
self.inner.slash = None;
|
|
|
|
|
BareJid { inner: self.inner }
|
2017-02-27 15:35:57 +01:00
|
|
|
|
}
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromStr for BareJid {
|
2023-06-20 12:51:37 +02:00
|
|
|
|
type Err = Error;
|
2019-04-22 00:52:02 +02:00
|
|
|
|
|
2023-06-20 12:51:37 +02:00
|
|
|
|
fn from_str(s: &str) -> Result<BareJid, Error> {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
BareJid::new(s)
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-27 15:35:57 +01:00
|
|
|
|
|
2019-04-22 00:52:02 +02:00
|
|
|
|
impl BareJid {
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// Constructs a bare Jabber ID, containing two components. This is of the form
|
|
|
|
|
/// `node`@`domain`, where node part is optional.
|
2023-06-21 13:52:31 +02:00
|
|
|
|
/// If you want a non-fallible version, use [`BareJid::from_parts`] instead.
|
2017-06-12 18:40:39 +02:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2019-04-22 00:52:02 +02:00
|
|
|
|
/// use jid::BareJid;
|
2023-06-20 12:51:37 +02:00
|
|
|
|
/// # use jid::Error;
|
2017-06-12 18:40:39 +02:00
|
|
|
|
///
|
2023-06-20 12:51:37 +02:00
|
|
|
|
/// # fn main() -> Result<(), Error> {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
/// let jid = BareJid::new("node@domain")?;
|
2017-06-12 18:40:39 +02:00
|
|
|
|
///
|
2023-06-20 12:49:20 +02:00
|
|
|
|
/// assert_eq!(jid.node(), Some("node"));
|
|
|
|
|
/// assert_eq!(jid.domain(), "domain");
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
/// # }
|
2017-06-12 19:20:42 +02:00
|
|
|
|
/// ```
|
2023-06-20 12:51:37 +02:00
|
|
|
|
pub fn new(s: &str) -> Result<BareJid, Error> {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
let inner = InnerJid::new(s)?;
|
|
|
|
|
if inner.slash.is_none() {
|
|
|
|
|
Ok(BareJid { inner })
|
|
|
|
|
} else {
|
2023-06-20 12:51:37 +02:00
|
|
|
|
Err(Error::ResourceInBareJid)
|
2017-02-27 15:35:57 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-30 14:48:32 +02:00
|
|
|
|
/// Returns the inner String of this JID.
|
|
|
|
|
pub fn into_inner(self) -> String {
|
|
|
|
|
self.inner.normalized
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 13:52:31 +02:00
|
|
|
|
/// 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
|
|
|
|
|
/// and does not consume the typed parts.
|
|
|
|
|
pub fn from_parts(node: Option<&NodePart>, domain: &DomainPart) -> BareJid {
|
|
|
|
|
let (at, normalized) = if let Some(node) = node {
|
|
|
|
|
// Parts are never empty so len > 0 for NonZeroU16::new is always Some
|
|
|
|
|
(
|
|
|
|
|
NonZeroU16::new(node.0.len() as u16),
|
|
|
|
|
format!("{}@{}", node.0, domain.0),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
(None, domain.0.clone())
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let inner = InnerJid {
|
|
|
|
|
normalized,
|
|
|
|
|
at,
|
|
|
|
|
slash: None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BareJid { inner }
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// The optional node part of the JID.
|
2023-06-20 12:49:20 +02:00
|
|
|
|
pub fn node(&self) -> Option<&str> {
|
|
|
|
|
self.inner.node()
|
2017-02-27 15:35:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 20:07:26 +02:00
|
|
|
|
/// The domain part of the JID.
|
2023-06-20 12:49:20 +02:00
|
|
|
|
pub fn domain(&self) -> &str {
|
|
|
|
|
self.inner.domain()
|
2017-02-27 15:35:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 18:30:25 +02:00
|
|
|
|
/// Constructs a [`BareJid`] from the bare JID, by specifying a [`ResourcePart`].
|
|
|
|
|
/// If you'd like to specify a stringy resource, use [`BareJid::with_resource_str`] instead.
|
2017-02-27 15:35:57 +01:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2023-06-21 18:30:25 +02:00
|
|
|
|
/// use jid::{BareJid, ResourcePart};
|
2017-02-27 15:35:57 +01:00
|
|
|
|
///
|
2023-06-21 18:30:25 +02:00
|
|
|
|
/// let resource = ResourcePart::new("resource").unwrap();
|
2023-06-20 12:49:20 +02:00
|
|
|
|
/// let bare = BareJid::new("node@domain").unwrap();
|
2023-06-21 18:30:25 +02:00
|
|
|
|
/// let full = bare.with_resource(&resource);
|
2017-02-27 15:35:57 +01:00
|
|
|
|
///
|
2023-06-20 12:49:20 +02:00
|
|
|
|
/// assert_eq!(full.node(), Some("node"));
|
|
|
|
|
/// assert_eq!(full.domain(), "domain");
|
|
|
|
|
/// assert_eq!(full.resource(), "resource");
|
2017-02-27 15:35:57 +01:00
|
|
|
|
/// ```
|
2023-06-21 18:30:25 +02:00
|
|
|
|
pub fn with_resource(&self, resource: &ResourcePart) -> FullJid {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
let slash = NonZeroU16::new(self.inner.normalized.len() as u16);
|
|
|
|
|
let normalized = format!("{}/{resource}", self.inner.normalized);
|
|
|
|
|
let inner = InnerJid {
|
|
|
|
|
normalized,
|
|
|
|
|
at: self.inner.at,
|
|
|
|
|
slash,
|
|
|
|
|
};
|
2023-06-21 18:30:25 +02:00
|
|
|
|
|
|
|
|
|
FullJid { inner }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Constructs a [`FullJid`] from the bare JID, by specifying a stringy `resource`.
|
|
|
|
|
/// If your resource has already been parsed into a [`ResourcePart`], use [`BareJid::with_resource`].
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use jid::BareJid;
|
|
|
|
|
///
|
|
|
|
|
/// let bare = BareJid::new("node@domain").unwrap();
|
|
|
|
|
/// let full = bare.with_resource_str("resource").unwrap();
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(full.node(), Some("node"));
|
|
|
|
|
/// assert_eq!(full.domain(), "domain");
|
|
|
|
|
/// assert_eq!(full.resource(), "resource");
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn with_resource_str(&self, resource: &str) -> Result<FullJid, Error> {
|
|
|
|
|
let resource = ResourcePart::new(resource)?;
|
|
|
|
|
Ok(self.with_resource(&resource))
|
2017-02-27 15:35:57 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-29 06:11:30 +02:00
|
|
|
|
#[cfg(feature = "minidom")]
|
2019-07-20 19:45:06 +02:00
|
|
|
|
use minidom::{IntoAttributeValue, Node};
|
2017-07-29 06:11:30 +02:00
|
|
|
|
|
|
|
|
|
#[cfg(feature = "minidom")]
|
|
|
|
|
impl IntoAttributeValue for Jid {
|
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
2023-06-20 17:37:17 +02:00
|
|
|
|
Some(format!("{}", self))
|
2017-07-29 06:11:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 21:24:41 +01:00
|
|
|
|
#[cfg(feature = "minidom")]
|
2021-10-11 15:35:27 +02:00
|
|
|
|
impl From<Jid> for Node {
|
|
|
|
|
fn from(jid: Jid) -> Node {
|
2023-06-20 17:37:17 +02:00
|
|
|
|
Node::Text(format!("{}", jid))
|
2017-10-31 21:24:41 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-22 00:52:02 +02:00
|
|
|
|
#[cfg(feature = "minidom")]
|
|
|
|
|
impl IntoAttributeValue for FullJid {
|
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
2023-06-20 17:37:17 +02:00
|
|
|
|
Some(format!("{}", self))
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "minidom")]
|
2021-10-11 15:35:27 +02:00
|
|
|
|
impl From<FullJid> for Node {
|
|
|
|
|
fn from(jid: FullJid) -> Node {
|
2023-06-20 17:37:17 +02:00
|
|
|
|
Node::Text(format!("{}", jid))
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "minidom")]
|
|
|
|
|
impl IntoAttributeValue for BareJid {
|
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
2023-06-20 17:37:17 +02:00
|
|
|
|
Some(format!("{}", self))
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "minidom")]
|
2021-10-11 15:35:27 +02:00
|
|
|
|
impl From<BareJid> for Node {
|
|
|
|
|
fn from(jid: BareJid) -> Node {
|
2023-06-20 17:37:17 +02:00
|
|
|
|
Node::Text(format!("{}", jid))
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 15:35:57 +01:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
2019-10-16 01:23:21 +02:00
|
|
|
|
use std::collections::HashMap;
|
2017-02-27 15:35:57 +01:00
|
|
|
|
|
2023-06-19 23:25:48 +02:00
|
|
|
|
macro_rules! assert_size (
|
|
|
|
|
($t:ty, $sz:expr) => (
|
|
|
|
|
assert_eq!(::std::mem::size_of::<$t>(), $sz);
|
|
|
|
|
);
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
assert_size!(BareJid, 16);
|
|
|
|
|
assert_size!(FullJid, 16);
|
|
|
|
|
assert_size!(Jid, 20);
|
2023-06-19 23:25:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
assert_size!(BareJid, 32);
|
|
|
|
|
assert_size!(FullJid, 32);
|
|
|
|
|
assert_size!(Jid, 40);
|
2023-06-19 23:25:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 15:35:57 +01:00
|
|
|
|
#[test]
|
2019-04-22 00:52:02 +02:00
|
|
|
|
fn can_parse_full_jids() {
|
2019-06-10 22:04:03 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
FullJid::from_str("a@b.c/d"),
|
2023-06-20 12:49:20 +02:00
|
|
|
|
Ok(FullJid::new("a@b.c/d").unwrap())
|
2019-06-10 22:04:03 +02:00
|
|
|
|
);
|
2019-04-22 00:52:02 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
FullJid::from_str("b.c/d"),
|
2023-06-20 12:49:20 +02:00
|
|
|
|
Ok(FullJid::new("b.c/d").unwrap())
|
2019-04-22 00:52:02 +02:00
|
|
|
|
);
|
|
|
|
|
|
2023-06-20 17:58:16 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
FullJid::from_str("a@b.c"),
|
|
|
|
|
Err(Error::ResourceMissingInFullJid)
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
FullJid::from_str("b.c"),
|
|
|
|
|
Err(Error::ResourceMissingInFullJid)
|
|
|
|
|
);
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn can_parse_bare_jids() {
|
|
|
|
|
assert_eq!(
|
2023-06-20 12:49:20 +02:00
|
|
|
|
BareJid::from_str("a@b.c"),
|
|
|
|
|
Ok(BareJid::new("a@b.c").unwrap())
|
2018-12-18 16:40:44 +01:00
|
|
|
|
);
|
2023-06-20 12:49:20 +02:00
|
|
|
|
assert_eq!(BareJid::from_str("b.c"), Ok(BareJid::new("b.c").unwrap()));
|
2017-02-27 15:35:57 +01:00
|
|
|
|
}
|
2017-04-23 15:49:00 +02:00
|
|
|
|
|
2019-04-22 00:52:02 +02:00
|
|
|
|
#[test]
|
|
|
|
|
fn can_parse_jids() {
|
|
|
|
|
let full = FullJid::from_str("a@b.c/d").unwrap();
|
|
|
|
|
let bare = BareJid::from_str("e@f.g").unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(Jid::from_str("a@b.c/d"), Ok(Jid::Full(full)));
|
|
|
|
|
assert_eq!(Jid::from_str("e@f.g"), Ok(Jid::Bare(bare)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn full_to_bare_jid() {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
let bare: BareJid = FullJid::new("a@b.c/d").unwrap().to_bare();
|
|
|
|
|
assert_eq!(bare, BareJid::new("a@b.c").unwrap());
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2023-06-21 18:30:25 +02:00
|
|
|
|
fn bare_to_full_jid_str() {
|
2019-06-10 22:04:03 +02:00
|
|
|
|
assert_eq!(
|
2023-06-21 18:30:25 +02:00
|
|
|
|
BareJid::new("a@b.c")
|
|
|
|
|
.unwrap()
|
|
|
|
|
.with_resource_str("d")
|
|
|
|
|
.unwrap(),
|
2023-06-20 12:49:20 +02:00
|
|
|
|
FullJid::new("a@b.c/d").unwrap()
|
2019-06-10 22:04:03 +02:00
|
|
|
|
);
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-20 19:01:25 +02:00
|
|
|
|
#[test]
|
2023-06-21 18:30:25 +02:00
|
|
|
|
fn bare_to_full_jid() {
|
2019-07-20 19:01:25 +02:00
|
|
|
|
assert_eq!(
|
2023-06-21 18:30:25 +02:00
|
|
|
|
BareJid::new("a@b.c")
|
|
|
|
|
.unwrap()
|
|
|
|
|
.with_resource(&ResourcePart::new("d").unwrap()),
|
|
|
|
|
FullJid::new("a@b.c/d").unwrap()
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn node_from_jid() {
|
|
|
|
|
let jid = Jid::new("a@b.c/d").unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(jid.node_str(), Some("a"),);
|
|
|
|
|
|
|
|
|
|
assert_eq!(jid.node(), Some(NodePart::new("a").unwrap()));
|
2019-07-20 19:01:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn domain_from_jid() {
|
2023-06-21 18:30:25 +02:00
|
|
|
|
let jid = Jid::new("a@b.c").unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(jid.domain_str(), "b.c");
|
|
|
|
|
|
|
|
|
|
assert_eq!(jid.domain(), DomainPart::new("b.c").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn resource_from_jid() {
|
|
|
|
|
let jid = Jid::new("a@b.c/d").unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(jid.resource_str(), Some("d"),);
|
|
|
|
|
|
|
|
|
|
assert_eq!(jid.resource(), Some(ResourcePart::new("d").unwrap()));
|
2019-07-20 19:01:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-08 22:09:25 +02:00
|
|
|
|
#[test]
|
|
|
|
|
fn jid_to_full_bare() {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
let full = FullJid::new("a@b.c/d").unwrap();
|
|
|
|
|
let bare = BareJid::new("a@b.c").unwrap();
|
2019-09-08 22:09:25 +02:00
|
|
|
|
|
2023-06-20 12:49:20 +02:00
|
|
|
|
assert_eq!(FullJid::try_from(Jid::Full(full.clone())), Ok(full.clone()));
|
2019-09-08 22:09:25 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
FullJid::try_from(Jid::Bare(bare.clone())),
|
2023-06-20 17:58:16 +02:00
|
|
|
|
Err(Error::ResourceMissingInFullJid),
|
2019-09-08 22:09:25 +02:00
|
|
|
|
);
|
2023-06-20 12:49:20 +02:00
|
|
|
|
assert_eq!(Jid::Bare(full.clone().to_bare()), bare.clone());
|
|
|
|
|
assert_eq!(Jid::Bare(bare.clone()), bare);
|
2019-09-08 22:09:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-23 15:49:00 +02:00
|
|
|
|
#[test]
|
|
|
|
|
fn serialise() {
|
2018-12-18 16:40:44 +01:00
|
|
|
|
assert_eq!(
|
2023-06-20 17:37:17 +02:00
|
|
|
|
format!("{}", FullJid::new("a@b/c").unwrap()),
|
2018-12-18 16:40:44 +01:00
|
|
|
|
String::from("a@b/c")
|
|
|
|
|
);
|
2023-06-20 12:49:20 +02:00
|
|
|
|
assert_eq!(
|
2023-06-20 17:37:17 +02:00
|
|
|
|
format!("{}", BareJid::new("a@b").unwrap()),
|
2023-06-20 12:49:20 +02:00
|
|
|
|
String::from("a@b")
|
|
|
|
|
);
|
2017-04-23 15:49:00 +02:00
|
|
|
|
}
|
2017-07-29 06:11:30 +02:00
|
|
|
|
|
2019-10-16 01:23:21 +02:00
|
|
|
|
#[test]
|
|
|
|
|
fn hash() {
|
|
|
|
|
let _map: HashMap<Jid, String> = HashMap::new();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 21:36:36 +01:00
|
|
|
|
#[test]
|
2018-03-01 16:26:44 +01:00
|
|
|
|
fn invalid_jids() {
|
2023-06-20 17:58:16 +02:00
|
|
|
|
assert_eq!(BareJid::from_str(""), Err(Error::DomainEmpty));
|
|
|
|
|
assert_eq!(BareJid::from_str("/c"), Err(Error::DomainEmpty));
|
|
|
|
|
assert_eq!(BareJid::from_str("a@/c"), Err(Error::DomainEmpty));
|
|
|
|
|
assert_eq!(BareJid::from_str("@b"), Err(Error::NodeEmpty));
|
|
|
|
|
assert_eq!(BareJid::from_str("b/"), Err(Error::ResourceEmpty));
|
|
|
|
|
|
|
|
|
|
assert_eq!(FullJid::from_str(""), Err(Error::DomainEmpty));
|
|
|
|
|
assert_eq!(FullJid::from_str("/c"), Err(Error::DomainEmpty));
|
|
|
|
|
assert_eq!(FullJid::from_str("a@/c"), Err(Error::DomainEmpty));
|
|
|
|
|
assert_eq!(FullJid::from_str("@b"), Err(Error::NodeEmpty));
|
|
|
|
|
assert_eq!(FullJid::from_str("b/"), Err(Error::ResourceEmpty));
|
|
|
|
|
assert_eq!(
|
|
|
|
|
FullJid::from_str("a@b"),
|
|
|
|
|
Err(Error::ResourceMissingInFullJid)
|
|
|
|
|
);
|
2018-02-18 21:36:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 15:17:51 +02:00
|
|
|
|
#[test]
|
|
|
|
|
fn display_jids() {
|
2019-10-23 01:32:41 +02:00
|
|
|
|
assert_eq!(
|
2023-06-20 12:49:20 +02:00
|
|
|
|
format!("{}", FullJid::new("a@b/c").unwrap()),
|
2019-10-23 01:32:41 +02:00
|
|
|
|
String::from("a@b/c")
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2023-06-20 12:49:20 +02:00
|
|
|
|
format!("{}", BareJid::new("a@b").unwrap()),
|
|
|
|
|
String::from("a@b")
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
format!("{}", Jid::Full(FullJid::new("a@b/c").unwrap())),
|
2019-10-23 01:32:41 +02:00
|
|
|
|
String::from("a@b/c")
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2023-06-20 12:49:20 +02:00
|
|
|
|
format!("{}", Jid::Bare(BareJid::new("a@b").unwrap())),
|
2019-10-23 01:32:41 +02:00
|
|
|
|
String::from("a@b")
|
|
|
|
|
);
|
2019-08-31 15:17:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-29 06:11:30 +02:00
|
|
|
|
#[cfg(feature = "minidom")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn minidom() {
|
2020-04-02 23:11:58 +02:00
|
|
|
|
let elem: minidom::Element = "<message xmlns='ns1' from='a@b/c'/>".parse().unwrap();
|
2017-07-29 06:11:30 +02:00
|
|
|
|
let to: Jid = elem.attr("from").unwrap().parse().unwrap();
|
2023-06-20 12:49:20 +02:00
|
|
|
|
assert_eq!(to, Jid::Full(FullJid::new("a@b/c").unwrap()));
|
2019-04-22 00:52:02 +02:00
|
|
|
|
|
2020-04-02 23:11:58 +02:00
|
|
|
|
let elem: minidom::Element = "<message xmlns='ns1' from='a@b'/>".parse().unwrap();
|
2019-04-22 00:52:02 +02:00
|
|
|
|
let to: Jid = elem.attr("from").unwrap().parse().unwrap();
|
2023-06-20 12:49:20 +02:00
|
|
|
|
assert_eq!(to, Jid::Bare(BareJid::new("a@b").unwrap()));
|
2019-04-22 00:52:02 +02:00
|
|
|
|
|
2020-04-02 23:11:58 +02:00
|
|
|
|
let elem: minidom::Element = "<message xmlns='ns1' from='a@b/c'/>".parse().unwrap();
|
2019-04-22 00:52:02 +02:00
|
|
|
|
let to: FullJid = elem.attr("from").unwrap().parse().unwrap();
|
2023-06-20 12:49:20 +02:00
|
|
|
|
assert_eq!(to, FullJid::new("a@b/c").unwrap());
|
2019-04-22 00:52:02 +02:00
|
|
|
|
|
2020-04-02 23:11:58 +02:00
|
|
|
|
let elem: minidom::Element = "<message xmlns='ns1' from='a@b'/>".parse().unwrap();
|
2019-04-22 00:52:02 +02:00
|
|
|
|
let to: BareJid = elem.attr("from").unwrap().parse().unwrap();
|
2023-06-20 12:49:20 +02:00
|
|
|
|
assert_eq!(to, BareJid::new("a@b").unwrap());
|
2019-04-22 00:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "minidom")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn minidom_into_attr() {
|
2023-06-20 12:49:20 +02:00
|
|
|
|
let full = FullJid::new("a@b/c").unwrap();
|
2020-04-02 23:11:58 +02:00
|
|
|
|
let elem = minidom::Element::builder("message", "jabber:client")
|
2019-04-22 00:52:02 +02:00
|
|
|
|
.attr("from", full.clone())
|
|
|
|
|
.build();
|
2023-06-20 17:37:17 +02:00
|
|
|
|
assert_eq!(elem.attr("from"), Some(format!("{}", full).as_str()));
|
2019-04-22 00:52:02 +02:00
|
|
|
|
|
2023-06-20 12:49:20 +02:00
|
|
|
|
let bare = BareJid::new("a@b").unwrap();
|
2020-04-02 23:11:58 +02:00
|
|
|
|
let elem = minidom::Element::builder("message", "jabber:client")
|
2019-04-22 00:52:02 +02:00
|
|
|
|
.attr("from", bare.clone())
|
|
|
|
|
.build();
|
2023-06-20 17:37:17 +02:00
|
|
|
|
assert_eq!(elem.attr("from"), Some(format!("{}", bare).as_str()));
|
2019-04-22 00:52:02 +02:00
|
|
|
|
|
|
|
|
|
let jid = Jid::Bare(bare.clone());
|
2020-04-02 23:11:58 +02:00
|
|
|
|
let _elem = minidom::Element::builder("message", "jabber:client")
|
2019-04-22 00:52:02 +02:00
|
|
|
|
.attr("from", jid)
|
|
|
|
|
.build();
|
2023-06-20 17:37:17 +02:00
|
|
|
|
assert_eq!(elem.attr("from"), Some(format!("{}", bare).as_str()));
|
2017-07-29 06:11:30 +02:00
|
|
|
|
}
|
2019-12-02 02:57:20 +01:00
|
|
|
|
|
|
|
|
|
#[test]
|
2023-06-19 23:39:21 +02:00
|
|
|
|
fn stringprep() {
|
2019-12-02 02:57:20 +01:00
|
|
|
|
let full = FullJid::from_str("Test@☃.coM/Test™").unwrap();
|
2023-06-20 12:49:20 +02:00
|
|
|
|
let equiv = FullJid::new("test@☃.com/TestTM").unwrap();
|
2019-12-02 02:57:20 +01:00
|
|
|
|
assert_eq!(full, equiv);
|
|
|
|
|
}
|
2023-06-21 18:30:25 +02:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn jid_from_parts() {
|
|
|
|
|
let node = NodePart::new("node").unwrap();
|
|
|
|
|
let domain = DomainPart::new("domain").unwrap();
|
|
|
|
|
let resource = ResourcePart::new("resource").unwrap();
|
|
|
|
|
|
|
|
|
|
let jid = Jid::from_parts(Some(&node), &domain, Some(&resource));
|
|
|
|
|
assert_eq!(jid, Jid::new("node@domain/resource").unwrap());
|
|
|
|
|
|
|
|
|
|
let barejid = BareJid::from_parts(Some(&node), &domain);
|
|
|
|
|
assert_eq!(barejid, BareJid::new("node@domain").unwrap());
|
|
|
|
|
|
|
|
|
|
let fulljid = FullJid::from_parts(Some(&node), &domain, &resource);
|
|
|
|
|
assert_eq!(fulljid, FullJid::new("node@domain/resource").unwrap());
|
|
|
|
|
}
|
2017-02-27 15:35:57 +01:00
|
|
|
|
}
|