Derive macro to generate boilerplate on unit variants enum types.
The macro generates the following methods:
as_str- returns a string representation of the targetenumvariant;as_str_abbr- returns an abbreviated string representation of the targetenumvariant;iter_variants- returns an iterator over targetenumvariants (owned values);iter_variants_as_str- returns an iterator over string representations of the targetenumvariants (&'static strvalues);iter_variants_as_str_abbr- returns an iterator over abbreviated string representations of theenumvariants (&'static strvalues);variants_list_str- returns a list of quoted (double-quotes) and comma separated string representations of theenumvariants;variants_list_str_abbr- returns a list of of quoted (double-quotes) and comma separated abbreviated string representation of theenumvariants.
Enum level attributes
The macro exposes the following enum outer attributes (i.e. attributes to
be applied to the enum type the macro is being derived on):
rename- customizes the string representation of each variant;rename_abbr- customizes the abbreviated string representation of each variant;display- automatically implements theDisplaytrait for the target enum using the string representation provided by the generatedas_strmethod.
Valid rename and rename_abbr customization strategies are:
uppercase- makes the (abbreviated) string representation uppercase;lowercase- makes the (abbreviated) string representation lowercase.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Variant level attributes
The macro exposes the following variant attributes:
skip- excludes the marked variant from iteration and listing;rename- customizes the string representation of the marked variant;rename_abbr- customizes the abbreviated string representation of the marked variant.
Valid rename and rename_abbr customization strategies are:
"..."(string literal) - overrides the string representation with a custom string;uppercase- makes the (abbreviated) string representation uppercase;lowercase- makes the (abbreviated) string representation lowercase.
For custom string overrides:
#[variants(rename = "...")]is equivalent to#[variants(rename("..."))];#[variants(rename_abbr = "...")]is equivalent to#[variants(rename_abbr("..."))];
both are valid, supported formats.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
String representation renaming priority
To produce string representations of enum variants, renaming can be applied at both the type level and variant level. The string representation of each variant is obtained by applying rename strategies following a priority-based fallback approach:
- Variant-level attribute (highest priority) - usese the string
produced by the rename strategy from the
#[variants(rename(...))]attribute, if one has been specified for the variant; - Type-level attribute (fallback) - uses the string produced by the
rename strategy from the
#[variants(rename(...))]attribute, if one has been specified for the type; - No renaming (default) - converts the variant identifier to a string if neither the type-level nor the variant-level rename attribute has been specified.
Abbreviated string representation renaming priority
To produce abbreviated string representations of the enum variants, renaming can be applied at both the type level and the variant level. The abbreviated string representation of each variant is obtained by applying rename strategies following a priority-based fallback approach:
- Variant-level attribute (highest priority) - uses the abbreviated
string produced by the rename strategy from the
#[variants(rename_abbr(...))]attribute, if one has been specified for the variant; - Type-level attribute (fallback) - uses the string produced by the
rename strategy from the
#[variants(rename(...))]attribute, if one has been specified for the type; - No renaming (default) - abbreviates the full length string representation of the variant as is, without applying any renaming strategy.
Likewise, the renaming follows a priority-based fallback approach to determine the full length string representation before applying the abbreviation:
- Variant-level attribute (highest priority) - uses the string
produced by the rename strategy from the
#[variants(rename(...))]attribute, if one has been specified for the type; - Type-level attribute (fallback) - uses the string produced by the
rename strategy from the
#[variants(rename(...))]attribute, if one has been specified for the type; - No renaming (default) - converts the variant identifier to a string if neither the type-level nor the variant-level rename attribute has been specified.
Errors
The macro will produce a compile error if:
- derived on
structtypes; - derived on
uniontypes; - derived on
enumtypes with any named field variants; - derived on
enumtypes with any unnamed field (i.e. tuple) variants; - derived on
enumtypes with any newtype variants; - the
renamevariant-level attribute is passed any other value than a string literal,uppercaseorlowercase; - the
rename_abbrvariant-level attribute is passed any other value than a string literal,uppercaseorlowercase; - the
renametype-level attribute is passed any other value thanuppercaseorlowercase; - the
rename_abbrtype-level attribute is passed any other value thanuppercaseorlowercase.
Notes
Deriving Variants on type automatically implements Clone and
Copy for such type. This means that deriving either trait on a type that
also derives Variants will result in a "conflicting implementations"
compilation error.
Examples
// Monday has been marked as `skip`, iterator will yield 6 values.
assert_eq!;
assert_eq!;
assert_eq!;
// The enum has been marked as `display`, so `std::fmt::Display` implementation is available.
assert_eq!;
assert_eq!;
let mut weekdays = iter_variants;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
let mut weekdays_as_str = iter_variants_as_str;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
let mut weekdays_as_str_abbr = iter_variants_as_str_abbr;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;