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

Introduce proc macros for scraper #203

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
*.bk
.idea
.vscode
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
[workspace]
resolver = "2"

members = ["scraper"]
members = ["scraper", "scraper_proc_macros"]

[workspace.dependencies]
scraper = { path = "./scraper" }
scraper_proc_macros = { path = "./scraper_proc_macros" }
2 changes: 1 addition & 1 deletion scraper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "README.md"

[dependencies]
cssparser = "0.31.0"
ego-tree = "0.8.0"
ego-tree = "0.9.0"
html5ever = "0.27"
selectors = "0.25.0"
tendril = "0.4.3"
Expand Down
13 changes: 13 additions & 0 deletions scraper_proc_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "scraper_proc_macros"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0.86"
quote = "1.0.37"
scraper = { workspace = true }
syn = "2.0.76"
21 changes: 21 additions & 0 deletions scraper_proc_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, LitStr};

#[proc_macro]
pub fn selector(input: TokenStream) -> TokenStream {
let selector = parse_macro_input!(input as LitStr);

match scraper::Selector::parse(&selector.value()) {
Ok(_) => quote!(
::scraper::Selector::parse(#selector).unwrap()
)
.into(),
Err(e) => syn::Error::new(
proc_macro2::Span::call_site(),
format!("Failed to parse CSS selector: {}", e),
)
.to_compile_error()
.into(),
}
}
Loading