Expand description
§Documentation for Additional Attributes
§Attributes on Enums
Strum supports several custom attributes to modify the generated code. At the enum level, the following attributes are supported:
-
#[strum(serialize_all = "case_style")]attribute can be used to change the case used when serializing to and deserializing from strings. This feature is enabled by withoutboats/heck and supported case styles are:camelCasePascalCasekebab-casesnake_caseSCREAMING_SNAKE_CASESCREAMING-KEBAB-CASElowercaseUPPERCASEtitle_casemixed_caseTrain-Case
use strum_macros; #[derive(Debug, Eq, PartialEq, strum_macros::Display)] #[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() ); -
You can also apply the
#[strum(ascii_case_insensitive)]attribute to the enum, and this has the same effect of applying it to every variant.
§Attributes on Variants
Custom attributes are applied to a variant by adding #[strum(parameter="value")] to the variant.
-
serialize="...": Changes the text thatFromStr()looks for when parsing a string. This attribute can be applied multiple times to an element and the enum variant will be parsed if any of them match. -
to_string="...": Similar toserialize. This value will be included when usingFromStr(). More importantly, this specifies what text to use when callingvariant.to_string()with theDisplayderivation, or when callingvariant.as_ref()withAsRefStr. -
default: Applied to a single variant of an enum. The variant must be a Tuple-like variant with a single piece of data that can be create from a&stri.e.T: From<&str>. The generated code will now return the variant with the input string captured as shown below instead of failing.// Replaces this: _ => Err(strum::ParseError::VariantNotFound) // With this in generated code: default => Ok(Variant(default.into()))The plugin will fail if the data doesn’t implement From<&str>. You can only have one
defaulton your enum. -
transparent: Signals that the inner field’s implementation should be used, instead of generating one for this variant. Only applicable to enum variants with a single field. Compatible with theAsRefStr,DisplayandIntoStaticStrderive macros. Note thatIntoStaticStrhas a few restrictions, the value must be'staticandconst_into_stris not supported in combination withtransparentb/c transparent relies on a call onFrom::from(variant). -
disabled: removes variant from generated code. -
ascii_case_insensitive: makes the comparison to this variant case insensitive (ASCII only). If the whole enum is markedascii_case_insensitive, you can specifyascii_case_insensitive = falseto disable case insensitivity on this variant. -
message="..": Adds a message to enum variant. This is used in conjunction with theEnumMessagetrait to associate a message with a variant. Ifdetailed_messageis not provided, thenmessagewill also be returned whenget_detailed_messageis called. -
detailed_message="..": Adds a more detailed message to a variant. If this value is omitted, thenmessagewill be used in it’s place. -
Structured documentation, as in
/// ...: If usingEnumMessage, is accessible via get_documentation(). -
props(key="value"): Enables associating additional information with a given variant.