1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//! A utility crate built for the libqaul ecosystem
//!
//! Note: unfortunately `serde` is very re-export unfriendly, so you
//! will want to include `serde = { version = "1.0", features =
//! ["derive"] }` to your crate manually!

pub use postcard::{Error, Result};

use heapless::{consts::*, Vec as Hecc};
use serde::{de::DeserializeOwned, Serialize};

/// Serialise any iteratable into a compacted binary payload
pub fn serialise<S>(s: &S) -> Result<Vec<u8>>
where
    S: Serialize,
{
    let v: Hecc<u8, U11> = postcard::to_vec(s)?;
    Ok(v.to_vec())
}

/// Deserialise a byte array payload into a concrete type
pub fn deserialise<D>(data: &Vec<u8>) -> Result<D>
where
    D: DeserializeOwned,
{
    Ok(postcard::from_bytes(&data)?)
}