binary_codec/
lib.rs

1#[derive(Debug)]
2pub enum SerializationError {
3    /// Value is out of bounds (value, min, max)
4    ValueOutOfBounds(i32, i32, i32),
5
6    // Unexpected size (expected, actual)
7    UnexpectedLength(usize, usize),
8
9    /// Missing runtime length key
10    MissingLengthByKey(String),
11
12    /// Validation did fail for the data
13    InvalidData(String),
14}
15
16#[derive(Debug)]
17pub enum DeserializationError {
18    /// Not enough bytes (bytes missing)
19    NotEnoughBytes(usize),
20
21    // Unexpected size (expected, actual)
22    UnexpectedLength(usize, usize),
23
24    /// Unknown enum discriminator
25    UnknownDiscriminant(u8),
26
27    /// Missing runtime length key
28    MissingLengthByKey(String),
29
30    /// Validation did fail for the data
31    InvalidData(String),
32}
33
34pub trait BinarySerializer<T: Clone = ()> {
35    fn serialize_bytes(
36        &self,
37        config: Option<&mut SerializerConfig<T>>,
38    ) -> Result<Vec<u8>, SerializationError>;
39    fn to_bytes(&self) -> Result<Vec<u8>, SerializationError> {
40        self.serialize_bytes(None)
41    }
42
43    fn write_bytes(
44        &self,
45        buffer: &mut Vec<u8>,
46        config: Option<&mut SerializerConfig<T>>,
47    ) -> Result<(), SerializationError>;
48}
49
50pub trait BinaryDeserializer<T: Clone = ()>: Sized {
51    fn deserialize_bytes(
52        bytes: &[u8],
53        config: Option<&mut SerializerConfig<T>>,
54    ) -> Result<Self, DeserializationError>;
55    fn from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError> {
56        Self::deserialize_bytes(bytes, None)
57    }
58}
59
60mod config;
61pub mod dyn_int;
62pub mod dynamics;
63pub mod fixed_int;
64pub mod utils;
65pub mod variable;
66
67pub use binary_codec_derive::{FromBytes, ToBytes};
68pub use config::SerializerConfig;