Code Monkey home page Code Monkey logo

serde_variant's Introduction

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

serde_variant's People

Contributors

d-e-s-o avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

serde_variant's Issues

Valued variants are not supported.

Change the readme example by adding a value to one of the variants

use serde_variant::to_variant_name;
use serde_derive::*;

#[derive(Serialize)]
enum Foo {
  Var1,
  #[serde(rename = "VAR2")]
  Var2(i32),
}

fn main() {
    assert_eq!(to_variant_name(&Foo::Var1).unwrap(), "Var1");
    assert_eq!(to_variant_name(&Foo::Var2(42)).unwrap(), "VAR2");
}

and it fails:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: UnsupportedType { method: "serialize_newtype_variant" }', src/main.rs:13:48

... which makes this crate much less useful than it could have been.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.