jamesmunns / postcard

A no_std + serde compatible message library for Rust
Apache License 2.0
915 stars 87 forks source link

Serialize/deserialize with footer? #16

Open korken89 opened 4 years ago

korken89 commented 4 years ago

What is the current recommended way to serialize/deserialize with a footer? With this I mean, in my usecase, I want to append a CRC to the end of the buffer and each byte being serialized should also go through the CRC calculation. Currently if I serialize with a flavor I cannot append the CRC manually as it can for example have been encoded with COBS.

Let's say I want something like this:

struct Message {
    // ...
}
pub fn serialize_into_buffer(msg: Message, buf: &mut [u8]) -> postcard::Result<usize> {

    // How to add footer? A "add footer flavor" which takes a closure?

    let res = serialize_with_flavor(&msg, Cobs::try_new(Slice::new(buf))?)?;
    Ok(res.len())
}
korken89 commented 4 years ago

The issue I see with a "footer flavor" is that there is no way to push the footer before COBS finishes. One could extend the COBS flavor to have an optional "generate footer" that is None in the normal case, and if there is something to generate a footer it will be pushed before the finalization of COBS, but then one would have to add so the deserialization returns an optional footer.

korken89 commented 4 years ago

An alternative is to implement Hash for it as such:

#[derive(Hash, Serialize, Deserialize)]
pub enum Message {
    /// ...
}

#[derive(Serialize, Deserialize)]
struct Packet {
    msg: Message,
    crc: u16,
}

And fill the CRC with a suitably implemented Hasher.

bugadani commented 2 years ago

I'm necromancing this issue because I am also looking for similar functionality. I'm not really familiar with Postcard at this time, so this might not be a good idea: My approach would be a CRC flavour that wraps COBS. Dropping the flavour object could/would write the computed CRC with its "last breath" to the COBS flavour - although a more elegant solution would be to expose the end of the message to the flavours.