Expand description
§gratte (a strum fork)
gratte is a fork of strum. It defines a set of
macros and traits for working with enums and strings easier in Rust.
The full version of the README can be found on GitHub.
§Installing
Add gratte to your dependencies:
[dependencies]
gratte = "1.1.0"or by running:
cargo add gratte§Usage
gratte has the following derive macros:
| Macro | Description |
|---|---|
| EnumString | Converts strings to enum variants based on their name. |
| Display | Converts enum variants to strings |
| FromRepr | Convert from an integer to an enum. |
| AsRefStr | Implement AsRef<str> for MyEnum |
| IntoStaticStr | Implements From<MyEnum> for &'static str on an enum |
| EnumIter | Creates a new type that iterates the variants of an enum. |
| EnumProperty | Add custom properties to enum variants. |
| EnumMessage | Add a verbose message to enum variants. |
| EnumDiscriminants | Generate a new type with only the discriminant names. |
| EnumCount | Add a constant usize equal to the number of variants. |
| VariantArray | Adds an associated VARIANTS constant which is an array of all enum discriminants |
| VariantNames | Adds an associated VARIANTS constant which is an array of discriminant names |
| EnumTable | Experimental, creates a new type that stores an item of a specified type for each variant of the enum. |
Modules§
- additional_
attributes - Documentation for Additional Attributes
Enums§
- Parse
Error - The
ParseErrorenum is a collection of all the possible reasons an enum can fail to parse from a string.
Traits§
- AsStatic
Ref Deprecated - A cheap reference-to-reference conversion. Used to convert a value to a
reference value with
'staticlifetime within generic code. - Enum
Count - A trait for capturing the number of variants in Enum. This trait can be autoderived by
gratte_macros. - Enum
Message - Associates additional pieces of information with an Enum. This can be
autoimplemented by deriving
EnumMessageand annotating your variants with#[strum(message="...")]. - Enum
Property EnumPropertyis a trait that makes it possible to store additional information with enum variants. This trait is designed to be used with the macro of the same name in thegratte_macroscrate. Currently, the string, integer and bool literals are supported in attributes.- Into
Discriminant - A trait for retrieving the enum generated by
EnumDiscriminantsfrom an associated Type on the original enumeration. This trait can be autoderived bygratte_macros. - Into
Enum Iterator - This trait designates that an
Enumcan be iterated over. It can be auto generated using theEnumIterderive macro. - Variant
Array - A trait for retrieving a static array containing all the variants in an Enum.
This trait can be autoderived by
gratte_macros. For derived usage, all the variants in the enumerator need to be unit-types, which means you can’t autoderive enums with inner data in one or more variants. Consider using it alongsideEnumDiscriminantsif you require inner data but still want to have an static array of variants. - Variant
Iterator - Variant
Metadata - Variant
Names - A trait for retrieving the names of each variant in Enum. This trait can
be autoderived by
gratte_macros.
Derive Macros§
- AsRef
Str derive - Converts enum variants to
&'a str, where'ais the lifetime of the input enum reference. - Display
derive - Converts enum variants to strings.
- Enum
Count derive - Add a constant
usizeequal to the number of variants. - Enum
Discriminants derive - Generate a new type with only the discriminant names.
- EnumIs
derive - Generated
is_*()methods for each variant. E.g.Color.is_red(). - Enum
Iter derive - Creates a new type that iterates over the variants of an enum.
- Enum
Message derive - Add a verbose message to an enum variant.
- Enum
Property derive - Add custom properties to enum variants.
- Enum
String derive - Converts strings to enum variants based on their name.
- Enum
TryAs derive - Generated
try_as_*()methods for all tuple-style variants. E.g.Message.try_as_write(). - From
Repr derive - Add a function to enum that allows accessing variants by its discriminant
- Into
Static Str derive - Implements
From<MyEnum> for &'static stron an enum. - Variant
Array derive - Adds a
'staticslice with all the Enum’s variants. - Variant
Names derive - Implements
gratte::VariantNameswhich adds an associated constantVARIANTSwhich is a'staticslice of discriminant names.