Crates.io | enser |
lib.rs | enser |
version | 0.1.4 |
source | src |
created_at | 2023-01-15 23:01:05.72703 |
updated_at | 2023-06-04 04:57:06.763848 |
description | Enum serialization with tag |
homepage | |
repository | https://github.com/azriel91/enser |
max_upload_size | |
id | 759731 |
size | 36,849 |
Enum Serialization with Tag
# 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 } }
]
}
Add the following to Cargo.toml
enser = "0.1.4"
#[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 },
}
This will automatically work for generic types:
#[enser::enser]
#[derive(Clone, Debug, Deserialize, Serialize)]
enum MyEnum<T, U> {
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!
Licensed under either of
at your option.
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.