From c5cec6cef6709a13f2f781e7925854c84929f423 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 13 Sep 2024 11:01:30 +0200 Subject: [PATCH] simplify --- src/url.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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) } }