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