#[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:
Generates variables like
#[template(rename = "UserData")] struct User { // fields... }UserData.field_nameinstead ofUser.field_name
§#[template(case = "case_type")]
- Purpose: Sets the naming case for the struct name
- Valid values:
Case Example Conversion 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:
Generates variables with the struct name converted to PascalCase
#[template(case = "pascal")] struct user_settings { // fields... }
§#[template(case_fields = "case_type")]
- Purpose: Sets default naming case for all fields
- Valid values: Same as
caseattribute - Overrides: Can be overridden by field-level case attributes
- Default:
snake_case - Example:
Generates variable name
#[template(case_fields = "scream")] struct Config { max_size: usize, }Config.MAX_SIZE
§Field-Level Attributes
These attributes are applied to individual struct fields:
§#[template(ignore)]
- Purpose: Excludes the field from variable generation
- Example:
Only generates variable for
struct Product { id: u64, #[template(ignore)] internal_code: String, }id
§#[template(rename = "name")] or #[template(alias = "name")]
- Purpose: Renames the field in the generated variable
- Example:
Generates variable name
struct Point { #[template(rename = "x-coord")] x: f64, }Point.x-coord
§#[template(case = "case_type")]
- Purpose: Sets naming case for this specific field
- Overrides: The struct-level
case_fieldssetting - Example:
Generates variable name
struct Settings { #[template(case = "kebab")] max_file_size: usize, }Settings.max-file-size
§Generated Implementation
The macro generates an implementation of the Variable trait that provides:
- A
variables()method returning an iterator of(Cow<str>, String)tuples - Proper case conversion according to the specified attributes
- 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-nameUserProfile.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