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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::error;
use std::fmt;
use std::result;
use std::convert::From;
use std::str::Utf8Error;
use std::num::ParseIntError;

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

pub struct Error {
    repr: Repr,
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(&self.repr, f)
    }
}

#[derive(Debug)]
enum Repr {
    Io(std::io::Error),
    ParseInt(ParseIntError),
    Utf8(Utf8Error),
    Simple(ErrorKind),
    Description(ErrorKind, &'static str),
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ErrorKind {
    Io,
    InvalidCommand,
    WrongNumberOfArguments,
    Parse,
}

impl ErrorKind {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            ErrorKind::Io => "Io error",
            ErrorKind::InvalidCommand => "invalid command",
            ErrorKind::WrongNumberOfArguments => "wrong number of arguments",
            ErrorKind::Parse => "parse error",
        }
    }

    pub(crate) fn with_description(self, description: &'static str) -> Error {
        Error {
            repr: Repr::Description(self, description)
        }
    }
}

impl From<ErrorKind> for Error {
    #[inline]
    fn from(kind: ErrorKind) -> Error {
        Error {
            repr: Repr::Simple(kind)
        }
    }
}

impl Error {
    /// Returns the corresponding `ErrorKind` for this error.
    pub fn kind(&self) -> ErrorKind {
        match self.repr {
            Repr::Io(_) => ErrorKind::Io,
            Repr::ParseInt(_) => ErrorKind::Parse,
            Repr::Utf8(_) => ErrorKind::Parse,
            Repr::Simple(kind) => kind,
            Repr::Description(kind, _) => kind,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self.repr {
            Repr::Io(ref err) => write!(fmt, "io error: {}", err),
            Repr::ParseInt(ref err) => write!(fmt, "parse int error: {}", err),
            Repr::Utf8(err) => write!(fmt, "utf8 error: {}", err),
            Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
            Repr::Description(kind, description) => write!(fmt, "{}: {}", kind.as_str(), description),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match self.repr {
            Repr::Simple(..) => self.kind().as_str(),
            Repr::Description(_, description) => description,
            Repr::Io(ref err) => err.description(),
            Repr::ParseInt(ref err) => err.description(),
            Repr::Utf8(ref err) => err.description(),
        }
    }

    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self.repr {
            Repr::Io(ref err) => err.source(),
            Repr::ParseInt(ref err) => err.source(),
            Repr::Utf8(ref err) => err.source(),
            Repr::Simple(..) => None,
            Repr::Description(..) => None,
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Error {
        Error {
            repr: Repr::Io(err),
        }
    }
}

impl From<ParseIntError> for Error {
    fn from(err: ParseIntError) -> Error {
        Error {
            repr: Repr::ParseInt(err),
        }
    }
}

impl From<Utf8Error> for Error {
    fn from(err: Utf8Error) -> Error {
        Error {
            repr: Repr::Utf8(err),
        }
    }
}