diff --git a/src/into_url.rs b/src/into_url.rs new file mode 100644 index 0000000..5bf8405 --- /dev/null +++ b/src/into_url.rs @@ -0,0 +1,21 @@ +pub trait IntoUrl { + fn into_url(self) -> Result; +} + +impl<'a> IntoUrl for &'a str { + fn into_url(self) -> Result { + url::Url::parse(self) + } +} + +impl<'a> IntoUrl for &'a String { + fn into_url(self) -> Result { + (&**self).into_url() + } +} + +impl IntoUrl for String { + fn into_url(self) -> Result { + (&*self).into_url() + } +} diff --git a/src/lib.rs b/src/lib.rs index 0e5b335..80b3cc5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod into_url; pub mod request; pub mod response; pub mod selector; diff --git a/src/request.rs b/src/request.rs index cf775bc..d50ff65 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,11 +1,12 @@ use http::Method; use serde::Serialize; -use url::{ParseError, Url}; +use url::ParseError; +use crate::into_url::IntoUrl; use crate::response; pub trait RequestBase { - fn new(url: &'static str) -> Result + fn new(url: T) -> Result where Self: Sized; fn launch(self) -> response::Response; @@ -18,8 +19,8 @@ pub struct Request { } impl RequestBase for Request { - fn new<'a>(url: &'a str) -> Result { - match Url::parse(url) { + fn new(url: T) -> Result { + match url.into_url() { Ok(url_parsed) => Ok(Request { url: url_parsed, method: Method::GET,