From e60b3021b027f6cf3338f17829b6c8149d9ba5a5 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Mon, 19 Feb 2024 15:37:50 +0100 Subject: [PATCH] repr enum serde --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 2 ++ members/serde-json-demo/Cargo.toml | 7 ++++--- members/serde-json-demo/src/main.rs | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0ab4f0a..2caa857 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1282,6 +1282,7 @@ dependencies = [ "chrono", "serde", "serde_json", + "serde_repr", ] [[package]] @@ -1306,6 +1307,17 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_repr" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b2e6b945e9d3df726b65d6ee24060aff8e3533d431f677a9695db04eff9dfdb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.48", +] + [[package]] name = "sha2" version = "0.10.8" diff --git a/Cargo.toml b/Cargo.toml index d596c26..061bb8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,8 @@ tokio = { version = "1.35.1", features = [ "sync", ] } futures = { version = "0.3.30", features = ["executor"] } +serde = { version = "1.0.171", features = ["derive"] } +serde_json = "1.0.102" [package] name = "rs-basic" diff --git a/members/serde-json-demo/Cargo.toml b/members/serde-json-demo/Cargo.toml index 33ad066..0c9b92d 100644 --- a/members/serde-json-demo/Cargo.toml +++ b/members/serde-json-demo/Cargo.toml @@ -6,7 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -anyhow = "1.0.79" +anyhow.workspace = true chrono = { version = "0.4.26", features = ["serde"] } -serde = { version = "1.0.171", features = ["derive"] } -serde_json = "1.0.102" +serde_json.workspace = true +serde.workspace = true +serde_repr = "0.1.18" diff --git a/members/serde-json-demo/src/main.rs b/members/serde-json-demo/src/main.rs index 930beef..b108056 100644 --- a/members/serde-json-demo/src/main.rs +++ b/members/serde-json-demo/src/main.rs @@ -26,6 +26,16 @@ enum Color { Red, } +use serde_repr::{Serialize_repr, Deserialize_repr}; + +#[derive(Serialize_repr, Deserialize_repr, Clone, Debug, PartialEq)] +#[repr(u8)] +enum CLike { + Amida = 1, + Yamda = 2, + Omaba = 10, +} + fn main() -> anyhow::Result<()> { let qux_source = r#"{ "some Key": [1, 2, 3], @@ -52,5 +62,11 @@ fn main() -> anyhow::Result<()> { dbg!(&color); let color_str = serde_json::to_string(&color)?; dbg!(&color_str); + + let cl = CLike::Omaba; + let clrepr = serde_json::to_string(&cl)?; + dbg!(&clrepr); + let cl_from = serde_json::from_str(&clrepr)?; + dbg!(cl == cl_from); Ok(()) }