Skip to main content

Crate spider_macro

Crate spider_macro 

Source
Expand description

§spider-macro

Provides procedural macros for the spider-lib framework to reduce boilerplate code.

§Overview

The spider-macro crate contains procedural macros that automate the implementation of common traits and patterns used in the spider framework. These macros significantly reduce the amount of boilerplate code required when defining custom data structures for scraped items and spider state.

§Key Macros

  • #[scraped_item]: Derives the ScrapedItem trait along with necessary implementations for serialization, deserialization, cloning, and type conversions.

§Features

  • Automatic Trait Derivation: Implements Serialize, Deserialize, Clone, and Debug traits automatically
  • ScrapedItem Implementation: Provides the complete implementation of the ScrapedItem trait required by the framework
  • Type Safety: Maintains type safety while reducing boilerplate
  • Performance: Generates efficient code without runtime overhead

§Dependencies

When using this macro, your project must include the following dependencies:

[dependencies]
spider-lib = "1.1.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

§Example

use spider_lib::prelude::*;

#[scraped_item]
struct Article {
    title: String,
    content: String,
    author: String,
    published_date: String,
}

// The macro generates all necessary implementations automatically
// including serialization, deserialization, and the ScrapedItem trait
//
// Note: Make sure your Cargo.toml includes serde and serde_json as dependencies

Attribute Macros§

scraped_item
A procedural macro to derive the ScrapedItem trait.