diff --git a/src/url.rs b/src/url.rs index f3b3166..c64c54a 100644 --- a/src/url.rs +++ b/src/url.rs @@ -32,15 +32,18 @@ impl Url { pub fn domain(&self) -> &str { self.0.domain().expect("has domain") } + fn new(value: url::Url) -> Result { + if value.domain().is_none() { + return Err(url::ParseError::EmptyHost); + } + Ok(Url(value)) + } } impl TryFrom for Url { type Error = url::ParseError; fn try_from(value: url::Url) -> Result { - if value.domain().is_none() { - return Err(url::ParseError::EmptyHost); - } - Ok(Url(value)) + Self::new(value) } } @@ -55,11 +58,8 @@ impl FromStr for Url { type Err = url::ParseError; fn from_str(s: &str) -> Result { - let url = url::Url::from_str(s)?; - if url.domain().is_none() { - return Err(url::ParseError::EmptyHost); - } - Ok(Url(url)) + let value = url::Url::from_str(s)?; + Self::new(value) } }