handshake_encoding/
lib.rs

1use encodings::hex::{FromHex, FromHexError, ToHex};
2use extended_primitives::{Buffer, BufferError};
3use std::fmt;
4
5#[derive(Debug)]
6pub enum DecodingError {
7    //Overall wrapper type to enclose decoding errors.
8    InvalidData(String),
9    Buffer(BufferError),
10    Hex(FromHexError),
11    UnknownInventory,
12}
13
14impl From<FromHexError> for DecodingError {
15    fn from(e: FromHexError) -> DecodingError {
16        DecodingError::Hex(e)
17    }
18}
19
20impl From<BufferError> for DecodingError {
21    fn from(e: BufferError) -> DecodingError {
22        DecodingError::Buffer(e)
23    }
24}
25
26impl fmt::Display for DecodingError {
27    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28        match *self {
29            DecodingError::InvalidData(ref e) => write!(f, "Invalid Data: {}", e),
30            DecodingError::Buffer(ref e) => write!(f, "Buffer Error: {}", e),
31            DecodingError::Hex(ref e) => write!(f, "Hex Error: {}", e),
32            DecodingError::UnknownInventory => write!(f, "Unknown Inventory Type"),
33        }
34    }
35}
36
37pub trait Encodable {
38    fn size(&self) -> usize;
39
40    fn encode(&self) -> Buffer;
41}
42
43pub trait Decodable
44where
45    Self: Sized,
46{
47    type Err;
48    fn decode(buffer: &mut Buffer) -> Result<Self, Self::Err>;
49}
50
51//@todo would be nice to impl a proc_macro here for Hex.