From ded3cc40743d0ecabf53c3c45ed6724164c08951 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Sun, 21 Mar 2021 01:32:43 -0600 Subject: [PATCH] Now the selectors methods (css/_once, xpath/_once) works with str & String --- Cargo.toml | 2 +- src/selector.rs | 26 ++++++++++++-------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3b77d0c..85ee670 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,4 @@ http = "0.2" nipper = "0.1.8" serde_json = "1.0" serde = "1.0" -cssifier = "0.1.2" \ No newline at end of file +cssifier = "0.1.3" \ No newline at end of file diff --git a/src/selector.rs b/src/selector.rs index 3e4afdd..4e19690 100644 --- a/src/selector.rs +++ b/src/selector.rs @@ -3,26 +3,25 @@ pub trait SelectorBase { fn html(&self) -> String; - // fn css<'a, T: SelectorBase>(&self, css_selector: &'a str) -> Vec { - fn css<'a>(&self, css_selector: &'a str) -> Vec { + fn css>(&self, css_selector: S) -> Vec { let html = nipper::Document::from(self.html().as_str()); let mut output = vec![]; - for item in html.select(css_selector).iter() { + for item in html.select(css_selector.as_ref()).iter() { output.push(Selector::from_html(item.html().to_string())) } output } - fn css_once<'a>(&self, css_selector: &'a str) -> Option { - self.css(css_selector).pop() + fn css_once>(&self, css_selector: S) -> Option { + self.css(css_selector.as_ref()).pop() } - fn xpath(&self, xpath: &'static str) -> Vec { - match cssifier::cssifier(xpath) { + fn xpath>(&self, xpath: S) -> Vec { + match cssifier::cssifier(xpath.as_ref()) { Some(css_selector) => { - if css_selector == "" { + if css_selector.is_empty() { Vec::default() } else { self.css(css_selector.as_str()) @@ -32,8 +31,8 @@ pub trait SelectorBase { } } - fn xpath_once(&self, xpath: &'static str) -> Option { - self.xpath(xpath).pop() + fn xpath_once>(&self, xpath: S) -> Option { + self.xpath(xpath.as_ref()).pop() } fn content(&self) -> String { @@ -47,10 +46,9 @@ pub trait SelectorBase { 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)), - None => None, - } + html.select(" body > *") + .attr(attribute) + .map(|text| text.to_string()) } }