Skip to main content

EnumName

Derive Macro EnumName 

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

Derives methods to return the string representation of an enum variant.

This macro implements two const methods on the enum:

  • name(&self) -> &'static str: Returns the formatted variant name, taking into account any container rename_all casing transformation and variant-level rename overrides.
  • raw_name(&self) -> &'static str: Returns the exact Rust identifier name as written in code.

§Attributes

  • #[enuminfo(rename_all = "...")]: Container attribute that transforms output strings using serde-compatible casing names (e.g. "snake_case", "kebab-case", "camelCase").
  • #[enuminfo(rename = "custom_name")]: Variant attribute that overrides the output string for a specific variant.

§Compile-Time Checks

Validates at compile time that all variant names (after applying rename_all and rename) are unique. If a duplicate name is detected, compilation fails with an error.

§Compatibility & Limitations

  • Works on enum types only.
  • Supports all enum variant types: unit variants, tuple variants, and struct variants.

§Feature Flags

If the impl-enuminfo feature is enabled, this macro additionally implements the enuminfo::EnumName trait for the enum.

§Example

use enuminfo_macros::EnumName;

#[derive(EnumName)]
#[enuminfo(rename_all = "snake_case")]
enum Status {
    Pending,
    #[enuminfo(rename = "in_progress_custom")]
    InProgress,
    Completed,
}

let status = Status::Pending;
assert_eq!(status.name(), "pending");
assert_eq!(status.raw_name(), "Pending");

let in_progress = Status::InProgress;
assert_eq!(in_progress.name(), "in_progress_custom");
assert_eq!(in_progress.raw_name(), "InProgress");