Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/d-e-s-o/serde_variant
Retrieve serde provided variant names for enum objects.
https://github.com/d-e-s-o/serde_variant
enum rust serde variant
Last synced: 2 months ago
JSON representation
Retrieve serde provided variant names for enum objects.
- Host: GitHub
- URL: https://github.com/d-e-s-o/serde_variant
- Owner: d-e-s-o
- License: apache-2.0
- Created: 2020-01-27T05:25:45.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-04-07T14:45:40.000Z (10 months ago)
- Last Synced: 2024-10-31T12:12:57.831Z (3 months ago)
- Topics: enum, rust, serde, variant
- Language: Rust
- Homepage:
- Size: 57.6 KB
- Stars: 11
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[![pipeline](https://github.com/d-e-s-o/serde_variant/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/d-e-s-o/serde_variant/actions/workflows/test.yml)
[![crates.io](https://img.shields.io/crates/v/serde_variant.svg)](https://crates.io/crates/serde_variant)
[![Docs](https://docs.rs/serde_variant/badge.svg)](https://docs.rs/serde_variant)
[![rustc](https://img.shields.io/badge/rustc-1.57+-blue.svg)](https://blog.rust-lang.org/2021/12/02/Rust-1.57.0.html)serde_variant
=============- [Documentation][docs-rs]
- [Changelog](CHANGELOG.md)So you have just carefully defined your `enum` to be serialized and
deserialized using [`serde`][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:
```rust
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");
```[docs-rs]: https://docs.rs/crate/serde_variant
[serde]: https://crates.io/crates/serde