spider_macro/lib.rs
1//! Procedural macro for `spider-lib` to simplify `ScrapedItem` implementation.
2//!
3//! This module provides the `#[scraped_item]` attribute macro, which automatically
4//! derives the necessary traits and implementations for a struct to be used as
5//! a `ScrapedItem` within the `spider-lib` framework.
6//!
7//! By applying this macro to a struct, users can easily define their data
8//! structures for scraped items, reducing boilerplate code for serialization,
9//! deserialization, cloning, and type conversions required by the library's
10//! item processing pipeline.
11
12extern crate proc_macro;
13
14use proc_macro::TokenStream;
15use quote::quote;
16use syn::{parse_macro_input, ItemStruct};
17
18/// A procedural macro to derive the `ScrapedItem` trait.
19#[proc_macro_attribute]
20pub fn scraped_item(_attr: TokenStream, item: TokenStream) -> TokenStream {
21 let ast = parse_macro_input!(item as ItemStruct);
22 let name = &ast.ident;
23
24 let expanded = quote! {
25 #[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
26 #ast
27
28 impl ScrapedItem for #name {
29 fn as_any(&self) -> &dyn ::std::any::Any {
30 self
31 }
32
33 fn box_clone(&self) -> Box<dyn ScrapedItem + Send + Sync> {
34 Box::new(self.clone())
35 }
36
37 fn to_json_value(&self) -> ::serde_json::Value {
38 ::serde_json::to_value(self).unwrap()
39 }
40 }
41 };
42
43 TokenStream::from(expanded)
44}
45