From f1c0bc340ee0b0c6e6dbe44db5c3922da8434c17 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Sat, 20 Mar 2021 19:52:02 -0600 Subject: [PATCH] add new trait called IntoUrl --- src/into_url.rs | 21 +++++++++++++++++++++ src/lib.rs | 1 + src/request.rs | 9 +++++---- 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/into_url.rs 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,