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§
source§impl AddrSpec
impl AddrSpec
sourcepub fn normalize<Address>(address: Address) -> Result<String, ParseError>where
Address: AsRef<str>,
pub fn normalize<Address>(address: Address) -> Result<String, ParseError>where
Address: AsRef<str>,
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"
);sourcepub fn new<LocalPart, Domain>(
local_part: LocalPart,
domain: Domain
) -> Result<Self, ParseError>where
LocalPart: AsRef<str>,
Domain: AsRef<str>,
pub fn new<LocalPart, Domain>(
local_part: LocalPart,
domain: Domain
) -> Result<Self, ParseError>where
LocalPart: AsRef<str>,
Domain: AsRef<str>,
Creates a new address specification. This will validate the local part and domain and perform NFC-normalization.
sourcepub fn with_literal<LocalPart, Domain>(
local_part: LocalPart,
domain: Domain
) -> Result<Self, ParseError>where
LocalPart: AsRef<str>,
Domain: AsRef<str>,
pub fn with_literal<LocalPart, Domain>(
local_part: LocalPart,
domain: Domain
) -> Result<Self, ParseError>where
LocalPart: AsRef<str>,
Domain: AsRef<str>,
Creates a new address specification with a literal domain. This will validate the local part and domain and perform NFC-normalization.
sourcepub unsafe fn new_unchecked<LocalPart, Domain>(
local_part: LocalPart,
domain: Domain
) -> Selfwhere
LocalPart: Into<String>,
Domain: Into<String>,
pub unsafe fn new_unchecked<LocalPart, Domain>(
local_part: LocalPart,
domain: Domain
) -> Selfwhere
LocalPart: Into<String>,
Domain: Into<String>,
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.
sourcepub unsafe fn with_literal_unchecked<LocalPart, Domain>(
local_part: LocalPart,
domain: Domain
) -> Selfwhere
LocalPart: Into<String>,
Domain: Into<String>,
pub unsafe fn with_literal_unchecked<LocalPart, Domain>(
local_part: LocalPart,
domain: Domain
) -> Selfwhere
LocalPart: Into<String>,
Domain: Into<String>,
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.
sourcepub fn local_part(&self) -> &str
pub fn local_part(&self) -> &str
Returns the local part of the address.
sourcepub fn is_literal(&self) -> bool
pub fn is_literal(&self) -> bool
Returns whether the domain is literal.
sourcepub fn into_parts(self) -> (String, String)
pub fn into_parts(self) -> (String, String)
Returns the local part and domain of the address.
sourcepub fn into_parts_with_literal(self) -> (String, String, bool)
pub fn into_parts_with_literal(self) -> (String, String, bool)
Returns the local part and domain of the address.
Trait Implementations§
source§impl Ord for AddrSpec
impl Ord for AddrSpec
source§impl PartialEq<AddrSpec> for AddrSpec
impl PartialEq<AddrSpec> for AddrSpec
source§impl PartialOrd<AddrSpec> for AddrSpec
impl PartialOrd<AddrSpec> for AddrSpec
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more