summaryrefslogtreecommitdiffstats
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
parent781b052edd9c388a093279c92d4e54da49c641cd (diff)
feat(Domain) impl `FromStr` for domain
-rw-r--r--headers/src/header_components/email.rs29
-rw-r--r--mail/README.md4
2 files changed, 26 insertions, 7 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
diff --git a/mail/README.md b/mail/README.md
index 994c5c0..4dd7ee5 100644
--- a/mail/README.md
+++ b/mail/README.md
@@ -1,6 +1,6 @@
-# mail / mail-api &emsp;
+# mail &emsp;
-Documentation can be [viewed on docs.rs](https://docs.rs/mail-api).
+Documentation can be [viewed on docs.rs](https://docs.rs/mail).
Facade which re-exports functionality from a number of mail related crates.