scraper: Select::parse fails due to borrow the css query
Hi, I has been using this library from a while, has been great.
Here a example:
fn a(document: &Html) -> Result<(), Box<dyn Error>> {
let query = format!("div table thead tr:nth-child({}) th", 3);
let selector = Selector::parse(&query)?;
Ok(())
}
This function will fails, the reason is pretty simple, the function owns the query, but the error triggered by parse seems to have borrowed the string, so it can’t return an error when the function owns the data.
The workaround right now is unwrap instead use error propagation.
There is some ways to handle this, one would be a parse with String which owns the data, other one is instead use a borrowed string for the error, convert &str to String and use that instead.
Thx!
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 26 (19 by maintainers)
I am not sure if I find that proc macro particularly helpful as while it does verify at compile time, it will just compile the selector again at runtime. If the dependency tree is sufficiently messy, it could even be that the verification at runtime uses a different version of the
scrapercrate and compilation at runtime still fails (admittedly a somewhat unlikely scenario).I think what we should aim for is a proc macro that actually outputs a representation (maybe via serialization) of the already parsed selector so that work is not repeated at runtime.
I also think the span in the error case should tightened to indicate only the literal, not the whole macro invocation though.
There is some advantages.
While more clear, better :3 at least is what I think.
A simpler solution would be writing our macros in a different crate and let our users depend on this new crate in order to use them. However, I would prefer exposing our new macros directly from scraper behind a feature flag
The macro should generate a selector representation directly, without requiring parsing at run time. Moreover, while it’s true that the basic approach would require cyclic dependencies - which, as you now, are not allowed - the issue can be solved by writing three crates: selectors_core, selectors_macros (which will depend on core) and selectors, which will depend on both. Obviously, as this sounds a bit overcomplicated, feel free to suggest more straightforward approaches
I was reading that a
proc_macroneeds to live necessarely in a separate crate, so yes, this is true, it would be quite invasive as an addition though, but yet usefulAt the time, I chose to close #53 because another crate was actively being developed to address the issue. Now the repo of that crate is inactive.
I think we should provide an “official” implementation so that we can maintain and improve it over time. Moreover, it could be useful for beginners who don’t want to roll out their own macro
You can continue to use error propagation, you just need to downgrade the to a string, e.g.
but note that for selectors known at compile-time,
.unwrap()is somewhat reasonable as failing to parse such a selector is a logic error instead of a runtime error. (For your example of a dynamically generated selector, one would have to consider how much leeway there is, i.e. if you really only interpolate anu8into thenth-childpart, then.unwrap()might still be reasonable as calling code should be unable to produce an invalid selector by choosing the value of theu8.)