Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl IntoUrl for http::Uri #1950

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/into_url.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use http::Uri;
use url::Url;

/// A trait to try to convert some type into a `Url`.
Expand All @@ -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 {}
Expand Down Expand Up @@ -43,6 +45,21 @@ impl IntoUrlSealed for Url {
}
}

impl IntoUrlSealed for Uri {
fn into_url(self) -> crate::Result<Url> {
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> {
Url::parse(self).map_err(crate::error::builder)?.into_url()
Expand Down
Loading