poem_grpc/codec/
mod.rs

1//! Generic encoding and decoding.
2
3#[cfg(feature = "json-codec")]
4mod json_codec;
5mod prost_codec;
6
7use std::io::Result;
8
9use bytes::BytesMut;
10
11/// The encoder that can encode a message
12pub trait Encoder: Send + 'static {
13    /// The message type
14    type Item: Send + 'static;
15
16    /// Encode a message to buffer
17    fn encode(&mut self, message: Self::Item, buf: &mut BytesMut) -> Result<()>;
18}
19
20/// The decoder that can decode a message
21pub trait Decoder: Send + 'static {
22    /// The message type
23    type Item: Send + 'static;
24
25    /// Decode a message from buffer
26    fn decode(&mut self, buf: &[u8]) -> Result<Self::Item>;
27}
28
29/// Represents a type that can encode/decode a message
30pub trait Codec: Default {
31    /// Content types
32    const CONTENT_TYPES: &'static [&'static str];
33
34    /// The encodable message
35    type Encode: Send + 'static;
36
37    /// The decodable message
38    type Decode: Send + 'static;
39
40    /// The encoder that can encode a message
41    type Encoder: Encoder<Item = Self::Encode>;
42
43    /// The decoder that can encode a message
44    type Decoder: Decoder<Item = Self::Decode>;
45
46    /// Returns whether the specified content type is supported
47    #[inline]
48    fn check_content_type(&self, ct: &str) -> bool {
49        Self::CONTENT_TYPES.contains(&ct)
50    }
51
52    /// Create the encoder
53    fn encoder(&mut self) -> Self::Encoder;
54
55    /// Create the decoder
56    fn decoder(&mut self) -> Self::Decoder;
57}
58
59#[cfg(feature = "json-codec")]
60pub use json_codec::{
61    JsonCodec, JsonDecoder, JsonEncoder, JsonI64ToStringCodec, JsonI64ToStringDecoder,
62    JsonI64ToStringEncoder,
63};
64pub use prost_codec::{ProstCodec, ProstDecoder, ProstEncoder};