#enums #variant-name #variant #serde

serde_variant

Retrieve serde provided variant names for enum objects

4 releases

0.1.3 Apr 6, 2024
0.1.2 Feb 20, 2023
0.1.1 Aug 24, 2021
0.1.0 Jan 27, 2020

#142 in Encoding

Download history 14268/week @ 2024-07-01 22723/week @ 2024-07-08 16422/week @ 2024-07-15 21886/week @ 2024-07-22 16502/week @ 2024-07-29 20033/week @ 2024-08-05 20123/week @ 2024-08-12 20172/week @ 2024-08-19 20578/week @ 2024-08-26 24660/week @ 2024-09-02 22794/week @ 2024-09-09 21962/week @ 2024-09-16 25045/week @ 2024-09-23 27203/week @ 2024-09-30 30488/week @ 2024-10-07 38992/week @ 2024-10-14

122,728 downloads per month
Used in 108 crates (30 directly)

Apache-2.0 OR MIT

13KB
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–340KB