1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! This module provides traits and implementations for serialization and
//! deserialization, allowing you to convert Rust data structures to and from
//! different formats like JSON, Protobuf, etc.

use std::fmt::Display;
use std::marker::PhantomData;

use anyhow::anyhow;
#[cfg(feature = "serde-prost")]
use prost::bytes::Bytes;
#[cfg(feature = "serde-json")]
use serde::{Deserialize, Serialize};

/// A serializer interface that can be used to serialize a Rust data type
/// into a specific wire format as a byte array.
pub trait Serializer<T>: Send + Sync {
    /// Serializes the given value into the protocol supported by this implementation.
    ///
    /// # Errors
    ///
    /// An error ([`anyhow::Error`]) is returned in case the serialization could not
    /// succeed as expected.
    fn serialize(&self, value: T) -> anyhow::Result<Vec<u8>>;
}

/// A deserializer interface that can be used to deserialize a byte array
/// into an instance of a specific Rust data type from a specific wire format.
pub trait Deserializer<T>: Send + Sync {
    /// Deserializes the given value from a message encoded in the wire format
    /// supported by this implementation.
    ///
    /// # Errors
    ///
    /// An error ([`anyhow::Error`]) is returned in case the deserialization could not
    /// succeed as expected.
    fn deserialize(&self, data: &[u8]) -> anyhow::Result<T>;
}

/// [Serializer] and [Deserializer] that can be used to serialize into and deserialize
/// from a given type into a specific wire format, such as JSON, Protobuf, etc.
pub trait Serde<T>: Serializer<T> + Deserializer<T> + Send + Sync {}

impl<S, T> Serde<T> for S where S: Serializer<T> + Deserializer<T> {}

/// Implements the [Serde] trait to translate between two different types,
/// and using the specified [Serde] for serialization and deserialization
/// using the new `Out` type.
#[derive(Clone, Copy)]
pub struct Convert<In, Out, S>
where
    In: Send + Sync,
    Out: Send + Sync,
    S: Serde<Out> + Send + Sync,
{
    serde: S,
    inn: PhantomData<In>,
    out: PhantomData<Out>,
}

impl<In, Out, S> Convert<In, Out, S>
where
    In: Send + Sync,
    Out: Send + Sync,
    S: Serde<Out> + Send + Sync,
{
    /// Creates a new [Convert] serde instance.
    pub fn new(serde: S) -> Self {
        Self {
            serde,
            inn: PhantomData,
            out: PhantomData,
        }
    }
}

impl<In, Out, S> Serializer<In> for Convert<In, Out, S>
where
    In: TryFrom<Out> + Send + Sync,
    Out: TryFrom<In> + Send + Sync,
    <Out as TryFrom<In>>::Error: Display,
    S: Serde<Out> + Send + Sync,
{
    fn serialize(&self, value: In) -> anyhow::Result<Vec<u8>> {
        self.serde.serialize(
            value
                .try_into()
                .map_err(|err| anyhow!("failed to convert type values: {}", err))?,
        )
    }
}

impl<In, Out, S> Deserializer<In> for Convert<In, Out, S>
where
    In: TryFrom<Out> + Send + Sync,
    Out: TryFrom<In> + Send + Sync,
    <In as TryFrom<Out>>::Error: Display,
    S: Serde<Out> + Send + Sync,
{
    fn deserialize(&self, data: &[u8]) -> anyhow::Result<In> {
        let inn = self.serde.deserialize(data)?;

        inn.try_into()
            .map_err(|err| anyhow!("failed to convert type values: {}", err))
    }
}

/// Implements the [Serializer] and [Deserializer] traits, which use the [serde] crate
/// to serialize and deserialize a message into JSON.
#[cfg(feature = "serde-json")]
#[derive(Debug, Clone, Copy)]
pub struct Json<T>(PhantomData<T>)
where
    T: Serialize + Send + Sync,
    for<'d> T: Deserialize<'d>;

#[cfg(feature = "serde-json")]
impl<T> Default for Json<T>
where
    T: Serialize + Send + Sync,
    for<'d> T: Deserialize<'d>,
{
    fn default() -> Self {
        Self(PhantomData)
    }
}

#[cfg(feature = "serde-json")]
impl<T> Serializer<T> for Json<T>
where
    T: Serialize + Send + Sync,
    for<'d> T: Deserialize<'d>,
{
    fn serialize(&self, value: T) -> anyhow::Result<Vec<u8>> {
        serde_json::to_vec(&value)
            .map_err(|err| anyhow!("failed to serialize value to json: {}", err))
    }
}

#[cfg(feature = "serde-json")]
impl<T> Deserializer<T> for Json<T>
where
    T: Serialize + Send + Sync,
    for<'d> T: Deserialize<'d>,
{
    fn deserialize(&self, data: &[u8]) -> anyhow::Result<T> {
        serde_json::from_slice(data)
            .map_err(|err| anyhow!("failed to deserialize value from json: {}", err))
    }
}

/// Implements the [Serde] trait  which serializes and deserializes
/// the message using Protobuf format through the [`prost::Message`] trait.
#[cfg(feature = "serde-prost")]
#[derive(Debug, Clone, Copy, Default)]
pub struct Protobuf<T>(PhantomData<T>)
where
    T: prost::Message + Default;

#[cfg(feature = "serde-prost")]
impl<T> Serializer<T> for Protobuf<T>
where
    T: prost::Message + Default,
{
    fn serialize(&self, value: T) -> anyhow::Result<Vec<u8>> {
        Ok(value.encode_to_vec())
    }
}

#[cfg(feature = "serde-prost")]
impl<T> Deserializer<T> for Protobuf<T>
where
    T: prost::Message + Default,
{
    fn deserialize(&self, data: &[u8]) -> anyhow::Result<T> {
        let buf = Bytes::copy_from_slice(data);

        T::decode(buf)
            .map_err(|err| anyhow!("failed to deserialize protobuf message into value: {}", err))
    }
}

/// Implementation of [Serde] traits that uses [ProtoJson](https://protobuf.dev/programming-guides/proto3/#json)
/// as wire protocol.
#[cfg(feature = "serde-prost")]
#[cfg(feature = "serde-json")]
#[derive(Clone, Copy, Default)]
pub struct ProtoJson<T>(PhantomData<T>)
where
    T: prost::Message + Serialize + Default,
    for<'de> T: Deserialize<'de>;

#[cfg(feature = "serde-prost")]
#[cfg(feature = "serde-json")]
impl<T> Serializer<T> for ProtoJson<T>
where
    T: prost::Message + Serialize + Default,
    for<'de> T: Deserialize<'de>,
{
    fn serialize(&self, value: T) -> anyhow::Result<Vec<u8>> {
        Json::<T>::default().serialize(value)
    }
}

#[cfg(feature = "serde-prost")]
#[cfg(feature = "serde-json")]
impl<T> Deserializer<T> for ProtoJson<T>
where
    T: prost::Message + Serialize + Default,
    for<'de> T: Deserialize<'de>,
{
    fn deserialize(&self, data: &[u8]) -> anyhow::Result<T> {
        Json::<T>::default().deserialize(data)
    }
}