1use bytes::BytesMut;
4
5use crate::message::Message;
6
7#[derive(Debug)]
8#[non_exhaustive]
9pub enum Name {
10 Protobuf,
11}
12
13pub struct EncodingError {
14 inner: Box<dyn std::error::Error + Send>,
15}
16
17impl std::fmt::Debug for EncodingError {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 f.debug_tuple("EncodingError").field(&self.inner).finish()
20 }
21}
22
23impl std::fmt::Display for EncodingError {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25 write!(f, "encoding error: {}", self.inner)
26 }
27}
28
29impl std::error::Error for EncodingError {}
30
31impl EncodingError {
32 pub fn new<E: std::error::Error + Send + 'static>(err: E) -> Self {
33 Self {
34 inner: Box::new(err),
35 }
36 }
37}
38
39#[derive(Clone, Debug, Default)]
40pub struct EncodingBuilder {}
41
42impl EncodingBuilder {
43 pub fn new() -> Self {
44 Self::default()
45 }
46
47 pub fn build(self) -> Encoding {
48 Encoding {
49 encoder: Encoder {},
50 decoder: Decoder {},
51 }
52 }
53}
54
55#[derive(Clone, Debug)]
56pub struct Encoding {
57 pub(crate) encoder: Encoder,
58 pub(crate) decoder: Decoder,
59}
60
61impl Encoding {
62 pub fn name(&self) -> Name {
63 Name::Protobuf
64 }
65}
66
67#[derive(Debug)]
68pub struct Encoder {}
69
70impl Clone for Encoder {
71 fn clone(&self) -> Self {
72 Self {}
73 }
74}
75
76impl Encoder {
77 pub fn encode_to(&mut self, buf: &mut BytesMut, msg: &Message) -> Result<(), EncodingError> {
78 prost::Message::encode(msg, buf).map_err(EncodingError::new)?;
79 Ok(())
80 }
81}
82
83#[derive(Debug)]
84pub struct Decoder {}
85
86impl Clone for Decoder {
87 fn clone(&self) -> Self {
88 Self {}
89 }
90}
91
92impl Decoder {
93 pub fn decode_from(&mut self, data: &[u8]) -> Result<Message, EncodingError> {
94 prost::Message::decode(data).map_err(EncodingError::new)
95 }
96}