-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathenveloped.rs
46 lines (37 loc) · 1.02 KB
/
enveloped.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use bytes::BytesMut;
/// DecoderError for typed transactions.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum EnvelopedDecoderError<T> {
UnknownTypeId,
Payload(T),
}
impl<T> From<T> for EnvelopedDecoderError<T> {
fn from(e: T) -> Self {
Self::Payload(e)
}
}
/// Encodable typed transactions.
pub trait EnvelopedEncodable {
/// Convert self to an owned vector.
fn encode(&self) -> BytesMut {
let type_id = self.type_id();
let mut out = BytesMut::new();
if let Some(type_id) = type_id {
assert!(type_id <= 0x7f);
out.extend_from_slice(&[type_id]);
}
out.extend_from_slice(&self.encode_payload()[..]);
out
}
/// Type Id of the transaction.
fn type_id(&self) -> Option<u8>;
/// Encode inner payload.
fn encode_payload(&self) -> BytesMut;
}
/// Decodable typed transactions.
pub trait EnvelopedDecodable: Sized {
/// Inner payload decoder error.
type PayloadDecoderError;
/// Decode raw bytes to a Self type.
fn decode(bytes: &[u8]) -> Result<Self, EnvelopedDecoderError<Self::PayloadDecoderError>>;
}