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

#[repr(u16)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ErrorCode {
    Failed,
    InvalidData,
    ConnectFailed,
    InvalidParam,
}

#[derive(Debug)]
pub struct Error {
    code: ErrorCode,
    msg: String,
}

impl Error {
    pub fn new(code: impl Into<ErrorCode>, msg: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            msg: msg.into(),
        }
    }

    pub fn code(&self) -> ErrorCode {
        self.code
    }

    pub fn msg(&self) -> &str {
        self.msg.as_str()
    }
}

pub type Result<T> = std::result::Result<T, Error>;