1
0
Fork 0
mirror of https://github.com/clechasseur/gratte.git synced 2025-12-14 06:48:59 +01:00
A small rust library for adding custom derives to enums; forked from strum
renovate[bot] bb129f12fc
chore(deps): update swatinem/rust-cache action to v2.8.2 (#83)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-11-30 22:56:22 -05:00
.devcontainer Made a fix for #80 2020-03-01 23:55:53 +00:00
.github chore(deps): update swatinem/rust-cache action to v2.8.2 (#83) 2025-11-30 22:56:22 -05:00
strum chore: move to version 1.2.0 2025-07-27 01:08:59 -04:00
strum_macros chore: move to version 1.2.0 2025-07-27 01:08:59 -04:00
strum_nostd_tests chore: move to version 1.2.0 2025-07-27 01:08:59 -04:00
strum_tests chore: move to version 1.2.0 2025-07-27 01:08:59 -04:00
.gitignore chore: merge updates from template (#37) 2025-05-27 10:01:36 -04:00
Cargo.lock chore(deps): update rust crate clap to 4.5.53 (#78) 2025-11-24 01:11:05 -05:00
Cargo.toml chore(deps): update rust crate clap to 4.5.53 (#78) 2025-11-24 01:11:05 -05:00
CODE_OF_CONDUCT.md chore: setup project fork 2025-03-25 22:46:46 -04:00
CONTRIBUTING.md chore: setup project fork 2025-03-25 22:46:46 -04:00
DEVELOPMENT.md chore: merge updates from template (#37) 2025-05-27 10:01:36 -04:00
justfile chore: bring updates from template (#77) 2025-11-16 00:16:31 -05:00
LICENSE chore: setup project fork 2025-03-25 22:46:46 -04:00
README.md fix: update README to account for changes synched from strum 2025-07-27 01:09:47 -04:00
renovate.json chore: setup project fork 2025-03-25 22:46:46 -04:00
SECURITY.md chore: sync changes from template (#20) 2025-04-05 19:22:13 -04:00

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. 🙂)