(rust) ciborium!

This commit is contained in:
2025-07-07 14:50:20 +02:00
parent b02682ab70
commit 1d3c3b1930
2 changed files with 8 additions and 5 deletions
+1 -1
View File
@@ -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"
+7 -4
View File
@@ -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<u8> = 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(())
}