From 1d3c3b1930aa1fb6c2d0f37f4b0d31553de78243 Mon Sep 17 00:00:00 2001 From: zegonix Date: Mon, 7 Jul 2025 14:50:20 +0200 Subject: [PATCH] (rust) ciborium! --- rust/Cargo.toml | 2 +- rust/src/main.rs | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 13b315e..6c59bd8 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -6,9 +6,9 @@ edition = "2021" [dependencies] anyhow = "1.0" chrono = { version = "0.4.41", features = ["serde"] } +ciborium = "0.2.2" clap = { version = "4.5.0", features = ["derive"] } dirs = "5.0.1" serde = { version = "1.0.219", features = ["derive"] } -serde_json = { version = "1.0.140", features = ["default"] } thiserror = "2.0.12" diff --git a/rust/src/main.rs b/rust/src/main.rs index 43a929d..bf2b7ee 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -3,7 +3,7 @@ use chrono::{DateTime, Local, Utc}; use dirs::config_dir; use serde::{self, Deserialize, Serialize}; -use serde_json; +use ciborium; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct TestStruct { @@ -20,6 +20,7 @@ pub struct SubStruct { } fn main() -> anyhow::Result<()> { + let mut buffer: Vec = Vec::new(); let test = TestStruct { name: "ronald yellowegger".to_owned(), id: 029854u128, @@ -27,10 +28,12 @@ fn main() -> anyhow::Result<()> { pos: SubStruct { x: 8, y: -3 }, }; - let json = serde_json::to_string(&test)?; + _ = ciborium::into_writer(&test, &mut buffer)?; - println!("json = \\"); - println!("{}", json); + println!("{:02x?}", buffer); + + let cbor: TestStruct = ciborium::from_reader(std::io::Cursor::new(&mut buffer))?; + println!("deserialised cbor struct = {:#?}", cbor); Ok(()) }