Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/azriel91/enser
Enum Serialization with Tag
https://github.com/azriel91/enser
Last synced: about 2 months ago
JSON representation
Enum Serialization with Tag
- Host: GitHub
- URL: https://github.com/azriel91/enser
- Owner: azriel91
- License: apache-2.0
- Created: 2023-01-15T22:50:34.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-27T04:41:59.000Z (over 1 year ago)
- Last Synced: 2024-11-16T04:50:47.748Z (about 2 months ago)
- Language: Rust
- Size: 31.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# ✒️ enser
[![Crates.io](https://img.shields.io/crates/v/enser.svg)](https://crates.io/crates/enser)
[![docs.rs](https://img.shields.io/docsrs/enser)](https://docs.rs/enser)
[![CI](https://github.com/azriel91/enser/workflows/CI/badge.svg)](https://github.com/azriel91/enser/actions/workflows/ci.yml)
[![Coverage Status](https://codecov.io/gh/azriel91/enser/branch/main/graph/badge.svg)](https://codecov.io/gh/azriel91/enser)Enum Serialization with Tag
```diff
# serde_yaml -- every variant starts with a !Tag
enser:
-- Tbd
-- None
+- !Tbd null
+- !None null
- !Some 123
- !Named
value: 456# serde_json -- every variant is an object
{
"enser": [
- "Tbd",
- "None",
+ { "Tbd": null },
+ { "None": null },
{ "Some": 123 },
{ "Named": { "value": 456 } }
]
}
```## Usage
Add the following to `Cargo.toml`
```toml
enser = "0.1.4"
``````rust
#[enser::enser] // <-- just add this
// Note: It *must* come above `#[derive(Clone, Deserialize, Serialize)]`
#[derive(Clone, Debug, Deserialize, Serialize)]
enum MyEnum {
Tbd,
None,
Some(u32),
Named { value: u32 },
}
```## Generics
This will automatically work for generic types:
```rust
#[enser::enser]
#[derive(Clone, Debug, Deserialize, Serialize)]
enum MyEnum {
None,
Some(T),
Named { value: U },
}
```**However**, it also adds a `Clone` bound to each type parameter, so all `impl` blocks will require the type parameters to have a `Clone` bound.
If you can find a way for the `generics` example to work without causing the `Clone` bound propagation, then please let me know / submit a pull request!
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE] or )
* MIT license ([LICENSE-MIT] or )at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
[LICENSE-APACHE]: LICENSE-APACHE
[LICENSE-MIT]: LICENSE-MIT