lib.rs

Overview

This file implements a procedural macro named Setters that automatically generates setter methods for struct fields in Rust. It provides extensive customization options through a set of attributes that control how setters are generated, their naming, behavior, and integration with delegation patterns. The macro supports both public and private fields, different ownership models for self, and can optionally generate tracing calls or custom post-processing logic.

The core functionality revolves around parsing attributes on structs and fields, constructing internal representations of configuration (ContainerDef and FieldDef), and emitting Rust code that defines the setter methods accordingly. The file uses the darling crate for attribute parsing, and syn and quote for syntactic processing and code generation.

Key Components

Structs and Attribute Parsing

ExternalDelegate

ContainerAttrs

FieldAttrs

Internal Representation

ContainerDef

FieldDef

Functions

error(span: Span, data: &str) -> SynTokenStream

init_container_def(input: &DeriveInput) -> Result<ContainerDef, SynTokenStream>

init_field_def(container: &ContainerDef, field: &Field) -> Result<Option<FieldDef>, SynTokenStream>

generate_setter_method(input: &DeriveInput, container: &ContainerDef, def: FieldDef, additional_prefix: &Option<String>, delegate_toks: &Option<SynTokenStream>) -> Result<SynTokenStream, SynTokenStream>

generate_setters_for(input: &DeriveInput, data: &DataStruct, generics: &Generics, ty: SynTokenStream, additional_prefix: Option<String>, delegate_toks: Option<SynTokenStream>) -> Result<SynTokenStream, SynTokenStream>

generate_setters(input: &DeriveInput, data: &DataStruct) -> Result<TokenStream, TokenStream>

derive_setters(input: TokenStream) -> TokenStream

Implementation Details and Algorithms

Interaction with Other Parts of the Application

Usage Examples

Basic Usage

#[derive(Setters)]
struct MyStruct {
    field1: String,
    field2: Option<i32>,
}

Custom Prefix and Delegation

#[derive(Setters)]
#[setters(prefix = "set_")]
struct Config {
    value: i64,
    #[setters(skip)]
    internal: String,
    
    #[setters(rename = "custom_set")]
    renamed_field: bool,
    
    #[setters(generate_delegates(
        ty = "OtherType",
        field = "delegate_field",
        prefix = "delegate_",
    ))]
    delegate_field: DelegateType,
}

Visual Diagram

flowchart TD
A[derive_setters] --> B[generate_setters]
B --> C[init_container_def]
B --> D["generate_setters_for (main struct)"]
D --> E["init_field_def (for each field)"]
E --> F[generate_setter_method]
B --> G["generate_setters_for (delegates)"]
G --> H["generate_setter_method (with delegation)"]

This file encapsulates the logic and structure to generate efficient, customizable setter methods for Rust structs, supporting complex attribute-driven configuration and delegation patterns. For further details on attribute macros and procedural macro development, refer to the relevant topics on Procedural Macros and Attribute Parsing.