From e5a338ae64b458115eaa132d4c64cedff3e745eb Mon Sep 17 00:00:00 2001 From: scbj Date: Fri, 18 Jul 2025 10:06:09 +0200 Subject: [PATCH] (rust) tested `cobs` crate --- rust/Cargo.toml | 1 + rust/src/main.rs | 36 +++++++++++++----------------------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 6c59bd8..9941cf7 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -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" diff --git a/rust/src/main.rs b/rust/src/main.rs index bf2b7ee..4c2c613 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -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, - 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 = 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 = 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(()) }