fix the lifetimes declarations

master
kirbylife 2021-03-16 21:20:50 -06:00
parent fd0ef3caa9
commit 66bed7f049
3 changed files with 15 additions and 8 deletions

View File

@ -1,4 +1,5 @@
use http::Method; use http::Method;
use serde::Serialize;
use url::{ParseError, Url}; use url::{ParseError, Url};
use crate::response; use crate::response;
@ -7,7 +8,7 @@ pub trait RequestBase {
fn new(url: &'static str) -> Result<Self, ParseError> fn new(url: &'static str) -> Result<Self, ParseError>
where where
Self: Sized; Self: Sized;
fn launch(&self) -> response::Response; fn launch(self) -> response::Response;
} }
#[derive(Debug)] #[derive(Debug)]
@ -17,7 +18,7 @@ pub struct Request {
} }
impl RequestBase for Request { impl RequestBase for Request {
fn new(url: &'static str) -> Result<Request, ParseError> { fn new<'a>(url: &'a str) -> Result<Request, ParseError> {
match Url::parse(url) { match Url::parse(url) {
Ok(url_parsed) => Ok(Request { Ok(url_parsed) => Ok(Request {
url: url_parsed, url: url_parsed,
@ -27,7 +28,7 @@ impl RequestBase for Request {
} }
} }
fn launch(&self) -> response::Response { fn launch(self) -> response::Response {
let client = reqwest::blocking::Client::new(); let client = reqwest::blocking::Client::new();
let resp = (match self.method { let resp = (match self.method {
Method::GET => client.get(self.url.as_str()), Method::GET => client.get(self.url.as_str()),
@ -54,8 +55,12 @@ impl RequestBase for Request {
} }
impl Request { impl Request {
pub fn method(&mut self, method: Method) -> &mut Self { pub fn method(mut self, method: Method) -> Self {
self.method = method; self.method = method;
self self
} }
pub fn add_attrs<T: Serialize + Sized>(self, _attrs: T) -> Self {
unimplemented!();
}
} }

View File

@ -45,7 +45,7 @@ pub trait SelectorBase {
.unwrap() .unwrap()
} }
fn attr(&self, attribute: &'static str) -> Option<String> { fn attr<'a>(&self, attribute: &'a str) -> Option<String> {
let html = nipper::Document::from(self.html().as_str()); let html = nipper::Document::from(self.html().as_str());
match html.select("body > *").attr(attribute) { match html.select("body > *").attr(attribute) {
Some(text) => Some(format!("{}", text)), Some(text) => Some(format!("{}", text)),
@ -67,6 +67,6 @@ impl SelectorBase for Selector {
} }
fn html(&self) -> String { fn html(&self) -> String {
format!("{}", self.text) self.text.clone()
} }
} }

View File

@ -95,8 +95,10 @@ fn xpath_test() {
); );
assert_eq!(sel.xpath("//*[@id='text']")[0].content(), "good bye"); assert_eq!(sel.xpath("//*[@id='text']")[0].content(), "good bye");
assert_eq!( assert_eq!(
sel.xpath("//a[contains(@href, 'localhost')]")[0].content(), sel.xpath("//a[contains(@href, 'localhost')]")[0]
"link" .attr("href")
.unwrap(),
"http://localhost"
); );
assert_eq!( assert_eq!(
sel.xpath_once("//div[@class='container']/a[3]") sel.xpath_once("//div[@class='container']/a[3]")