Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/myuon/labelled-enum
Converting an enum to/from String
https://github.com/myuon/labelled-enum
enum rust
Last synced: 23 days 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 (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-03-26T10:13:07.000Z (over 2 years ago)
- Last Synced: 2024-09-14T03:50:22.516Z (about 2 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 [![labelled-enum at crates.io](https://img.shields.io/crates/v/labelled-enum.svg)](https://crates.io/crates/labelled-enum) [![labelled-enum at docs.rs](https://docs.rs/labelled-enum/badge.svg)](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,
}
```