Skip to main content

EnumStr

Derive Macro EnumStr 

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

Generate string conversion for an enum.

Supports unit enums by default. Use #[enum_str(default)] for non-unit enums.

This macro generates inherent items and impls on the enum:

  • const fn as_name(&self) -> &'static str
  • const fn as_aliases(&self) -> &'static [&'static str]
  • const ALL_NAMES: [&'static str; N]
  • const ALL_ALIASES: [&'static str; M]
  • impl From<T> for &'static str
  • impl AsRef<str> for T
  • impl Display for T
  • impl FromStr for T
  • impl TryFrom<&str> for T
  • Error struct for invalid conversion from str

§Container attributes

  • #[enum_str(rename_all = "snake_case")]: rename all variants by rule
  • #[enum_str(alias_all = "lowercase")]: add aliases to all variants by rule (repeatable)
  • #[enum_str(default)]: fills non-unit variant’s fields with default value during parsing
  • #[enum_str(no_rendering)]: skip as_name, as_aliases, ALL_NAMES, ALL_ALIASES, From, AsRef, Display
  • #[enum_str(no_parsing)]: skip FromStr, TryFrom, and the error struct
  • #[enum_str(as_name(name = ..., vis = "...", enable/disable))]: control the as_name method
  • #[enum_str(as_aliases(...))]: control the as_aliases method
  • #[enum_str(all_names(...))]: control the ALL_NAMES constant
  • #[enum_str(all_aliases(...))]: control the ALL_ALIASES constant
  • #[enum_str(impl_into_static_str(enable/disable))]: control impl From<T> for &'static str
  • #[enum_str(impl_as_ref_str(enable/disable))]: control impl AsRef<str> for T
  • #[enum_str(impl_display(enable/disable))]: control impl std::fmt::Display for T
  • #[enum_str(impl_from_str(enable/disable))]: control impl FromStr for T
  • #[enum_str(impl_try_from_str(enable/disable))]: control impl TryFrom<&str> for T
  • #[enum_str(error(name = ..., vis = "...", enable/disable))]: control the error struct (default name Invalid{Enum})
  • #[enum_str(error_msg = "...")]: custom error message template

§Variant attributes

  • #[enum_str(rename = "custom_name")]: override the variant’s name
  • #[enum_str(alias = "alt")]: add an alias (repeatable)
  • #[enum_str(skip)]: skip variant from parsing and ALL_NAMES / ALL_ALIASES

§Available rename rules

  • lowercase
  • UPPERCASE
  • PascalCase
  • camelCase
  • snake_case
  • SCREAMING_SNAKE_CASE
  • kebab-case
  • SCREAMING-KEBAB-CASE

§Available error message template variables

  • {name}: the enum’s type name
  • {input}: the invalid input string (requires allocation)
  • {names}: all variant primary names
  • {aliases}: all names and aliases

List variables accept format modifiers:

  • {names}: default, comma-separated, double-quoted: "Bar", "Baz"
  • {names:|}: custom separator, no quotes: Bar|Baz
  • {names: - :'}: custom separator and quote char: 'Bar' - 'Baz'

Use {{ and }} for literal braces. : cannot be used as a separator or quote character.

§Example

use enum_helper::EnumStr;

#[derive(EnumStr, Debug, PartialEq, Eq)]
#[enum_str(rename_all = "lowercase")]
pub enum Foo {
    Bar,
    #[enum_str(alias = "bazzz")]
    Baz,
}

assert_eq!(Foo::Bar.as_name(), "bar");
assert_eq!("bazzz".parse::<Foo>().unwrap(), Foo::Baz);