#variant-name #enums #variant #serialization #serde

serde_variant

Retrieve serde provided variant names for enum objects

3 releases

0.1.2 Feb 20, 2023
0.1.1 Aug 24, 2021
0.1.0 Jan 27, 2020

#353 in Encoding

Download history 2639/week @ 2023-11-27 2897/week @ 2023-12-04 2354/week @ 2023-12-11 3727/week @ 2023-12-18 4051/week @ 2023-12-25 4265/week @ 2024-01-01 5418/week @ 2024-01-08 8287/week @ 2024-01-15 9266/week @ 2024-01-22 10362/week @ 2024-01-29 11003/week @ 2024-02-05 10719/week @ 2024-02-12 11743/week @ 2024-02-19 19687/week @ 2024-02-26 11401/week @ 2024-03-04 3108/week @ 2024-03-11

46,440 downloads per month
Used in 37 crates (21 directly)

GPL-3.0-or-later

17KB
305 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

~110–350KB