Variable

Derive Macro Variable 

Source
#[derive(Variable)]
{
    // Attributes available to this derive:
    #[template]
}
Available on crate feature macros only.
Expand description

§Variable Derive Macro Documentation

This procedural macro generates an implementation of the Variable trait for structs, enabling conversion of struct fields into key-value pairs with various naming and casing options.

§Struct-Level Attributes

These attributes are applied to the struct definition:

§#[template(rename = "name")] or #[template(alias = "name")]

  • Purpose: Renames the struct in the generated variable names
  • Example:
    #[template(rename = "UserData")]
    struct User {
        // fields...
    }
    Generates variables like UserData.field_name instead of User.field_name

§#[template(case = "case_type")]

  • Purpose: Sets the naming case for the struct name
  • Valid values:
    CaseExampleConversion Method
    Lowerhello worldto_lowercase()
    UpperHELLO WORLDto_uppercase()
    Snakehello_worldLowercase with underscores
    CamelhelloWorldFirst word lowercase, others capitalized
    PascalHelloWorldAll words capitalized
    Kebabhello-worldLowercase with hyphens
    ScreamingSnakeHELLO_WORLDUppercase with underscores
  • Example:
    #[template(case = "pascal")]
    struct user_settings {
        // fields...
    }
    Generates variables with the struct name converted to PascalCase

§#[template(case_fields = "case_type")]

  • Purpose: Sets default naming case for all fields
  • Valid values: Same as case attribute
  • Overrides: Can be overridden by field-level case attributes
  • Default: snake_case
  • Example:
    #[template(case_fields = "scream")]
    struct Config {
        max_size: usize,
    }
    Generates variable name Config.MAX_SIZE

§Field-Level Attributes

These attributes are applied to individual struct fields:

§#[template(ignore)]

  • Purpose: Excludes the field from variable generation
  • Example:
    struct Product {
        id: u64,
        #[template(ignore)]
        internal_code: String,
    }
    Only generates variable for id

§#[template(rename = "name")] or #[template(alias = "name")]

  • Purpose: Renames the field in the generated variable
  • Example:
    struct Point {
        #[template(rename = "x-coord")]
        x: f64,
    }
    Generates variable name Point.x-coord

§#[template(case = "case_type")]

  • Purpose: Sets naming case for this specific field
  • Overrides: The struct-level case_fields setting
  • Example:
    struct Settings {
        #[template(case = "kebab")]
        max_file_size: usize,
    }
    Generates variable name Settings.max-file-size

§Generated Implementation

The macro generates an implementation of the Variable trait that provides:

  1. A variables() method returning an iterator of (Cow<str>, String) tuples
  2. Proper case conversion according to the specified attributes
  3. Field filtering for ignored fields

§Example Output

For this input:

#[template(case = "pascal", case_fields = "kebab")]
struct UserProfile {
    #[template(rename = "first-name")]
    first_name: String,
    #[template(ignore)]
    password: String,
    age: u8,
}

The generated implementation would produce variables:

  • UserProfile.first-name
  • UserProfile.age

§Supported Field Types

The macro works with:

  • Named fields (standard structs)
  • Tuple structs (fields accessed by index)
  • Unit structs (treated as a single value)

§Error Handling

The macro will error if:

  • Applied to non-struct types (enums, unions, etc.)
  • Invalid case type is specified
  • Attribute syntax is malformed