#proc-macro #string #enums

no-std gratte

Helpful macros for working with enums and strings; forked from strum

3 releases (stable)

new 1.0.1 Apr 2, 2025
1.0.0 Mar 30, 2025
0.1.0 Mar 29, 2025

#132 in Procedural macros

Download history 190/week @ 2025-03-25

190 downloads per month

MIT license

27KB
172 lines

gratte (a strum fork)

CI Security audit crates.io downloads docs.rs Contributor Covenant

gratte is a fork of strum. It defines a set of macros and traits for working with enums and strings easier in Rust.

Installing

Add gratte to your dependencies:

[dependencies]
gratte = "1.0.1"

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.

Debugging

To see the generated code, set the STRUM_DEBUG environment variable before compiling your code. STRUM_DEBUG=1 will dump all the generated code for every type. STRUM_DEBUG=YourType will only dump the code generated on a type named YourType.

Differences from strum

  • EnumDiscriminants now supports custom attributes on the discriminants enum in two new formats:
    • Path only (ex: #[strum_discriminants(non_exhaustive)])
    • Name/value (ex: #[strum_discriminants(doc = "foo")])

Questions? Comments?

For instructions on filing bug reports or feature requests and contributing to the project, see CONTRIBUTING.

Minimum Rust version

gratte currently builds on Rust 1.66.1 or newer.

gratte?

gratte is the French translation of strum.

(For more information on the original name, see the project page. 🙂)

Dependencies

~170KB