Skip to main content

atomr_core/serialization/
traits.rs

1//! Serializer traits.
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum SerializerError {
7    #[error("no serializer registered for type")]
8    NotRegistered,
9    #[error("serializer id {0} not known")]
10    UnknownId(u32),
11    #[error("encode error: {0}")]
12    Encode(String),
13    #[error("decode error: {0}")]
14    Decode(String),
15}
16
17pub trait Serializer<T>: Send + Sync {
18    fn identifier(&self) -> u32;
19    fn manifest(&self) -> &'static str;
20    fn to_bytes(&self, value: &T) -> Result<Vec<u8>, SerializerError>;
21    #[allow(clippy::wrong_self_convention)]
22    fn from_bytes(&self, bytes: &[u8]) -> Result<T, SerializerError>;
23}