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

View File

@ -45,7 +45,7 @@ pub trait SelectorBase {
.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());
match html.select("body > *").attr(attribute) {
Some(text) => Some(format!("{}", text)),
@ -67,6 +67,6 @@ impl SelectorBase for Selector {
}
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("//a[contains(@href, 'localhost')]")[0].content(),
"link"
sel.xpath("//a[contains(@href, 'localhost')]")[0]
.attr("href")
.unwrap(),
"http://localhost"
);
assert_eq!(
sel.xpath_once("//div[@class='container']/a[3]")