(rust) tested cobs crate
This commit is contained in:
@@ -8,6 +8,7 @@ anyhow = "1.0"
|
|||||||
chrono = { version = "0.4.41", features = ["serde"] }
|
chrono = { version = "0.4.41", features = ["serde"] }
|
||||||
ciborium = "0.2.2"
|
ciborium = "0.2.2"
|
||||||
clap = { version = "4.5.0", features = ["derive"] }
|
clap = { version = "4.5.0", features = ["derive"] }
|
||||||
|
cobs = { version = "0.4.0" }
|
||||||
dirs = "5.0.1"
|
dirs = "5.0.1"
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
thiserror = "2.0.12"
|
thiserror = "2.0.12"
|
||||||
|
|||||||
+13
-23
@@ -1,39 +1,29 @@
|
|||||||
#![allow(dead_code, unused)]
|
#![allow(dead_code, unused)]
|
||||||
|
|
||||||
use chrono::{DateTime, Local, Utc};
|
use chrono::{DateTime, Local, Utc};
|
||||||
use dirs::config_dir;
|
|
||||||
use serde::{self, Deserialize, Serialize};
|
|
||||||
use ciborium;
|
use ciborium;
|
||||||
|
use cobs;
|
||||||
|
use dirs::config_dir;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
pub struct TestStruct {
|
pub struct TestStruct {
|
||||||
name: String,
|
pub test_int: i32,
|
||||||
id: u128,
|
pub test_string: String,
|
||||||
time: DateTime<Utc>,
|
|
||||||
pos: SubStruct,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
||||||
pub struct SubStruct {
|
|
||||||
x: i32,
|
|
||||||
y: i32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let mut buffer: Vec<u8> = Vec::new();
|
let test_var: TestStruct = TestStruct { test_int: 0, test_string: "hello\0 world".to_owned() };
|
||||||
let test = TestStruct {
|
let mut serialised: Vec<u8> = Vec::with_capacity(1024);
|
||||||
name: "ronald yellowegger".to_owned(),
|
let mut framed = vec![0u8; 1024];
|
||||||
id: 029854u128,
|
|
||||||
time: chrono::Utc::now(),
|
|
||||||
pos: SubStruct { x: 8, y: -3 },
|
|
||||||
};
|
|
||||||
|
|
||||||
_ = 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))?;
|
let num = cobs::encode(&serialised[..], &mut framed);
|
||||||
println!("deserialised cbor struct = {:#?}", cbor);
|
|
||||||
|
println!("cobs: {:02x?}", &framed[..num]);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user