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.
If you intend to create an address specification from individual parts, make
sure to validate both local part and domain using the is_valid_local_part
and is_valid_domain functions before to_stringing the spec. You can also
use the is_valid function to validate the whole address.
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(), r#"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".to_string(), "example.com".to_string());
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: &str) -> Result<String, ParseError>
pub fn normalize(address: &str) -> 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 with_literal(local_part: String, domain: String) -> Self
pub fn with_literal(local_part: String, domain: String) -> Self
Creates a new address specification with a literal domain.
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.
sourcepub fn is_local_part_valid(&self) -> Option<usize>
pub fn is_local_part_valid(&self) -> Option<usize>
Returns the index of the first invalid character in the local part.
sourcepub fn is_domain_valid(&self) -> Option<usize>
pub fn is_domain_valid(&self) -> Option<usize>
Returns the index of the first invalid character in the domain.
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