rust-enum-str-derive
A procedural macro derive allowing for automatically implementing AsRef<str>
for enums.
Purpose
Automate the tedious process of stringifying enum variants where needed.
How to use
Procedural macros are not fully standardized as of September 2017, but sufficient features are available in the current rust nightly version (1.22). See the RFC and the tracking issue for more information.
Therefore, to create procedural macros and use this crate, you need Nightly:
rustup default nightly
Add this to your Cargo.toml
file:
[dependencies]
enum-str-derive = { git = "https://github.com/victorporof/rust-enum-str-derive.git" }
Then, simply import the library into your code and derive the EnumStr
trait on your data structures.
Available derives:
EnumStr
EnumStrCamelCase
EnumStrKebabCase
EnumStrMixedCase
EnumStrShoutySnakeCase
EnumStrSnakeCase
EnumStrTitleCase
#![feature(proc_macro)]
extern crate enum_str_derive;
use enum_str_derive::{EnumStrSnakeCase};
#[derive(EnumStrCamelCase)]
enum Data {
Foo,
BarBaz
}
assert_eq!(Data::Foo.as_ref(), "foo");
assert_eq!(Data::BarBaz.as_ref(), "bar-baz");