2 releases

0.1.1 Mar 26, 2022
0.1.0 Mar 26, 2022

#2577 in Rust patterns

32 downloads per month

MIT license

5KB

labelled-enum labelled-enum at crates.io labelled-enum at docs.rs

Converting an enum to/from String.

Getting Started

Derives ToString, FromStr impl:

#[derive(ToString, FromStr)]
enum Test {
    Foo,
    Bar,
}

assert_eq!(Test::Foo.to_string(), "Foo");
assert_eq!(Test::from_str("Foo").unwrap(), Test::Foo);

Casing

You can specify snake_case using attribute:

#[derive(ToString, FromStr)]
#[label(rename_all = "snake_case")]
enum Test {
    SnakeCase,
}

Working with serde

labelled-enum provides serde_plugin feature to work with serde Serializer/Deserializer:

// install labelled-enum with --features serde_plugin

#[derive(Serialize, Deserialize)]
struct Wrapper {
    #[serde(with = "labelled_enum::serde_plugin")]
    test_snake_case: TestSnakeCase,
}

Dependencies

~1.5MB
~36K SLoC