(rust) tested cobs crate

This commit is contained in:
scbj
2025-07-18 10:06:09 +02:00
parent 860fed5810
commit e5a338ae64
2 changed files with 14 additions and 23 deletions
+1
View File
@@ -8,6 +8,7 @@ anyhow = "1.0"
chrono = { version = "0.4.41", features = ["serde"] }
ciborium = "0.2.2"
clap = { version = "4.5.0", features = ["derive"] }
cobs = { version = "0.4.0" }
dirs = "5.0.1"
serde = { version = "1.0.219", features = ["derive"] }
thiserror = "2.0.12"
+13 -23
View File
@@ -1,39 +1,29 @@
#![allow(dead_code, unused)]
use chrono::{DateTime, Local, Utc};
use dirs::config_dir;
use serde::{self, Deserialize, Serialize};
use ciborium;
use cobs;
use dirs::config_dir;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TestStruct {
name: String,
id: u128,
time: DateTime<Utc>,
pos: SubStruct,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SubStruct {
x: i32,
y: i32,
pub test_int: i32,
pub test_string: String,
}
fn main() -> anyhow::Result<()> {
let mut buffer: Vec<u8> = Vec::new();
let test = TestStruct {
name: "ronald yellowegger".to_owned(),
id: 029854u128,
time: chrono::Utc::now(),
pos: SubStruct { x: 8, y: -3 },
};
let test_var: TestStruct = TestStruct { test_int: 0, test_string: "hello\0 world".to_owned() };
let mut serialised: Vec<u8> = Vec::with_capacity(1024);
let mut framed = vec![0u8; 1024];
_ = ciborium::into_writer(&test, &mut buffer)?;
_ = ciborium::into_writer(&test_var, &mut serialised);
println!("{:02x?}", buffer);
println!("cbor: {:02x?}", &serialised[..]);
let cbor: TestStruct = ciborium::from_reader(std::io::Cursor::new(&mut buffer))?;
println!("deserialised cbor struct = {:#?}", cbor);
let num = cobs::encode(&serialised[..], &mut framed);
println!("cobs: {:02x?}", &framed[..num]);
Ok(())
}