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
55
use std::error;
use std::fmt;
use crate::ErrorKind;

#[derive(Debug, Clone, PartialEq)]
pub struct Error {
    kind: ErrorKind,
    message: String,
}

impl Error {

    pub fn new<M>(kind: ErrorKind, message: M) -> Self
        where
        M: Into<String>,
    {
        Self {
            kind,
            message: message.into(),
        }
    }

    pub fn kind(&self) -> &ErrorKind {
        &self.kind
    }

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

    pub fn exit_code(&self) -> i32 {
        match self.kind { // [64 - 113]
            ErrorKind::UnknownCommand => 65,
            ErrorKind::UnknownFlag => 66,
            ErrorKind::MissingFlagValue => 67,
            ErrorKind::InvalidFlagValue => 69,
            ErrorKind::MissingResolver => 68,
            ErrorKind::CommandFailed => 1,
        }
    }
}

impl error::Error for Error {

    fn description(&self) -> &str {
        &self.message
    }
}

impl fmt::Display for Error {

    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.message)
    }
}