From 66bed7f0492fb941f3b4fd9dfd66895598609972 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Tue, 16 Mar 2021 21:20:50 -0600 Subject: [PATCH] fix the lifetimes declarations --- src/request.rs | 13 +++++++++---- src/selector.rs | 4 ++-- tests/unit_tests.rs | 6 ++++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/request.rs b/src/request.rs index 8901ac8..cf775bc 100644 --- a/src/request.rs +++ b/src/request.rs @@ -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 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 { + fn new<'a>(url: &'a str) -> Result { 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(self, _attrs: T) -> Self { + unimplemented!(); + } } diff --git a/src/selector.rs b/src/selector.rs index e6c33d7..3e4afdd 100644 --- a/src/selector.rs +++ b/src/selector.rs @@ -45,7 +45,7 @@ pub trait SelectorBase { .unwrap() } - fn attr(&self, attribute: &'static str) -> Option { + fn attr<'a>(&self, attribute: &'a str) -> Option { 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() } } diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs index 16f3c43..605cc1a 100644 --- a/tests/unit_tests.rs +++ b/tests/unit_tests.rs @@ -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]")