From 786d025691490241f1727489f2a7cfd2c5b12770 Mon Sep 17 00:00:00 2001 From: Filip Gospodinov Date: Wed, 23 Aug 2023 13:52:23 +0200 Subject: [PATCH] impl IntoUrl for http::Uri --- src/into_url.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/into_url.rs b/src/into_url.rs index d60f5c952..ca5ce727e 100644 --- a/src/into_url.rs +++ b/src/into_url.rs @@ -1,3 +1,4 @@ +use http::Uri; use url::Url; /// A trait to try to convert some type into a `Url`. @@ -7,6 +8,7 @@ use url::Url; pub trait IntoUrl: IntoUrlSealed {} impl IntoUrl for Url {} +impl IntoUrl for Uri {} impl IntoUrl for String {} impl<'a> IntoUrl for &'a str {} impl<'a> IntoUrl for &'a String {} @@ -43,6 +45,21 @@ impl IntoUrlSealed for Url { } } +impl IntoUrlSealed for Uri { + fn into_url(self) -> crate::Result { + let url = Url::parse(&self.to_string()).map_err(crate::error::builder)?; + if self.host().is_some() { + Ok(url) + } else { + Err(crate::error::url_bad_scheme(url)) + } + } + + fn as_str(&self) -> &str { + self.to_string().as_str() + } +} + impl<'a> IntoUrlSealed for &'a str { fn into_url(self) -> crate::Result { Url::parse(self).map_err(crate::error::builder)?.into_url()