#tags #enums #serde

macro enser_derive

Proc macro for enser crate

5 releases

0.1.4 Jun 4, 2023
0.1.3 Jan 16, 2023
0.1.2 Jan 16, 2023
0.1.1 Jan 16, 2023
0.1.0 Jan 15, 2023

#448 in #tags

Download history 34/week @ 2023-12-13 54/week @ 2023-12-20 259/week @ 2023-12-27 94/week @ 2024-01-03 75/week @ 2024-01-10 37/week @ 2024-01-17 145/week @ 2024-01-24 145/week @ 2024-01-31 40/week @ 2024-02-07 49/week @ 2024-02-14 47/week @ 2024-02-21 65/week @ 2024-02-28 125/week @ 2024-03-06 66/week @ 2024-03-13 93/week @ 2024-03-20 99/week @ 2024-03-27

386 downloads per month
Used in 24 crates (via enser)

MIT/Apache

15KB
223 lines

✒️ enser

Crates.io docs.rs CI Coverage Status

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 } }
   ]
 }

Usage

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 },
}

Generics

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!

License

Licensed under either of

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.

Dependencies

~0.4–1MB
~21K SLoC