micro_tower/api/codec.rs
1use bytes::buf::{Reader, Writer};
2use bytes::BytesMut;
3
4mod json;
5
6pub use json::Json;
7
8pub trait Decode<T> {
9 type Error;
10
11 /// Read bytes from byte buffer and decode them to the structure `T`.
12 ///
13 /// # Errors
14 ///
15 /// Will return `Err` if bytes of buffer cannot be decoded to structure `T`.
16 fn decode(reader: &mut Reader<BytesMut>) -> Result<T, Self::Error>;
17}
18
19pub trait Encode<T> {
20 type Error;
21
22 /// Encode message structure `T` and write the encoded message to byte buffer.
23 ///
24 /// # Errors
25 ///
26 /// Will return `Err` if `message` cannot be encoded.
27 fn encode(writer: &mut Writer<BytesMut>, message: T) -> Result<(), Self::Error>;
28}