Generator

Derive Macro Generator 

Source
#[derive(Generator)]
{
    // Attributes available to this derive:
    #[generator]
}
Expand description

Derive macro for automatically implementing the Generator trait

This macro can be applied to structs and enums to automatically generate implementations of the Generator trait.

§Basic Usage

use protest::Generator;

#[derive(Generator)]
struct User {
    id: u32,
    name: String,
    active: bool,
}

§Customization

The derive macro supports various customization attributes:

use protest::Generator;

#[derive(Generator)]
struct CustomUser {
    #[generator(range = "1..1000")]
    id: u32,
    #[generator(length = "5..20")]
    name: String,
    #[generator(custom = "always_true")]
    active: bool,
}

fn always_true() -> bool {
    true
}

§Supported Attributes

  • range = "min..max": For numeric types, specifies the range of generated values
  • length = "min..max": For collections and strings, specifies the length range
  • custom = "function_name": Uses a custom function to generate the field value

§Generic Types

The derive macro supports generic types with appropriate bounds:

use protest::Generator;

#[derive(Generator)]
struct GenericStruct<T, U> {
    first: T,
    second: U,
}

§Enums

The derive macro supports enums with all variant types:

use protest::Generator;

#[derive(Generator)]
enum Status {
    Active,
    Inactive(String),
    Pending { reason: String },
}