add new trait called IntoUrl

master
kirbylife 2021-03-20 19:52:02 -06:00
parent 66bed7f049
commit f1c0bc340e
3 changed files with 27 additions and 4 deletions

21
src/into_url.rs 100644
View File

@ -0,0 +1,21 @@
pub trait IntoUrl {
fn into_url(self) -> Result<url::Url, url::ParseError>;
}
impl<'a> IntoUrl for &'a str {
fn into_url(self) -> Result<url::Url, url::ParseError> {
url::Url::parse(self)
}
}
impl<'a> IntoUrl for &'a String {
fn into_url(self) -> Result<url::Url, url::ParseError> {
(&**self).into_url()
}
}
impl IntoUrl for String {
fn into_url(self) -> Result<url::Url, url::ParseError> {
(&*self).into_url()
}
}

View File

@ -1,3 +1,4 @@
pub mod into_url;
pub mod request; pub mod request;
pub mod response; pub mod response;
pub mod selector; pub mod selector;

View File

@ -1,11 +1,12 @@
use http::Method; use http::Method;
use serde::Serialize; use serde::Serialize;
use url::{ParseError, Url}; use url::ParseError;
use crate::into_url::IntoUrl;
use crate::response; use crate::response;
pub trait RequestBase { pub trait RequestBase {
fn new(url: &'static str) -> Result<Self, ParseError> fn new<T: IntoUrl>(url: T) -> Result<Self, ParseError>
where where
Self: Sized; Self: Sized;
fn launch(self) -> response::Response; fn launch(self) -> response::Response;
@ -18,8 +19,8 @@ pub struct Request {
} }
impl RequestBase for Request { impl RequestBase for Request {
fn new<'a>(url: &'a str) -> Result<Request, ParseError> { fn new<T: IntoUrl>(url: T) -> Result<Request, ParseError> {
match Url::parse(url) { match url.into_url() {
Ok(url_parsed) => Ok(Request { Ok(url_parsed) => Ok(Request {
url: url_parsed, url: url_parsed,
method: Method::GET, method: Method::GET,