#serialization #serde #enum #variants

serde_variant

Retrieve serde provided variant names for enum objects

2 releases

0.1.1 Aug 24, 2021
0.1.0 Jan 27, 2020

#408 in Encoding

Download history 577/week @ 2022-08-10 557/week @ 2022-08-17 346/week @ 2022-08-24 469/week @ 2022-08-31 370/week @ 2022-09-07 593/week @ 2022-09-14 770/week @ 2022-09-21 645/week @ 2022-09-28 955/week @ 2022-10-05 943/week @ 2022-10-12 952/week @ 2022-10-19 757/week @ 2022-10-26 1187/week @ 2022-11-02 753/week @ 2022-11-09 802/week @ 2022-11-16 802/week @ 2022-11-23

3,666 downloads per month
Used in 23 crates (12 directly)

GPL-3.0-or-later

16KB
239 lines

pipeline crates.io Docs rustc

serde_variant

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.

Usage

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");

Dependencies

~130–250KB