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
use ebacktrace::define_error;
use std::{
error, result,
fmt::{ self, Display, Formatter }
};
#[doc(hidden)]
#[macro_export] macro_rules! e {
($kind:expr, $($arg:tt)*) => ({ $crate::error::ErrorImpl::with_string($kind, format!($($arg)*)) })
}
#[macro_export] macro_rules! eserde {
($($arg:tt)*) => ({ e!($crate::error::ErrorKind::SerdeError, $($arg)*) });
}
#[macro_export] macro_rules! einval {
($($arg:tt)*) => ({ e!($crate::error::ErrorKind::InvalidData, $($arg)*) });
}
#[macro_export] macro_rules! etype {
($($arg:tt)*) => ({ e!($crate::error::ErrorKind::TypeError, $($arg)*) });
}
#[macro_export] macro_rules! enokey {
($($arg:tt)*) => ({ e!($crate::error::ErrorKind::NoSuchKey, $($arg)*) });
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ErrorKind {
SerdeError,
InvalidData,
TypeError,
NoSuchKey
}
impl Display for ErrorKind {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::SerdeError => write!(f, "Serde error"),
Self::InvalidData => write!(f, "Invalid encoded data"),
Self::TypeError => write!(f, "Failed to convert a value from/to the given type"),
Self::NoSuchKey => write!(f, "There is no such key or no value for the given key")
}
}
}
impl error::Error for ErrorKind {
}
define_error!(ErrorImpl);
impl serde::de::Error for ErrorImpl<ErrorKind> {
fn custom<T>(msg: T) -> Self where T: Display {
Self::with_string(ErrorKind::SerdeError, msg.to_string())
}
}
impl serde::ser::Error for ErrorImpl<ErrorKind> {
fn custom<T>(msg: T) -> Self where T: Display {
Self::with_string(ErrorKind::SerdeError, msg.to_string())
}
}
pub type Error = ErrorImpl<ErrorKind>;
pub type Result<T = ()> = result::Result<T, ErrorImpl<ErrorKind>>;