Module strum::additional_attributes[][src]

Documentation for Additional Attributes

Strum supports several custom attributes to modify the generated code. At the enum level, the #[strum(serialize_all = "snake_case")] attribute can be used to change the case used when serializing to and deserializing from strings:

use std::string::ToString;
use strum;
use strum_macros;

#[derive(Debug, Eq, PartialEq, strum_macros::ToString)]
#[strum(serialize_all = "snake_case")]
enum Brightness {
    DarkBlack,
    Dim {
        glow: usize,
    },
    #[strum(serialize = "bright")]
    BrightWhite,
}

assert_eq!(
    String::from("dark_black"),
    Brightness::DarkBlack.to_string().as_ref()
);
assert_eq!(
    String::from("dim"),
    Brightness::Dim { glow: 0 }.to_string().as_ref()
);
assert_eq!(
    String::from("bright"),
    Brightness::BrightWhite.to_string().as_ref()
);

Custom attributes are applied to a variant by adding #[strum(parameter="value")] to the variant.