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
69,913 downloads per month
Used in 71 crates
(32 directly)
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