Expand description
A collection of procedural derive macros providing inspection, string conversion, lookup, and variant enumeration utilities for Rust enums.
§Overview
This crate provides four derive macros:
EnumIs: Generates boolean checker methods (is_<variant>()) for enum variants.EnumName: Generatesconstmethods (name()andraw_name()) returning variant names as string slices.EnumFromName: Generates lookup constructors (from_name()andfrom_raw_name()) to instantiate unit enum variants from string names (with optionalFromStrsupport).EnumVariants: Generates an associated constant array slice (VARIANTS) and helper methods (variants()andvariant_count()) containing all unit variants.
§Attributes Reference
The following attributes can be placed on enums or their variants under the #[enuminfo(...)] helper attribute:
| Attribute | Target | Supported Macros | Description |
|---|---|---|---|
#[enuminfo(rename_all = "...")] | Enum | EnumName, EnumFromName, EnumVariants | Applies a casing convention to variant names (e.g. "snake_case"). |
#[enuminfo(rename = "...")] | Variant | EnumName, EnumFromName, EnumVariants | Overrides the formatted name for an individual variant. |
#[enuminfo(ignore_from_name)] | Variant | EnumFromName | Excludes the variant from being constructed by from_name(), from_raw_name(), or FromStr. |
#[enuminfo(ignore_variant)] | Variant | EnumVariants | Excludes the variant from the generated VARIANTS slice and variant_count(). |
Note:
#[enuminfo(rename = "...")]and#[enuminfo(rename_all = "...")]are accepted byEnumVariantsfor attribute compatibility when used alongsideEnumNameorEnumFromName, but they do not affect variant instances inVARIANTS.
§Case Conversions (rename_all)
The #[enuminfo(rename_all = "...")] container attribute modifies how variant names are matched or returned.
Supported casing conventions match the standard conventions used by serde:
| Option | Conversion Type | Example (MyVariant) |
|---|---|---|
"lowercase" | All lower case | "myvariant" |
"UPPERCASE" | All upper case | "MYVARIANT" |
"camelCase" / "lowerCamelCase" | Lower camel case | "myVariant" |
"snake_case" | Snake case | "my_variant" |
"SCREAMING-SNAKE-CASE" | Screaming snake case | "MY_SNAKE_CASE" |
"kebab-case" | Kebab case | "my-variant" |
"SCREAMING-KEBAB-CASE" | Screaming kebab case | "MY-KEBAB-CASE" |
"PascalCase" / "UpperCamelCase" | Pascal / Upper camel case | "MyVariant" |
If an unrecognized casing string is provided, variant names remain unmodified.
§Feature Flags
This crate provides several Cargo feature flags:
from-str(enabled by default): Generates an implementation ofcore::str::FromStrfor enums derivingEnumFromName.impl-enuminfo: Implements companion traits from theenuminfocrate (EnumIs,EnumName,EnumFromName,EnumVariants).skip-inherent: Skips generating inherentimplblocks on the enum, generating only trait implementations when combined withimpl-enuminfo.
Derive Macros§
- Enum
From Name - Derives constructor lookup methods to instantiate unit enum variants from string names.
- EnumIs
- Derives helper methods to check if an enum instance matches a specific variant.
- Enum
Name - Derives methods to return the string representation of an enum variant.
- Enum
Variants - Derives a static slice and enumeration helpers containing all variants of a unit enum.