hyperion/servers/proto/
codec.rs

1use bytes::BytesMut;
2use prost::Message;
3use thiserror::Error;
4use tokio_util::codec::{Decoder, Encoder, LengthDelimitedCodec};
5
6use crate::api::proto::message;
7
8#[derive(Debug, Error)]
9pub enum ProtoCodecError {
10    #[error("i/o error: {0}")]
11    Io(#[from] futures_io::Error),
12    #[error(transparent)]
13    LengthDelimited(#[from] tokio_util::codec::LengthDelimitedCodecError),
14    #[error(transparent)]
15    Decode(#[from] prost::DecodeError),
16    #[error(transparent)]
17    Encode(#[from] prost::EncodeError),
18}
19
20/// JSON tokio codec
21pub struct ProtoCodec {
22    /// Line parsing codec
23    inner: LengthDelimitedCodec,
24    /// Buffer for encoding messages
25    buf: BytesMut,
26}
27
28impl ProtoCodec {
29    /// Create a new ProtoCodec
30    pub fn new() -> Self {
31        Self {
32            inner: LengthDelimitedCodec::builder()
33                .length_field_length(4)
34                .new_codec(),
35            buf: BytesMut::new(),
36        }
37    }
38}
39
40impl Decoder for ProtoCodec {
41    type Item = message::HyperionRequest;
42    type Error = ProtoCodecError;
43
44    fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
45        match self.inner.decode(src) {
46            Ok(inner_result) => Ok(match inner_result {
47                Some(ref data) => Some(message::HyperionRequest::decode(data.clone().freeze())?),
48                None => None,
49            }),
50            Err(error) => Err(error.into()),
51        }
52    }
53}
54
55impl Encoder<message::HyperionReply> for ProtoCodec {
56    type Error = ProtoCodecError;
57
58    fn encode(
59        &mut self,
60        item: message::HyperionReply,
61        dst: &mut BytesMut,
62    ) -> Result<(), Self::Error> {
63        self.buf.clear();
64        self.buf.reserve(item.encoded_len());
65
66        match item.encode(&mut self.buf) {
67            Ok(_) => Ok(self.inner.encode(self.buf.clone().freeze(), dst)?),
68            Err(encode_error) => Err(encode_error.into()),
69        }
70    }
71}