#![allow(missing_docs)]
use wolfcose::{from_slice, to_vec, ByteBuf, CborDeserialize, CborSerialize};
#[derive(Debug, PartialEq, CborSerialize, CborDeserialize)]
#[cbor(rename_all = "snake_case")]
struct DeviceReport {
device_id: u32,
#[cbor(rename = "kid")]
key_id: ByteBuf,
#[cbor(default, skip_serializing_if = "Option::is_none")]
note: Option<String>,
}
#[derive(Debug, PartialEq, CborSerialize, CborDeserialize)]
enum DeviceEvent {
Booted,
Reading(u32, i32),
#[cbor(rename = "link_state")]
LinkState {
online: bool,
},
}
fn main() -> wolfcose::Result<()> {
let report = DeviceReport {
device_id: 42,
key_id: ByteBuf(b"device-key".to_vec()),
note: None,
};
let encoded = to_vec(&report)?;
let decoded: DeviceReport = from_slice(&encoded)?;
assert_eq!(decoded, report);
let event = DeviceEvent::LinkState { online: true };
let event_encoded = to_vec(&event)?;
let event_decoded: DeviceEvent = from_slice(&event_encoded)?;
assert_eq!(event_decoded, event);
println!("report={} event={}", encoded.len(), event_encoded.len());
Ok(())
}