1
use crate::{error::Error, Frame, Protocol};
2
use futures::{
3
    stream::{SplitSink, SplitStream},
4
    Sink, Stream, StreamExt,
5
};
6
use jetstream_wireformat::WireFormat;
7

            
8
use tokio_util::{
9
    bytes::{self, Buf, BufMut},
10
    codec::{Decoder, Encoder},
11
};
12

            
13
pub struct ClientCodec<P>
14
where
15
    P: Protocol,
16
{
17
    _p: std::marker::PhantomData<P>,
18
}
19

            
20
impl<P: Protocol> Encoder<Frame<P::Request>> for ClientCodec<P> {
21
    type Error = Error;
22

            
23
1436
    fn encode(
24
1436
        &mut self,
25
1436
        item: Frame<P::Request>,
26
1436
        dst: &mut bytes::BytesMut,
27
1436
    ) -> Result<(), Self::Error> {
28
1436
        WireFormat::encode(&item, &mut dst.writer())?;
29
1436
        Ok(())
30
1436
    }
31
}
32

            
33
impl<P: Protocol> Decoder for ClientCodec<P> {
34
    type Error = Error;
35
    type Item = Frame<P::Response>;
36

            
37
4678
    fn decode(
38
4678
        &mut self,
39
4678
        src: &mut bytes::BytesMut,
40
4678
    ) -> Result<Option<Self::Item>, Self::Error> {
41
        // check to see if you have at least 4 bytes to figure out the size
42
4678
        if src.len() < 4 {
43
2334
            src.reserve(4);
44
2334
            return Ok(None);
45
2344
        }
46
2344
        let Some(mut bytz) = src.get(..4) else {
47
            return Ok(None);
48
        };
49

            
50
2344
        let byte_size: u32 = WireFormat::decode(&mut bytz)?;
51
2344
        if src.len() < byte_size as usize {
52
            src.reserve(byte_size as usize);
53
            return Ok(None);
54
2344
        }
55
2344
        let frame = Frame::<P::Response>::decode(&mut src.reader())?;
56
2344
        Ok(Some(frame))
57
4678
    }
58
}
59

            
60
impl<P> Default for ClientCodec<P>
61
where
62
    P: Protocol,
63
{
64
297
    fn default() -> Self {
65
297
        Self {
66
297
            _p: std::marker::PhantomData,
67
297
        }
68
297
    }
69
}
70

            
71
pub trait ClientTransport<P: Protocol>:
72
    Sink<Frame<P::Request>, Error = Error>
73
    + Stream<Item = Result<Frame<P::Response>, Error>>
74
    + Send
75
    + Sync
76
    + Unpin
77
{
78
}
79

            
80
impl<P: Protocol, T> ClientTransport<P> for T
81
where
82
    Self: Sized,
83
    T: Sink<Frame<P::Request>, Error = Error>
84
        + Stream<Item = Result<Frame<P::Response>, Error>>
85
        + Send
86
        + Sync
87
        + Unpin,
88
{
89
}
90

            
91
pub trait Channel<P: Protocol>: Unpin + Sized {
92
    fn split(self) -> (SplitSink<Self, Frame<P::Request>>, SplitStream<Self>);
93
}
94

            
95
impl<P, T> Channel<P> for T
96
where
97
    P: Protocol,
98
    T: ClientTransport<P> + Unpin + Sized,
99
{
100
    fn split(
101
        self,
102
    ) -> (
103
        SplitSink<Self, Frame<<P as Protocol>::Request>>,
104
        SplitStream<Self>,
105
    ) {
106
        StreamExt::split(self)
107
    }
108
}