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
use ebacktrace::define_error;
use std::{
io, result,
fmt::{ self, Display, Formatter }
};
#[macro_export] macro_rules! e {
($kind:expr, $($arg:tt)*) => ({ $crate::error::ErrorImpl::with_string($kind, format!($($arg)*)) })
}
#[macro_export] macro_rules! eio {
($($arg:tt)*) => ({ e!($crate::error::ErrorKind::InOutError, $($arg)*) });
}
#[macro_export] macro_rules! einval {
($($arg:tt)*) => ({ e!($crate::error::ErrorKind::InvalidValue, $($arg)*) });
}
#[derive(Debug, PartialEq, Eq)]
pub enum ErrorKind {
InOutError,
InvalidValue
}
impl Display for ErrorKind {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::InOutError => write!(f, "An I/O-error occurred"),
Self::InvalidValue => write!(f, "A value is invalid")
}
}
}
define_error!(ErrorImpl);
impl From<io::Error> for ErrorImpl<ErrorKind> {
fn from(underlying: io::Error) -> Self {
ErrorImpl::with_string(ErrorKind::InOutError, underlying)
}
}
pub type Error = ErrorImpl<ErrorKind>;
pub type Result<T = ()> = result::Result<T, ErrorImpl<ErrorKind>>;