Struct Jid

Source
pub struct Jid<'a> { /* private fields */ }
Expand description

A parsed JID.

Implementations§

Source§

impl<'a> Jid<'a>

Source

pub fn split(s: &'a str) -> Result<(Option<&'a str>, &'a str, Option<&'a str>)>

Splits a JID formatted as a string into its localpart, domainpart, and resourcepart. The localpart and resourcepart are optional, but the domainpart is always returned.

§Errors

This function performs a “naive” string split and does not perform any validation of the individual parts other than to make sure that required parts exist. For example “@example.com” will return an error (EmptyLocal), but “%@example.com” will not (even though “%” is not a valid localpart). The length of localparts and resourceparts is also not checked (other than if they’re empty). This is because when creating an actual JID it is possible for certain Unicode characters to be canonicalized into a shorter length encoding, meaning that a part that was previously too long may suddenly fit in the maximum length. ShortDomain may be returned because we know that domains will never become longer after performing IDNA2008 operations, but LongDomain may not be returned for the same reasons as mentioned above.

Possible errors include:

§Examples

Basic usage:

let (lp, dp, rp) = Jid::split("feste@example.net")?;
assert_eq!(lp, Some("feste"));
assert_eq!(dp, "example.net");
assert_eq!(rp, None);
Source

pub fn new<L, R>(local: L, domain: &'a str, resource: R) -> Result<Jid<'a>>
where L: Into<Option<&'a str>>, R: Into<Option<&'a str>>,

Constructs a JID from its constituent parts. The localpart is generally the username of a user on a particular server, the domainpart is a domain, hostname, or IP address where the user or entity resides, and the resourcepart identifies a specific client. Everything but the domain is optional.

§Errors

If the localpart or resourcepart passed to this function is not valid, or the domainpart fails IDNA processing or is not a valid IPv6 address, this function returns an error variant.

§Examples

Basic usage:

let j = Jid::new("feste", "example.net", None)?;
assert_eq!(j, "feste@example.net");
Source

pub fn from_domain(domain: &'a str) -> Result<Jid<'a>>

Construct a JID containing only a domain part.

§Errors

If domain fails the IDNA “to Unicode” operation, or is enclosed in square brackets (“[]”) but is not a valid IPv6 address, this function returns an error variant.

§Examples

Basic usage:

let j = Jid::from_domain("example.net")?;
assert_eq!(j, "example.net");
Source

pub fn bare(self) -> Jid<'a>

Consumes a JID to construct a bare JID (a JID without a resourcepart).

§Examples

Basic usage:

let j = Jid::new("feste", "example.net", "res")?;
assert_eq!(j.bare(), "feste@example.net");
Source

pub fn domain(self) -> Jid<'a>

Consumes a JID to construct a JID with only the domainpart.

§Examples

Basic usage:

let j = Jid::new("feste", "example.net", "res")?;
assert_eq!(j.domain(), "example.net");
Source

pub fn with_local<T: Into<Option<&'a str>>>(self, local: T) -> Result<Jid<'a>>

Consumes a JID to construct a new JID with the given localpart.

§Errors

If the localpart is too long Error::LongLocal is returned.

§Examples

Basic usage:

let j = Jid::from_str("example.net")?;
assert_eq!(j.with_local("feste")?, "feste@example.net");

let j = Jid::from_str("iago@example.net")?;
assert_eq!(j.with_local(None)?, "example.net");

let j = Jid::from_str("feste@example.net")?;
assert!(j.with_local("").is_err());
Source

pub fn with_domain(self, domain: &'a str) -> Result<Jid<'a>>

Consumes a JID to construct a new JID with the given domainpart.

§Errors

If the domain is too long, too short, or fails IDNA processing, an error variant is returned.

§Examples

Basic usage:

let j = Jid::from_str("feste@example.net")?;
assert_eq!(j.with_domain("example.org")?, "feste@example.org");
Source

pub fn with_resource<T: Into<Option<&'a str>>>( self, resource: T, ) -> Result<Jid<'a>>

Consumes a JID to construct a new JID with the given resourcepart.

§Errors

If the resource is too long Error::LongResource is returned.

§Examples

Basic usage:

let j = Jid::from_str("feste@example.net")?;
assert_eq!(j.with_resource("1234")?, "feste@example.net/1234");

let j = Jid::from_str("feste@example.net/1234")?;
assert_eq!(j.with_resource(None)?, "feste@example.net");

let j = Jid::from_str("feste@example.net")?;
assert!(j.with_resource("").is_err());
Source

pub fn from_str(s: &'a str) -> Result<Jid<'a>>

Parse a string to create a Jid.

This does not implement the FromStr trait because the Jid type requires an explicit lifetime annotation and the from_str method of FromStr uses an implicit annotation which is not compatible with the Jid type.

§Errors

If the entire string or any part of the JID is empty or not valid, or the domainpart fails IDNA processing or is not a valid IPv6 address, this function returns an error variant.

§Examples
let j = Jid::from_str("juliet@example.net/balcony")?;
assert_eq!(j, "juliet@example.net/balcony");
Source

pub fn localpart(&self) -> Option<&str>

Returns the localpart of the JID in canonical form.

§Examples
let j = Jid::from_str("mercutio@example.net/rp")?;
assert_eq!(j.localpart(), Some("mercutio"));

let j = Jid::from_str("example.net/rp")?;
assert!(j.localpart().is_none());
Source

pub fn domainpart(&self) -> &str

Returns the domainpart of the JID in canonical form.

§Examples
let j = Jid::from_str("mercutio@example.net/rp")?;
assert_eq!(j.domainpart(), "example.net");
Source

pub fn resourcepart(&self) -> Option<&str>

Returns the resourcepart of the JID in canonical form.

§Examples
let j = Jid::from_str("example.net/rp")?;
assert_eq!(j.resourcepart(), Some("rp"));

let j = Jid::from_str("feste@example.net")?;
assert!(j.resourcepart().is_none());
Source

pub unsafe fn new_unchecked<L, R>( local: L, domain: &'a str, resource: R, ) -> Jid<'a>
where L: Into<Option<&'a str>>, R: Into<Option<&'a str>>,

Constructs a JID from its constituent parts, bypassing safety checks. A None value for the localpart or resourcepart indicates that there is no localpart or resourcepart. A value of Some("") (although note that the Some() wrapper may be elided) indicates that the localpart or resourcepart is empty, which is invalid, but allowed by this unsafe function (eg. @example.com).

§Examples

Constructing an invalid JID:

unsafe {
    let j = Jid::new_unchecked(r#"/o\"#, "[badip]", None);
    assert_eq!(j.localpart(), Some(r#"/o\"#));
    assert_eq!(j.domainpart(), "[badip]");
    assert_eq!(j.resourcepart(), None);

    // Note that comparisons you would expect to work may fail when creating unsafe JIDs.
    assert_ne!(j, r#"/o\@[badip]"#);

    let j = Jid::new_unchecked("", "example.com", "");
    assert_eq!(j, "@example.com/");
}

Trait Implementations§

Source§

impl<'a> Clone for Jid<'a>

Source§

fn clone(&self) -> Jid<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Jid<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Jid<'_>

Format the JID in its canonical string form.

§Examples

Formatting and printing:

let j = Jid::from_str("viola@example.net")?;

assert_eq!(format!("{}", j), "viola@example.net");
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<IpAddr> for Jid<'a>

Creates a JID from an IP address.

Source§

fn from(addr: IpAddr) -> Jid<'a>

Converts to this type from the input type.
Source§

impl<'a> From<Ipv4Addr> for Jid<'a>

Creates a JID from an IPv4 address.

Source§

fn from(addr: Ipv4Addr) -> Jid<'a>

Converts to this type from the input type.
Source§

impl<'a> From<Ipv6Addr> for Jid<'a>

Creates a JID from an IPv6 address.

Source§

fn from(addr: Ipv6Addr) -> Jid<'a>

Converts to this type from the input type.
Source§

impl<'a> Ord for Jid<'a>

Source§

fn cmp(&self, other: &Jid<'a>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a, 'b> PartialEq<&'b str> for Jid<'a>

Allows JIDs to be compared with strings.

This may be expensive. The string is first split using Jid::split and each part is compared with its corresponding part in the JID. If this comparison is successful, true is returned and the comparison is cheap. If this does not match, however, the string is then canonicalized (by converting it into a JID) and the parts are compared again; this may require expensive heap allocations. If constructing a JID from the string fails, the comparison always fails (even if the original JID is would match the invalid output). Comparisons involving unsafe JIDs constructed with Jid::new_unchecked should construct an unsafe JID from the string and manually compare the parts.

Source§

fn eq(&self, other: &&'b str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b> PartialEq<Cow<'b, str>> for Jid<'a>

Allows JIDs to be compared with strings.

This may be expensive. The string is first split using Jid::split and each part is compared with its corresponding part in the JID. If this comparison is successful, true is returned and the comparison is cheap. If this does not match, however, the string is then canonicalized (by converting it into a JID) and the parts are compared again; this may require expensive heap allocations. If constructing a JID from the string fails, the comparison always fails (even if the original JID is would match the invalid output). Comparisons involving unsafe JIDs constructed with Jid::new_unchecked should construct an unsafe JID from the string and manually compare the parts.

Source§

fn eq(&self, other: &Cow<'b, str>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<Jid<'_>> for str

Allows JIDs to be compared with strings.

This may be expensive. The string is first split using Jid::split and each part is compared with its corresponding part in the JID. If this comparison is successful, true is returned and the comparison is cheap. If this does not match, however, the string is then canonicalized (by converting it into a JID) and the parts are compared again; this may require expensive heap allocations. If constructing a JID from the string fails, the comparison always fails (even if the original JID is would match the invalid output). Comparisons involving unsafe JIDs constructed with Jid::new_unchecked should construct an unsafe JID from the string and manually compare the parts.

§Examples

let j = Jid::from_str("example.net/rp")?;
assert!("example.net/rp" == j);
Source§

fn eq(&self, other: &Jid<'_>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b> PartialEq<Jid<'a>> for &'b str

Allows JIDs to be compared with strings.

This may be expensive. The string is first split using Jid::split and each part is compared with its corresponding part in the JID. If this comparison is successful, true is returned and the comparison is cheap. If this does not match, however, the string is then canonicalized (by converting it into a JID) and the parts are compared again; this may require expensive heap allocations. If constructing a JID from the string fails, the comparison always fails (even if the original JID is would match the invalid output). Comparisons involving unsafe JIDs constructed with Jid::new_unchecked should construct an unsafe JID from the string and manually compare the parts.

Source§

fn eq(&self, other: &Jid<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b> PartialEq<Jid<'a>> for Cow<'b, str>

Allows JIDs to be compared with strings.

This may be expensive. The string is first split using Jid::split and each part is compared with its corresponding part in the JID. If this comparison is successful, true is returned and the comparison is cheap. If this does not match, however, the string is then canonicalized (by converting it into a JID) and the parts are compared again; this may require expensive heap allocations. If constructing a JID from the string fails, the comparison always fails (even if the original JID is would match the invalid output). Comparisons involving unsafe JIDs constructed with Jid::new_unchecked should construct an unsafe JID from the string and manually compare the parts.

Source§

fn eq(&self, other: &Jid<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b> PartialEq<Jid<'a>> for String

Allows JIDs to be compared with strings.

This may be expensive. The string is first split using Jid::split and each part is compared with its corresponding part in the JID. If this comparison is successful, true is returned and the comparison is cheap. If this does not match, however, the string is then canonicalized (by converting it into a JID) and the parts are compared again; this may require expensive heap allocations. If constructing a JID from the string fails, the comparison always fails (even if the original JID is would match the invalid output). Comparisons involving unsafe JIDs constructed with Jid::new_unchecked should construct an unsafe JID from the string and manually compare the parts.

Source§

fn eq(&self, other: &Jid<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b> PartialEq<String> for Jid<'a>

Allows JIDs to be compared with strings.

This may be expensive. The string is first split using Jid::split and each part is compared with its corresponding part in the JID. If this comparison is successful, true is returned and the comparison is cheap. If this does not match, however, the string is then canonicalized (by converting it into a JID) and the parts are compared again; this may require expensive heap allocations. If constructing a JID from the string fails, the comparison always fails (even if the original JID is would match the invalid output). Comparisons involving unsafe JIDs constructed with Jid::new_unchecked should construct an unsafe JID from the string and manually compare the parts.

Source§

fn eq(&self, other: &String) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<str> for Jid<'_>

Allows JIDs to be compared with strings.

This may be expensive. The string is first split using Jid::split and each part is compared with its corresponding part in the JID. If this comparison is successful, true is returned and the comparison is cheap. If this does not match, however, the string is then canonicalized (by converting it into a JID) and the parts are compared again; this may require expensive heap allocations. If constructing a JID from the string fails, the comparison always fails (even if the original JID is would match the invalid output). Comparisons involving unsafe JIDs constructed with Jid::new_unchecked should construct an unsafe JID from the string and manually compare the parts.

§Examples

let j = Jid::from_str("example.net/rp")?;
assert!(j == "example.net/rp");
Source§

fn eq(&self, other: &str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialEq for Jid<'a>

Source§

fn eq(&self, other: &Jid<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialOrd for Jid<'a>

Source§

fn partial_cmp(&self, other: &Jid<'a>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> Eq for Jid<'a>

Source§

impl<'a> StructuralPartialEq for Jid<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Jid<'a>

§

impl<'a> RefUnwindSafe for Jid<'a>

§

impl<'a> Send for Jid<'a>

§

impl<'a> Sync for Jid<'a>

§

impl<'a> Unpin for Jid<'a>

§

impl<'a> UnwindSafe for Jid<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.