You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let dom = tl::parse("<my html content>", tl::ParserOptions::default())?;let parser = dom.parser();let a = dom
.query_selector("#my-id").context("could not query selector")?
.nth(0).context("could not get first element")?;let a_html = a
.get(parser).context("could not get node")?
.inner_html(parser).to_string();let a_dom = tl::parse(&a_html, tl::ParserOptions::default())?;let b = a_dom
.query_selector(".my-class").context("could not query select")?;
I tryied to create a helper function to reduce the boilerplate code of getting a NodeHandle html, creating a sub dom and applying my query selector like so:
fnquery_selector<'b>(selector:&'bstr,node:&'bNodeHandle,parser:&'bParser,) -> Option<QuerySelectorIterator<'b,'b,VDom<'b>>>{let html = node.get(parser)?.inner_html(parser);let dom = tl::parse(&html, tl::ParserOptions::default()).ok()?;let selected = dom.query_selector(selector)?;returnSome(selected);}
But of course I will have referencing errors
error[E0515]: cannot return value referencing local variable `dom`
How could I manage to reduce this boilerplate code, or is it even imposible to do so because of the design of the library and the rigidity of Rust?
I'm fairly new to the language, still tinkering with the way I have to think in order to please the borrow checker.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
This library is really great, however a very common thing that can quickly become messy is to query select elements inside another element.
Like this in JS:
With
tl
I'm doing it this way:I tryied to create a helper function to reduce the boilerplate code of getting a
NodeHandle
html, creating a sub dom and applying my query selector like so:But of course I will have referencing errors
How could I manage to reduce this boilerplate code, or is it even imposible to do so because of the design of the library and the rigidity of Rust?
I'm fairly new to the language, still tinkering with the way I have to think in order to please the borrow checker.
Beta Was this translation helpful? Give feedback.
All reactions