roboplc_rpc/dataformat/
mod.rs

1use core::fmt;
2
3use serde::{Deserialize, Serialize};
4
5mod json;
6pub use json::Packer as Json;
7#[cfg(feature = "msgpack")]
8mod msgpack;
9#[cfg(feature = "msgpack")]
10pub use msgpack::Packer as Msgpack;
11
12/// A trait for data formats that can be packed and unpacked.
13pub trait DataFormat {
14    /// The error type for packing.
15    type PackError: fmt::Display;
16    /// The error type for unpacking.
17    type UnpackError: fmt::Display;
18
19    /// Pack data into a byte vector.
20    fn pack<D: Serialize>(data: &D) -> Result<Vec<u8>, Self::PackError>;
21    /// Unpack data from a byte slice.
22    fn unpack<'de, T: Deserialize<'de>>(payload: &'de [u8]) -> Result<T, Self::UnpackError>;
23}