1use std::marker::PhantomData;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::BoxDynError;
6
7pub trait Codec {
9 type Compact;
11 type Error: Into<BoxDynError>;
13 fn encode<I>(input: I) -> Result<Self::Compact, Self::Error>
15 where
16 I: Serialize;
17 fn decode<O>(input: Self::Compact) -> Result<O, Self::Error>
19 where
20 O: for<'de> Deserialize<'de>;
21}
22
23#[derive(Debug, Clone)]
26pub struct NoopCodec<Compact> {
27 compact: PhantomData<Compact>,
28}
29
30impl<Compact> Codec for NoopCodec<Compact> {
31 type Compact = Compact;
32 type Error = BoxDynError;
33
34 fn decode<O>(_: Self::Compact) -> Result<O, Self::Error>
35 where
36 O: for<'de> Deserialize<'de>,
37 {
38 unreachable!("NoopCodec doesn't have decoding functionality")
39 }
40 fn encode<I>(_: I) -> Result<Self::Compact, Self::Error>
41 where
42 I: Serialize,
43 {
44 unreachable!("NoopCodec doesn't have decoding functionality")
45 }
46}
47
48#[cfg(feature = "json")]
50pub mod json;