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;
#[cfg(feature = "literals")]
{
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>
pub fn normalize<Address>(address: Address) -> Result<String, ParseError>
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>
pub fn new<LocalPart, Domain>( local_part: LocalPart, domain: Domain, ) -> Result<Self, ParseError>
Creates a new address specification. This will validate the local part and domain and perform NFC-normalization.
Sourcepub unsafe fn new_unchecked<LocalPart, Domain>(
local_part: LocalPart,
domain: Domain,
) -> Self
pub unsafe fn new_unchecked<LocalPart, Domain>( local_part: LocalPart, domain: Domain, ) -> Self
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 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_serialized_parts(self) -> (String, String)
pub fn into_serialized_parts(self) -> (String, String)
Returns serialized versions of the local part and domain of the address.
This is useful if you need to transport the address specification over line-based protocols such as SMTP and need to ensure that the local part and domain fit on a single line or require folding white-spaces.