gratte

Crates.iogratte
lib.rsgratte
version1.2.0
created_at2025-03-29 22:55:50.101717+00
updated_at2025-07-27 13:14:56.539833+00
descriptionHelpful macros for working with enums and strings; forked from strum
homepagehttps://github.com/clechasseur/gratte#readme
repositoryhttps://github.com/clechasseur/gratte
max_upload_size
id1611809
size27,463
Charles Lechasseur (clechasseur)

documentation

https://docs.rs/gratte/

README

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.

Should I use this?

Good question.

My motivation when creating this fork was to be able to add new features more quickly. It was also a good opportunity to learn more about proc macros in Rust.

However, fragmentation can be dangerous. Ultimately, I would like for gratte's new features to be backported to strum. I will try to create PRs for this to happen. However, I cannot guarantee that strum's maintainer will accept them.

Unless you have a specific need for something new in gratte, I would probably stick with strum. But if you need one of gratte's new features, then by all means use this 🙂

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

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

  • The derive feature of the gratte crate is enabled by default.
  • All code generated by the derive macros now bear the #[automatically_derived] attribute.
    • This has no bearing on the code, but will correctly exclude such generated code from code coverage, for example.
    • strum has this for some of the code but not all (for now).
  • 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. 🙂)

Commit count: 365

cargo fmt