Struct addr_spec::AddrSpec

source ·
pub struct AddrSpec { /* private fields */ }
Expand description

Address specification as defined in RFC 5322 with UTF-8 support as defined in RFC 6532.

Both the local part and the domain are normalized using the NFC as recommended in Section 3.1, RFC 6532. Address strings built using this crate work well for unique, UTF-8 identifiers.

Examples

use std::str::FromStr;

use addr_spec::AddrSpec;

let addr_spec = AddrSpec::from_str("test@example.com").unwrap();
assert_eq!(addr_spec.local_part(), "test");
assert_eq!(addr_spec.domain(), "example.com");
assert_eq!(addr_spec.is_literal(), false);
assert_eq!(addr_spec.to_string(), "test@example.com");

Quoted local parts will be unescaped if possible:

use std::str::FromStr;

use addr_spec::AddrSpec;

let addr_spec = AddrSpec::from_str(r#""test"@example.com"#).unwrap();
assert_eq!(addr_spec.local_part(), "test");
assert_eq!(addr_spec.domain(), "example.com");
assert_eq!(addr_spec.is_literal(), false);
assert_eq!(addr_spec.to_string(), "test@example.com");

Literal domains are also supported:

use std::str::FromStr;

use addr_spec::AddrSpec;

let addr_spec = AddrSpec::from_str("test@[IPv6:2001:db8::1]").unwrap();
assert_eq!(addr_spec.local_part(), "test");
assert_eq!(addr_spec.domain(), "IPv6:2001:db8::1");
assert_eq!(addr_spec.is_literal(), true);
assert_eq!(addr_spec.to_string(), "test@[IPv6:2001:db8::1]");

You can also create an address specification from its parts:

use addr_spec::AddrSpec;

let addr_spec = AddrSpec::new("test", "example.com").unwrap();
assert_eq!(addr_spec.local_part(), "test");
assert_eq!(addr_spec.domain(), "example.com");
assert_eq!(addr_spec.is_literal(), false);
assert_eq!(addr_spec.to_string(), "test@example.com");

If you want to just normalize an address, you can use the normalize function:

use addr_spec::AddrSpec;

assert_eq!(
    &AddrSpec::normalize("\"test\"@example.com").unwrap(),
    "test@example.com"
);

References

Implementations§

Normalizes the address.

This is a convenience function that parses the address and then serializes it again.

It is equivalent to address.parse::<AddrSpec>()?.to_string().

Examples
use addr_spec::AddrSpec;

assert_eq!(
    &AddrSpec::normalize("\"test\"@example.com").unwrap(),
    "test@example.com"
);

Creates a new address specification. This will validate the local part and domain and perform NFC-normalization.

Creates a new address specification with a literal domain. This will validate the local part and domain and perform NFC-normalization.

Creates a new address specification without performing any validation or normalization.

Safety

This function is unsafe because it does not validate nor normalize the local part or domain. If the local part or domain contains invalid characters or is not NFC-normalized, the resulting address specification will be invalid.

Only use this function if you are sure that the local part and domain are valid and NFC-normalized. This is typically the case if you are getting them from a trusted source.

Creates a new address specification with a domain literal without performing any validation or normalization.

Safety

This function is unsafe because it does not validate nor normalize the local part or domain. If the local part or domain contains invalid characters or is not NFC-normalized, the resulting address specification will be invalid.

Only use this function if you are sure that the local part and domain are valid and NFC-normalized. This is typically the case if you are getting them from a trusted source.

Returns the local part of the address.

Returns the domain of the address.

Returns whether the domain is literal.

Returns the local part and domain of the address.

Returns the local part and domain of the address.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Converts the given value to a String. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.