Crates.io | serde_short |
lib.rs | serde_short |
version | 0.1.2 |
source | src |
created_at | 2024-09-02 16:15:25.257619 |
updated_at | 2024-09-02 18:37:00.825623 |
description | Derive Serialize and Deserialize for enum reperesented as C short enum |
homepage | |
repository | https://github.com/jazi007/serde-short |
max_upload_size | |
id | 1360756 |
size | 27,006 |
This crate provides a derive macro to derive Serde's Serialize
and
Deserialize
traits for C enum represented as short enum
u8
, u16
, u32
or in case first value is < 0 i8
, i16
or i32
99% of the code is a copy/paste from serde-repr all credits goes to this crate
[dependencies]
serde = "1.0"
serde_short = "0.1"
use serde_short::{Serialize_short, Deserialize_short};
#[derive(Serialize_short, Deserialize_short, PartialEq, Debug)]
#[repr(u8)]
enum SmallPrime {
Two = 2,
Three = 3,
Five = 5,
Seven = 7,
}
fn main() -> serde_json::Result<()> {
let j = serde_json::to_string(&SmallPrime::Seven)?;
assert_eq!(j, "7");
let p: SmallPrime = serde_json::from_str("2")?;
assert_eq!(p, SmallPrime::Two);
Ok(())
}