|
| 1 | +use crate::{Decoder, Encoder}; |
| 2 | +use serde_lite::{Deserialize, Intermediate, Serialize}; |
| 3 | + |
| 4 | +/// A wrapper codec that relies on [`serde_lite`]. With this, you can wrap serde based codecs that |
| 5 | +/// also work with serde-lite like the [`JsonSerdeCodec`] or the [`MsgpackSerdeCodec`]. |
| 6 | +/// |
| 7 | +/// Only available with the **`serde_lite` feature** enabled plus the feature you need for the |
| 8 | +/// wrapped codec. |
| 9 | +/// |
| 10 | +/// ## Example |
| 11 | +/// |
| 12 | +/// ``` |
| 13 | +/// use codee::{Encoder, Decoder, SerdeLite, string::JsonSerdeCodec, binary::MsgpackSerdeCodec}; |
| 14 | +/// use serde_lite::{Deserialize, Serialize}; |
| 15 | +/// |
| 16 | +/// #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 17 | +/// struct Test { |
| 18 | +/// s: String, |
| 19 | +/// i: i32, |
| 20 | +/// } |
| 21 | +/// |
| 22 | +/// let t = Test { |
| 23 | +/// s: String::from("party time 🎉"), |
| 24 | +/// i: 42, |
| 25 | +/// }; |
| 26 | +/// |
| 27 | +/// let enc = SerdeLite::<JsonSerdeCodec>::encode(&t).unwrap(); |
| 28 | +/// let dec: Test = SerdeLite::<JsonSerdeCodec>::decode(&enc).unwrap(); |
| 29 | +/// |
| 30 | +/// let enc = SerdeLite::<MsgpackSerdeCodec>::encode(&t).unwrap(); |
| 31 | +/// let dec: Test = SerdeLite::<MsgpackSerdeCodec>::decode(&enc).unwrap(); |
| 32 | +/// assert_eq!(dec, t); |
| 33 | +/// ``` |
| 34 | +pub struct SerdeLite<C>(C); |
| 35 | + |
| 36 | +#[derive(Debug, thiserror::Error)] |
| 37 | +pub enum SerdeLiteEncodeError<E> { |
| 38 | + #[error("Error from serde-lite: {0}")] |
| 39 | + SerdeLite(serde_lite::Error), |
| 40 | + #[error("Error from wrapped encoder")] |
| 41 | + Encoder(#[from] E), |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Debug, thiserror::Error)] |
| 45 | +pub enum SerdeLiteDecodeError<E> { |
| 46 | + #[error("Error from serde-lite: {0}")] |
| 47 | + SerdeLite(serde_lite::Error), |
| 48 | + #[error("Error from wrapped decoder")] |
| 49 | + Decoder(#[from] E), |
| 50 | +} |
| 51 | + |
| 52 | +impl<T, E> Encoder<T> for SerdeLite<E> |
| 53 | +where |
| 54 | + T: Serialize, |
| 55 | + E: Encoder<Intermediate>, |
| 56 | +{ |
| 57 | + type Error = SerdeLiteEncodeError<<E as Encoder<Intermediate>>::Error>; |
| 58 | + type Encoded = <E as Encoder<Intermediate>>::Encoded; |
| 59 | + |
| 60 | + fn encode(val: &T) -> Result<Self::Encoded, Self::Error> { |
| 61 | + let intermediate = val.serialize().map_err(SerdeLiteEncodeError::SerdeLite)?; |
| 62 | + Ok(E::encode(&intermediate)?) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl<T, D> Decoder<T> for SerdeLite<D> |
| 67 | +where |
| 68 | + T: Deserialize, |
| 69 | + D: Decoder<Intermediate>, |
| 70 | +{ |
| 71 | + type Error = SerdeLiteDecodeError<<D as Decoder<Intermediate>>::Error>; |
| 72 | + type Encoded = <D as Decoder<Intermediate>>::Encoded; |
| 73 | + |
| 74 | + fn decode(val: &Self::Encoded) -> Result<T, Self::Error> { |
| 75 | + let intermediate = D::decode(val)?; |
| 76 | + Ok(T::deserialize(&intermediate).map_err(SerdeLiteDecodeError::SerdeLite)?) |
| 77 | + } |
| 78 | +} |
0 commit comments