From 10c635f1673ba319257d3cbf1a71c6ff09bed62a Mon Sep 17 00:00:00 2001 From: LoZack19 Date: Wed, 28 Aug 2024 17:01:47 +0200 Subject: [PATCH 1/4] add scraper_proc_macros package --- Cargo.lock | 10 ++++++++++ Cargo.toml | 5 ++++- scraper_proc_macros/Cargo.toml | 13 +++++++++++++ scraper_proc_macros/src/lib.rs | 18 ++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 scraper_proc_macros/Cargo.toml create mode 100644 scraper_proc_macros/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index c9054e5e..1b30520b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -434,6 +434,16 @@ dependencies = [ "tendril", ] +[[package]] +name = "scraper_proc_macros" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "scraper", + "syn", +] + [[package]] name = "selectors" version = "0.25.0" diff --git a/Cargo.toml b/Cargo.toml index cbece98a..1020d0f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,7 @@ [workspace] resolver = "2" -members = ["scraper"] +members = ["scraper", "scraper_proc_macros"] + +[workspace.dependencies] +scraper = { path = "./scraper" } \ No newline at end of file diff --git a/scraper_proc_macros/Cargo.toml b/scraper_proc_macros/Cargo.toml new file mode 100644 index 00000000..aeac45ce --- /dev/null +++ b/scraper_proc_macros/Cargo.toml @@ -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" diff --git a/scraper_proc_macros/src/lib.rs b/scraper_proc_macros/src/lib.rs new file mode 100644 index 00000000..b1f1d634 --- /dev/null +++ b/scraper_proc_macros/src/lib.rs @@ -0,0 +1,18 @@ +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).value(); + + match scraper::Selector::parse(&selector) { + 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(), + } +} \ No newline at end of file From 247edfebb55875a257519d5d0c4e2cf7afcc1d8c Mon Sep 17 00:00:00 2001 From: LoZack19 Date: Fri, 30 Aug 2024 11:36:58 +0200 Subject: [PATCH 2/4] implement scraper_proc_macros as independent crate --- Cargo.toml | 3 ++- scraper/Cargo.toml | 1 + scraper_proc_macros/src/lib.rs | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1020d0f1..f6b9bc2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ resolver = "2" members = ["scraper", "scraper_proc_macros"] [workspace.dependencies] -scraper = { path = "./scraper" } \ No newline at end of file +scraper = { path = "./scraper" } +scraper_proc_macros = { path = "./scraper_proc_macros" } \ No newline at end of file diff --git a/scraper/Cargo.toml b/scraper/Cargo.toml index 6d1b5414..4fc8e807 100644 --- a/scraper/Cargo.toml +++ b/scraper/Cargo.toml @@ -31,6 +31,7 @@ deterministic = ["indexmap"] main = ["getopts"] atomic = [] errors = [] +macros = [] [[bin]] name = "scraper" diff --git a/scraper_proc_macros/src/lib.rs b/scraper_proc_macros/src/lib.rs index b1f1d634..4b3d8cb7 100644 --- a/scraper_proc_macros/src/lib.rs +++ b/scraper_proc_macros/src/lib.rs @@ -4,9 +4,9 @@ use syn::{parse_macro_input, LitStr}; #[proc_macro] pub fn selector(input: TokenStream) -> TokenStream { - let selector = parse_macro_input!(input as LitStr).value(); + let selector = parse_macro_input!(input as LitStr); - match scraper::Selector::parse(&selector) { + match scraper::Selector::parse(&selector.value()) { Ok(_) => quote!( ::scraper::Selector::parse(#selector).unwrap() ).into(), From 3dd3c2372c2dc698ccb7a997b2d736ddec3e7eba Mon Sep 17 00:00:00 2001 From: LoZack19 Date: Fri, 30 Aug 2024 11:53:01 +0200 Subject: [PATCH 3/4] fix formatting with rustfmt --- .gitignore | 1 + scraper/Cargo.toml | 1 - scraper_proc_macros/src/lib.rs | 13 ++++++++----- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index cc563fb6..0fda6e1c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target *.bk .idea +.vscode \ No newline at end of file diff --git a/scraper/Cargo.toml b/scraper/Cargo.toml index 4fc8e807..6d1b5414 100644 --- a/scraper/Cargo.toml +++ b/scraper/Cargo.toml @@ -31,7 +31,6 @@ deterministic = ["indexmap"] main = ["getopts"] atomic = [] errors = [] -macros = [] [[bin]] name = "scraper" diff --git a/scraper_proc_macros/src/lib.rs b/scraper_proc_macros/src/lib.rs index 4b3d8cb7..43b0ebd2 100644 --- a/scraper_proc_macros/src/lib.rs +++ b/scraper_proc_macros/src/lib.rs @@ -5,14 +5,17 @@ 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(), + ) + .into(), Err(e) => syn::Error::new( proc_macro2::Span::call_site(), - format!("Failed to parse CSS selector: {}", e) - ).to_compile_error().into(), + format!("Failed to parse CSS selector: {}", e), + ) + .to_compile_error() + .into(), } -} \ No newline at end of file +} From 5dbecb3b09305728f9ca959b2dadaa3a5efbfd19 Mon Sep 17 00:00:00 2001 From: LoZack19 Date: Sun, 1 Sep 2024 17:26:25 +0200 Subject: [PATCH 4/4] update ego-tree version --- Cargo.lock | 4 ++-- scraper/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1b30520b..a0f4c1ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,9 +90,9 @@ dependencies = [ [[package]] name = "ego-tree" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0a1c26db6cdc3076a84e511b708f85d5c7167a8c65c25fc2279a6315d34777c" +checksum = "7c6ba7d4eec39eaa9ab24d44a0e73a7949a1095a8b3f3abb11eddf27dbb56a53" [[package]] name = "equivalent" diff --git a/scraper/Cargo.toml b/scraper/Cargo.toml index 6d1b5414..b496d526 100644 --- a/scraper/Cargo.toml +++ b/scraper/Cargo.toml @@ -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"