1use bytes::BytesMut;
2
3#[derive(Clone, Debug, Eq, PartialEq)]
5pub enum EnvelopedDecoderError<T> {
6 UnknownTypeId,
7 Payload(T),
8}
9
10impl<T> From<T> for EnvelopedDecoderError<T> {
11 fn from(e: T) -> Self {
12 Self::Payload(e)
13 }
14}
15
16pub trait EnvelopedEncodable {
18 fn encode(&self) -> BytesMut {
20 let type_id = self.type_id();
21
22 let mut out = BytesMut::new();
23 if let Some(type_id) = type_id {
24 assert!(type_id <= 0x7f);
25 out.extend_from_slice(&[type_id]);
26 }
27
28 out.extend_from_slice(&self.encode_payload()[..]);
29 out
30 }
31
32 fn type_id(&self) -> Option<u8>;
34
35 fn encode_payload(&self) -> BytesMut;
37}
38
39pub trait EnvelopedDecodable: Sized {
41 type PayloadDecoderError;
43
44 fn decode(bytes: &[u8]) -> Result<Self, EnvelopedDecoderError<Self::PayloadDecoderError>>;
46}