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
use std::io;

use dyn_clone::DynClone;

use super::Frame;

mod chain;
mod compression;
mod encryption;
mod plain;
mod predicate;

pub use chain::*;
pub use compression::*;
pub use encryption::*;
pub use plain::*;
pub use predicate::*;

/// Represents abstraction that implements specific encoder and decoder logic to transform an
/// arbitrary collection of bytes. This can be used to encrypt and authenticate bytes sent and
/// received by transports.
pub trait Codec: DynClone {
    /// Encodes a frame's item
    fn encode<'a>(&mut self, frame: Frame<'a>) -> io::Result<Frame<'a>>;

    /// Decodes a frame's item
    fn decode<'a>(&mut self, frame: Frame<'a>) -> io::Result<Frame<'a>>;
}

/// Represents a [`Box`]ed version of [`Codec`]
pub type BoxedCodec = Box<dyn Codec + Send + Sync>;

macro_rules! impl_traits {
    ($($x:tt)+) => {
        impl Clone for Box<dyn $($x)+> {
            fn clone(&self) -> Self {
                dyn_clone::clone_box(&**self)
            }
        }

        impl Codec for Box<dyn $($x)+> {
            fn encode<'a>(&mut self, frame: Frame<'a>) -> io::Result<Frame<'a>> {
                Codec::encode(self.as_mut(), frame)
            }

            fn decode<'a>(&mut self, frame: Frame<'a>) -> io::Result<Frame<'a>> {
                Codec::decode(self.as_mut(), frame)
            }
        }
    };
}

impl_traits!(Codec);
impl_traits!(Codec + Send);
impl_traits!(Codec + Sync);
impl_traits!(Codec + Send + Sync);

/// Interface that provides extensions to the codec interface
pub trait CodecExt {
    /// Chains this codec with another codec
    fn chain<T>(self, codec: T) -> ChainCodec<Self, T>
    where
        Self: Sized;
}

impl<C: Codec> CodecExt for C {
    fn chain<T>(self, codec: T) -> ChainCodec<Self, T> {
        ChainCodec::new(self, codec)
    }
}