#enums #string #deserialize #package

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

#1000 in Encoding

Download history 20136/week @ 2024-07-21 19957/week @ 2024-07-28 16053/week @ 2024-08-04 16964/week @ 2024-08-11 14199/week @ 2024-08-18 15919/week @ 2024-08-25 17395/week @ 2024-09-01 17518/week @ 2024-09-08 16064/week @ 2024-09-15 15390/week @ 2024-09-22 14802/week @ 2024-09-29 11873/week @ 2024-10-06 16354/week @ 2024-10-13 19651/week @ 2024-10-20 17899/week @ 2024-10-27 15163/week @ 2024-11-03

69,913 downloads per month
Used in 71 crates (32 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
~45K SLoC