https://github.com/myuon/labelled-enum
Converting an enum to/from String
https://github.com/myuon/labelled-enum
enum rust
Last synced: 2 months ago
JSON representation
Converting an enum to/from String
- Host: GitHub
- URL: https://github.com/myuon/labelled-enum
- Owner: myuon
- License: mit
- Created: 2022-03-23T12:51:15.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-03-26T10:13:07.000Z (about 3 years ago)
- Last Synced: 2025-03-01T22:28:42.828Z (3 months ago)
- Topics: enum, rust
- Language: Rust
- Homepage: https://crates.io/crates/labelled-enum
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# labelled-enum [](https://crates.io/crates/labelled-enum) [](https://docs.rs/labelled-enum)
Converting an enum to/from String.
## Getting Started
Derives ToString, FromStr impl:
```rust
#[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:
```rust
#[derive(ToString, FromStr)]
#[label(rename_all = "snake_case")]
enum Test {
SnakeCase,
}
```## Working with [serde](https://serde.rs)
labelled-enum provides `serde_plugin` feature to work with serde Serializer/Deserializer:
```rust
// install labelled-enum with --features serde_plugin#[derive(Serialize, Deserialize)]
struct Wrapper {
#[serde(with = "labelled_enum::serde_plugin")]
test_snake_case: TestSnakeCase,
}
```