#enums #serialization #string #serde #deserialize

macro no-std serde-enum-str

Serialize and deserialize enum string

12 unstable releases (3 breaking)

0.4.0 Aug 4, 2023
0.3.2 Dec 17, 2022
0.2.5 Apr 5, 2022
0.2.4 Aug 22, 2021
0.1.4 Jul 25, 2021

#1357 in Encoding

Download history 22609/week @ 2024-03-14 22006/week @ 2024-03-21 22550/week @ 2024-03-28 20939/week @ 2024-04-04 21417/week @ 2024-04-11 24310/week @ 2024-04-18 24712/week @ 2024-04-25 23373/week @ 2024-05-02 24550/week @ 2024-05-09 23293/week @ 2024-05-16 21223/week @ 2024-05-23 22237/week @ 2024-05-30 21585/week @ 2024-06-06 22583/week @ 2024-06-13 19815/week @ 2024-06-20 14412/week @ 2024-06-27

82,360 downloads per month
Used in 67 crates (30 directly)

Apache-2.0 OR MIT

51KB
1K SLoC

serde-enum-str

Solves

https://stackoverflow.com/questions/57469527

Dev

cargo expand --verbose --test test

lib.rs:

Serialize and deserialize enum string.

[dependencies]
serde-enum-str = "0.3"
serde = { version = "1", features = ["derive"] }
use serde_enum_str::{Deserialize_enum_str, Serialize_enum_str};

#[derive(Deserialize_enum_str, Serialize_enum_str, Debug, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
enum Foo {
    A,
    #[serde(rename = "B")]
    B,
    #[serde(other)]
    Other(String),
}

fn main() -> serde_json::Result<()> {
    assert_eq!(serde_json::to_string(&Foo::A).unwrap(), r#""a""#);
    assert_eq!(serde_json::from_str::<Foo>(r#""a""#).unwrap(), Foo::A);

    assert_eq!(
        serde_json::from_str::<Foo>(r#""c""#).unwrap(),
        Foo::Other("c".to_owned())
    );

    assert_eq!(Foo::A.to_string(), "a");
    assert_eq!("a".parse::<Foo>().unwrap(), Foo::A);

    Ok(())
}

Dependencies

~2MB
~43K SLoC