summaryrefslogtreecommitdiffstats
path: root/headers
diff options
context:
space:
mode:
authorPhilipp Korber <p.korber@1aim.com>2018-12-03 17:32:58 +0100
committerPhilipp Korber <p.korber@1aim.com>2018-12-03 17:32:58 +0100
commitb7025344f11bc7ae05d2a4d7c6f0f20dbeecf9b6 (patch)
tree62b39a4559746e30c6d3251a1c5eba2f7bd8841b /headers
parent781b052edd9c388a093279c92d4e54da49c641cd (diff)
feat(Domain) impl `FromStr` for domain
Diffstat (limited to 'headers')
-rw-r--r--headers/src/header_components/email.rs29
1 files changed, 24 insertions, 5 deletions
diff --git a/headers/src/header_components/email.rs b/headers/src/header_components/email.rs
index afe549f..5b9ad4b 100644
--- a/headers/src/header_components/email.rs
+++ b/headers/src/header_components/email.rs
@@ -1,5 +1,6 @@
use std::ops::Deref;
use std::borrow::Cow;
+use std::str::FromStr;
use failure::Fail;
use soft_ascii_string::{SoftAsciiStr, SoftAsciiString, SoftAsciiChar};
@@ -163,19 +164,28 @@ impl Deref for LocalPart {
impl<T> HeaderTryFrom<T> for Domain
where T: HeaderTryInto<Input>
{
- fn try_from( input: T ) -> Result<Self, ComponentCreationError> {
+ fn try_from(input: T) -> Result<Self, ComponentCreationError> {
let input = input.try_into()?;
let item =
- match Domain::check_domain( input.as_str() )? {
+ match Domain::check_domain(input.as_str())? {
MailType::Ascii | MailType::Mime8BitEnabled => {
- SimpleItem::Ascii( input.into_ascii_item_unchecked() )
+ SimpleItem::Ascii(input.into_ascii_item_unchecked())
},
MailType::Internationalized => {
- SimpleItem::from_utf8_input( input )
+ SimpleItem::from_utf8_input(input)
}
};
- Ok( Domain( item ) )
+ Ok(Domain(item))
+ }
+}
+
+impl FromStr for Domain {
+ type Err = ComponentCreationError;
+
+ fn from_str(domain: &str) -> Result<Self, Self::Err> {
+ let input = Input::from(domain);
+ Self::try_from(input)
}
}
@@ -401,4 +411,13 @@ mod test {
let stringified = domain.into_ascii_string().unwrap();
assert_eq!(&*stringified, "xn--h-1ga.test")
}
+
+ #[test]
+ fn domain_from_str() {
+ let domain: Domain = "1aim.com".parse().unwrap();
+ assert_eq!(domain.as_str(), "1aim.com");
+
+ let res: Result<Domain, _> = "...".parse();
+ assert!(res.is_err());
+ }
} \ No newline at end of file