Skip to content

Commit

Permalink
Implement IntoUrl for String (#1201)
Browse files Browse the repository at this point in the history
Also change from blanket impl to improve docs.
  • Loading branch information
ibraheemdev authored Mar 1, 2021
1 parent ff2381e commit 9fa58e3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
38 changes: 25 additions & 13 deletions src/into_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ use url::Url;
/// A trait to try to convert some type into a `Url`.
///
/// This trait is "sealed", such that only types within reqwest can
/// implement it. The reason is that it will eventually be deprecated
/// and removed, when `std::convert::TryFrom` is stabilized.
pub trait IntoUrl: PolyfillTryInto {}
/// implement it.
pub trait IntoUrl: IntoUrlSealed {}

impl<T: PolyfillTryInto> IntoUrl for T {}
impl IntoUrl for Url {}
impl IntoUrl for String {}
impl<'a> IntoUrl for &'a str {}
impl<'a> IntoUrl for &'a String {}

pub trait PolyfillTryInto {
pub trait IntoUrlSealed {
// Besides parsing as a valid `Url`, the `Url` must be a valid
// `http::Uri`, in that it makes sense to use in a network request.
fn into_url(self) -> crate::Result<Url>;

fn _as_str(&self) -> &str;
fn as_str(&self) -> &str;
}

impl PolyfillTryInto for Url {
impl IntoUrlSealed for Url {
fn into_url(self) -> crate::Result<Url> {
if self.has_host() {
Ok(self)
Expand All @@ -26,27 +28,37 @@ impl PolyfillTryInto for Url {
}
}

fn _as_str(&self) -> &str {
fn as_str(&self) -> &str {
self.as_ref()
}
}

impl<'a> PolyfillTryInto for &'a str {
impl<'a> IntoUrlSealed for &'a str {
fn into_url(self) -> crate::Result<Url> {
Url::parse(self).map_err(crate::error::builder)?.into_url()
}

fn _as_str(&self) -> &str {
self.as_ref()
fn as_str(&self) -> &str {
self
}
}

impl<'a> PolyfillTryInto for &'a String {
impl<'a> IntoUrlSealed for &'a String {
fn into_url(self) -> crate::Result<Url> {
(&**self).into_url()
}

fn _as_str(&self) -> &str {
fn as_str(&self) -> &str {
self.as_ref()
}
}

impl<'a> IntoUrlSealed for String {
fn into_url(self) -> crate::Result<Url> {
(&*self).into_url()
}

fn as_str(&self) -> &str {
self.as_ref()
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;
use std::net::SocketAddr;
use std::sync::Arc;

use crate::into_url::{IntoUrl, PolyfillTryInto};
use crate::into_url::{IntoUrl, IntoUrlSealed};
use crate::Url;
use http::{header::HeaderValue, Uri};
use ipnet::IpNet;
Expand Down Expand Up @@ -111,11 +111,11 @@ pub trait IntoProxyScheme {
impl<S: IntoUrl> IntoProxyScheme for S {
fn into_proxy_scheme(self) -> crate::Result<ProxyScheme> {
// validate the URL
let url = match self._as_str().into_url() {
let url = match self.as_str().into_url() {
Ok(ok) => ok,
Err(e) => {
// the issue could have been caused by a missing scheme, so we try adding http://
format!("http://{}", self._as_str())
format!("http://{}", self.as_str())
.into_url()
.map_err(|_| {
// return the original error
Expand Down

0 comments on commit 9fa58e3

Please sign in to comment.