labelled-enum

Crates.iolabelled-enum
lib.rslabelled-enum
version0.1.1
sourcesrc
created_at2022-03-26 09:50:29.63102
updated_at2022-03-26 10:04:45.761167
descriptionConverting an enum to/from String
homepagehttps://github.com/myuon/labelled-enum#readme
repositoryhttps://github.com/myuon/labelled-enum
max_upload_size
id556627
size6,027
myuon (myuon)

documentation

README

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,
}
Commit count: 23

cargo fmt