Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ergonomics by allowing to use a string input for fragment selections #27

Closed
svenstaro opened this issue Jul 14, 2019 · 2 comments

Comments

@svenstaro
Copy link

The intermediate parsing of the Selector itself is a bit annoying. I think it would be neat if it were part of the select().

Current:

let fragment = Html::parse_fragment(html);
let selector = Selector::parse("li").unwrap();

for element in fragment.select(&selector) {}

Proposed a):

let fragment = Html::parse_fragment(html);
for element in fragment.select("li".into()?) {}

Proposed b):

let fragment = Html::parse_fragment(html);
for element in fragment.select("li")? {}
@brokenthorn
Copy link

Isn't it more idiomatic Rust to implement Into<Selector> for str and/or String, instead of adding an extra function try_select, in #30 ?

@spegoraro
Copy link
Contributor

@brokenthorn I agree. I think it makes it much clearer as to what's going on, however it's more idiomatic again to implement TryFrom<&str> for Selector. The From (and implied Into) traits are meant for conversions that won't fail. Using their Try* equivalents returns a Result which makes more sense. By implementing TryFrom we get TryInto for free also.

Note, that it would be awesome to do something even more generic like this: impl<'i, T: Deref<Target = str>> TryFrom<&'i T> for Selector so that we get deref coercion automatically but at the moment that will cause a conflict with the blanket implementation in core (see rust-lang/rust#50133 (comment)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants