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
47
48
49
50
51
52
53
54
#![feature(test)]

pub mod codec;
pub mod fixed_codec;
pub mod traits;
pub mod types;

use std::error::Error;

pub use bytes::{Buf, BufMut, Bytes, BytesMut};
use derive_more::{Constructor, Display};

#[derive(Debug, Clone)]
pub enum ProtocolErrorKind {
    // traits
    API,
    Consensus,
    Executor,
    Mempool,
    Network,
    Storage,
    Runtime,
    Binding,
    BindingMacro,
    Service,
    Main,

    // codec
    Codec,

    // fixed codec
    FixedCodec,

    // types
    Types,
}

// refer to https://github.com/rust-lang/rust/blob/a17951c4f80eb5208030f91fdb4ae93919fa6b12/src/libstd/io/error.rs#L73
#[derive(Debug, Constructor, Display)]
#[display(fmt = "[ProtocolError] Kind: {:?} Error: {:?}", kind, error)]
pub struct ProtocolError {
    kind:  ProtocolErrorKind,
    error: Box<dyn Error + Send>,
}

impl From<ProtocolError> for Box<dyn Error + Send> {
    fn from(error: ProtocolError) -> Self {
        Box::new(error) as Box<dyn Error + Send>
    }
}

impl Error for ProtocolError {}

pub type ProtocolResult<T> = Result<T, ProtocolError>;