Added Address:new_unchecked (#887)

This commit is contained in:
Wyatt Herkamp
2023-08-18 18:11:47 -04:00
committed by GitHub
parent 16c35ef583
commit 7e6ffe8aea

View File

@@ -73,7 +73,18 @@ impl Address {
pub fn new<U: AsRef<str>, D: AsRef<str>>(user: U, domain: D) -> Result<Self, AddressError> {
(user, domain).try_into()
}
/// Creates a new email address from a string without checking it.
///
/// # Panics
/// Will panic if @ is not present in the string
pub fn new_unchecked(serialized: String) -> Self {
let at_start = serialized.chars().position(|c| c == '@').unwrap();
Self {
serialized,
at_start,
}
}
/// Gets the user portion of the `Address`.
///
/// # Examples
@@ -303,4 +314,19 @@ mod tests {
Address::check_domain("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com").is_err()
);
}
#[test]
fn test_new_unchecked() {
let addr = Address::new_unchecked("something@example.com".to_owned());
assert_eq!(addr.user(), "something");
assert_eq!(addr.domain(), "example.com");
assert_eq!(addr, Address::new("something", "example.com").unwrap());
}
#[test]
#[should_panic]
fn test_new_unchecked_panic() {
Address::new_unchecked("somethingexample.com".to_owned());
}
}