#enums #proc-macro #string #macro

macro strum_macros

Helpful macros for working with enums and strings

43 releases (22 breaking)

0.26.4 Jun 5, 2024
0.26.2 Mar 10, 2024
0.25.3 Oct 15, 2023
0.25.1 Jul 3, 2023
0.5.0 Feb 23, 2017

#137 in Procedural macros

Download history 1768845/week @ 2024-07-21 1735907/week @ 2024-07-28 1724462/week @ 2024-08-04 1901947/week @ 2024-08-11 1892711/week @ 2024-08-18 1897797/week @ 2024-08-25 1863219/week @ 2024-09-01 1934129/week @ 2024-09-08 1791189/week @ 2024-09-15 2071386/week @ 2024-09-22 2251207/week @ 2024-09-29 2687767/week @ 2024-10-06 2542392/week @ 2024-10-13 2423687/week @ 2024-10-20 2038926/week @ 2024-10-27 1842384/week @ 2024-11-03

9,123,530 downloads per month
Used in 7,756 crates (1,326 directly)

MIT license

135KB
2.5K SLoC

Strum

Build Status Build status Latest Version Rust Documentation Crates.io Crates.io

Strum is a set of macros and traits for working with enums and strings easier in Rust.

Compatibility

Strum is currently compatible with versions of rustc >= 1.56.1. Pull Requests that improve compatibility with older versions are welcome. The project goal is to support a rust version for at least 2 years after release and even longer is preferred since this project changes slowly.

Including Strum in Your Project

Import strum and strum_macros into your project by adding the following lines to your Cargo.toml. Strum_macros contains the macros needed to derive all the traits in Strum.

[dependencies]
strum = "0.26"
strum_macros = "0.26"

# You can also use the "derive" feature, and import the macros directly from "strum"
# strum = { version = "0.26", features = ["derive"] }

Strum Macros

Strum has implemented the following 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 of the variants of an enum.
EnumProperty Add custom properties to enum variants.
EnumMessage Add a verbose message to an enum variant.
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.

Contributing

Thanks for your interest in contributing. Bug fixes are always welcome. If you are interested in implementing or adding a macro, please open an issue first to discuss the feature. I have limited bandwidth to review new features.

The project is divided into 3 parts, the traits are in the /strum folder. The procedural macros are in the /strum_macros folder, and the integration tests are in /strum_tests. If you are adding additional features to strum or strum_macros, you should make sure to run the tests and add new integration tests to make sure the features work as expected.

Debugging

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

Name

Strum is short for STRing enUM because it's a library for augmenting enums with additional information through strings.

Strumming is also a very whimsical motion, much like writing Rust code.

Dependencies

~280–740KB
~17K SLoC