Skip to main content

Crate enuminfo_macros

Crate enuminfo_macros 

Source
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: Generates const methods (name() and raw_name()) returning variant names as string slices.
  • EnumFromName: Generates lookup constructors (from_name() and from_raw_name()) to instantiate unit enum variants from string names (with optional FromStr support).
  • EnumVariants: Generates an associated constant array slice (VARIANTS) and helper methods (variants() and variant_count()) containing all unit variants.

§Attributes Reference

The following attributes can be placed on enums or their variants under the #[enuminfo(...)] helper attribute:

AttributeTargetSupported MacrosDescription
#[enuminfo(rename_all = "...")]EnumEnumName, EnumFromName, EnumVariantsApplies a casing convention to variant names (e.g. "snake_case").
#[enuminfo(rename = "...")]VariantEnumName, EnumFromName, EnumVariantsOverrides the formatted name for an individual variant.
#[enuminfo(ignore_from_name)]VariantEnumFromNameExcludes the variant from being constructed by from_name(), from_raw_name(), or FromStr.
#[enuminfo(ignore_variant)]VariantEnumVariantsExcludes the variant from the generated VARIANTS slice and variant_count().

Note: #[enuminfo(rename = "...")] and #[enuminfo(rename_all = "...")] are accepted by EnumVariants for attribute compatibility when used alongside EnumName or EnumFromName, but they do not affect variant instances in VARIANTS.

§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:

OptionConversion TypeExample (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 of core::str::FromStr for enums deriving EnumFromName.
  • impl-enuminfo: Implements companion traits from the enuminfo crate (EnumIs, EnumName, EnumFromName, EnumVariants).
  • skip-inherent: Skips generating inherent impl blocks on the enum, generating only trait implementations when combined with impl-enuminfo.

Derive Macros§

EnumFromName
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.
EnumName
Derives methods to return the string representation of an enum variant.
EnumVariants
Derives a static slice and enumeration helpers containing all variants of a unit enum.