Crates.io | serde_variant |
lib.rs | serde_variant |
version | 0.1.3 |
source | src |
created_at | 2020-01-27 06:55:17.191205 |
updated_at | 2024-04-06 20:28:32.899336 |
description | Retrieve serde provided variant names for enum objects. |
homepage | https://github.com/d-e-s-o/serde_variant |
repository | https://github.com/d-e-s-o/serde_variant.git |
max_upload_size | |
id | 202347 |
size | 27,086 |
So you have just carefully defined your enum
to be serialized and
deserialized using serde
as you intended and now you need an
additional FromStr
or Display
implementation that uses the same
names for enum
variants as serde
uses? You are reluctant to
duplicate all those definitions in two places?
serde_variant is a crate that allows you to retrieve back the
identifier of any enum
variant passed to it.
The crate provides a single function, to_variant_name
, that retrieves
the name of a passed in enum
variant. For example:
use serde_variant::to_variant_name;
#[derive(Serialize)]
enum Foo {
Var1,
#[serde(rename = "VAR2")]
Var2,
}
assert_eq!(to_variant_name(&Foo::Var1).unwrap(), "Var1");
assert_eq!(to_variant_name(&Foo::Var2).unwrap(), "VAR2");