From 7e6ffe8aea301eef2125415e80297bdf6cf6c0e8 Mon Sep 17 00:00:00 2001 From: Wyatt Herkamp Date: Fri, 18 Aug 2023 18:11:47 -0400 Subject: [PATCH] Added Address:new_unchecked (#887) --- src/address/types.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/address/types.rs b/src/address/types.rs index f18c84e..b02f580 100644 --- a/src/address/types.rs +++ b/src/address/types.rs @@ -73,7 +73,18 @@ impl Address { pub fn new, D: AsRef>(user: U, domain: D) -> Result { (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()); + } }