disintegrate_serde/
serde.rs

1#[cfg(feature = "avro")]
2pub mod avro;
3#[cfg(feature = "json")]
4pub mod json;
5#[cfg(feature = "prost")]
6pub mod prost;
7#[cfg(feature = "protobuf")]
8pub mod protobuf;
9
10/// Serialization and deserialization error.
11#[derive(Debug, thiserror::Error)]
12pub enum Error {
13    /// an error occurred during the deserialization of the data
14    #[error("deserialization error: {0}")]
15    Deserialization(#[source] Box<dyn std::error::Error + Sync + Send>),
16    /// an error occurred while converting the persisted data to the application data
17    #[error("conversion error")]
18    Conversion,
19}
20
21/// Defines the behavior for serializing values of type `T`.
22pub trait Serializer<T> {
23    /// Serializes a value of type `T` into a byte vector.
24    ///
25    /// # Arguments
26    ///
27    /// * `value` - The value to be serialized.
28    ///
29    /// # Returns
30    ///
31    /// A byte vector containing the serialized representation of the value.
32    fn serialize(&self, value: T) -> Vec<u8>;
33}
34
35/// Defines the behavior for deserializing values of type `T`.
36pub trait Deserializer<T> {
37    /// Deserializes a byte vector into a value of type `T`.
38    ///
39    /// # Arguments
40    ///
41    /// * `data` - The byte vector to be deserialized.
42    ///
43    /// # Returns
44    ///
45    /// A `Result` containing the deserialized value on success, or an error on failure.
46    fn deserialize(&self, data: Vec<u8>) -> Result<T, Error>;
47}
48
49/// Combines the `Serializer` and `Deserializer` traits for convenience.
50pub trait Serde<T>: Serializer<T> + Deserializer<T> {}
51
52impl<K, T> Serde<T> for K where K: Serializer<T> + Deserializer<T> {}