Now from_html method accept &str and String

master
kirbylife 2021-03-21 01:47:11 -06:00
parent 4b7bd8e221
commit 570a5d485d
4 changed files with 11 additions and 17 deletions

View File

@ -20,13 +20,10 @@ pub struct Request {
impl RequestBase for Request { impl RequestBase for Request {
fn new<T: IntoUrl>(url: T) -> Result<Request, ParseError> { fn new<T: IntoUrl>(url: T) -> Result<Request, ParseError> {
match url.into_url() { url.into_url().map(|url_parsed| Request {
Ok(url_parsed) => Ok(Request { url: url_parsed,
url: url_parsed, method: Method::GET,
method: Method::GET, })
}),
Err(error) => Err(error),
}
} }
fn launch(self) -> response::Response { fn launch(self) -> response::Response {

View File

@ -13,7 +13,7 @@ pub struct Response {
} }
impl SelectorBase for Response { impl SelectorBase for Response {
fn from_html(_: String) -> Self { fn from_html<S: AsRef<str>>(_: S) -> Self {
unimplemented!() unimplemented!()
} }

View File

@ -1,5 +1,5 @@
pub trait SelectorBase { pub trait SelectorBase {
fn from_html(html: String) -> Self; fn from_html<S: AsRef<str>>(html: S) -> Self;
fn html(&self) -> String; fn html(&self) -> String;
@ -58,9 +58,9 @@ pub struct Selector {
} }
impl SelectorBase for Selector { impl SelectorBase for Selector {
fn from_html(html: String) -> Self { fn from_html<S: AsRef<str>>(html: S) -> Self {
Selector { Selector {
text: html.to_string(), text: html.as_ref().to_string(),
} }
} }

View File

@ -15,8 +15,7 @@ fn plain_text_selector() {
<a>simple text</a> <a>simple text</a>
</body> </body>
</html> </html>
" ";
.to_string();
let sel = Selector::from_html(html); let sel = Selector::from_html(html);
assert_eq!(sel.css("h1")[0].html(), "<h1>hello world</h1>"); assert_eq!(sel.css("h1")[0].html(), "<h1>hello world</h1>");
assert_eq!(sel.css("#text")[0].content(), "good bye"); assert_eq!(sel.css("#text")[0].content(), "good bye");
@ -50,8 +49,7 @@ fn complex_selectors() {
</div> </div>
</body> </body>
</html> </html>
" ";
.to_string();
let sel = Selector::from_html(html); let sel = Selector::from_html(html);
assert_eq!(sel.css_once("p").unwrap().attr("id").unwrap(), "text"); assert_eq!(sel.css_once("p").unwrap().attr("id").unwrap(), "text");
assert_eq!(sel.css("a")[0].attr("href").unwrap(), "http://google.com"); assert_eq!(sel.css("a")[0].attr("href").unwrap(), "http://google.com");
@ -86,8 +84,7 @@ fn xpath_test() {
</div> </div>
</body> </body>
</html> </html>
" ";
.to_string();
let sel = Selector::from_html(html); let sel = Selector::from_html(html);
assert_eq!( assert_eq!(
sel.xpath_once("//div/a[1]").unwrap().content(), sel.xpath_once("//div/a[1]").unwrap().content(),