Comments (10)
Here is a MVP: https://github.com/SirWindfield/scraper-proc-macro. Compilation fails for one of the tests as the proc-macro panics due to a wrong formatted selector string.
from scraper.
FWIW I define the following macro that enables caching too.
use once_cell::sync::Lazy;
use scraper::Selector;
macro_rules! selector {
($e: expr) => {{
static SELECTOR: Lazy<Selector> = Lazy::new(|| Selector::parse($e).unwrap());
&*SELECTOR
}};
}
from scraper.
Why do you think it has to be a macro?
A macro is nicer in this case because it's easier to return compile errors which reduce the need for run-time debugging.
The first thing I did when using this crate is write a function that takes a &'static str, constructs a selector and unwraps the Result.
It is probably useful in some cases to construct selectors dynamically (so either a function taking a String
or Cow<'static, str>
is helpful thereย โ &'_ str
's are harder to manipulate because they are stack rather than heap references).
from scraper.
The first thing I did when using this crate is write a function that takes a &'static str, constructs a selector and unwraps the Result.
I would like if this function could be included in this crate.
Why do you think it has to be a macro?
(Btw, the regex crate doesn't have that macro.)
from scraper.
A macro is nicer in this case because it's easier to return compile errors which reduce the need for run-time debugging.
How would a macro return a compile-time error?
FWIW I define the following macro that enables caching too.
Wouldn't caching be the only benefit of a selector!
style macro?
from scraper.
How would a macro return a compile-time error?
There's a compiler built-in macro called [compile_error!](https://doc.rust-lang.org/stable/std/macro.compile_error.html)
; if a macro outputs that in the code it generates, the compiler fails the compilation and reports the error(s).
from scraper.
Sorry, I meant how would a macro know if a selector was invalid?
from scraper.
Oops, sorry, I miswrote "too", but my prototype is the one which only enables caching.
from scraper.
use once_cell::sync::Lazy; use scraper::Selector; macro_rules! selector { ($e: expr) => {{ static SELECTOR: Lazy<Selector> = Lazy::new(|| Selector::parse($e).unwrap()); &*SELECTOR }}; }
I personally think that there isn't much value in adding this to the library (in its current form). It's easy enough that it can be added to the user's codebase instead and is probably the place where something like this should be added as well.
As far as I can tell there is no way to actually check a static string at compile-time and throw a compilation-error if the string is not valid. Normal macros are just expanded, not evaluated.
Maybe a proc-macro would do the trick (just the idea sketched out):
#[proc_macro]
pub fn make_answer(ts: TokenStream) -> TokenStream {
if /* first and only argument is a static string */ {
if Selector::parse(/* first argument */).is_ok() {
/* emit "Selector::parse(first arg).unwrap()" */
} else {
compile_error!("failed to compile selector string");
}
}
}
Probably not the cleanest solution, but I think it would work. It's easy enough to not even require complex library dependencies like syn
, since the only check that has to be done is the "token is a static string" check.
from scraper.
I personally think that there isn't much value in adding this to the library (in its current form). It's easy enough that it can be added to the user's codebase instead and is probably the place where something like this should be added as well.
Agree. Maybe an external library, like the one @mainrs provided, is better
from scraper.
Related Issues (20)
- same element? HOT 3
- Allow `Selector` to be const-creatable HOT 3
- Save source code `ElementRef::html()` HOT 3
- Malformed HTML parsed differently from browsers
- Make element traversal more convenient HOT 2
- [Feature Request] Find by Text HOT 1
- Dom Nodes closes prematurely on recursion HOT 4
- How to select contains and start with? HOT 1
- any way to scrape in a stream? HOT 2
- Support for `:has()` selector HOT 8
- Implement Send for ElementRef HOT 16
- Convert <br> to '\n' in `text`? HOT 1
- future created by async block is not `Send` HOT 2
- Upgrade ahash HOT 1
- Html and its children do not impl Send HOT 5
- Scraper logs 1gb HOT 4
- Select::parse fails due to borrow the css query HOT 26
- select() doesn't work on current ElementRef HOT 2
- The problem of creating a structure with the Select field HOT 7
- Get text of element without children HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scraper.