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
use std::error;
use std::fmt;
use std::fmt::Display;
#[derive(Debug)]
pub enum Error {
Generic(GenericError),
FromUtf8(std::string::FromUtf8Error),
ParseInt(std::num::ParseIntError),
}
impl Error {
pub fn generic(message: &str) -> Error {
Error::Generic(GenericError::new(message))
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(err: std::string::FromUtf8Error) -> Error {
Error::FromUtf8(err)
}
}
impl From<std::num::ParseIntError> for Error {
fn from(err: std::num::ParseIntError) -> Error {
Error::ParseInt(err)
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Generic(ref err) => write!(f, "{}", err),
Error::FromUtf8(ref err) => write!(f, "{}", err),
Error::ParseInt(ref err) => write!(f, "{}", err),
}
}
}
impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::Generic(ref err) => Some(err),
Error::FromUtf8(ref err) => Some(err),
Error::ParseInt(ref err) => Some(err),
}
}
}
#[derive(Debug)]
pub struct GenericError {
message: String,
}
impl GenericError {
pub fn new(message: &str) -> GenericError {
GenericError {
message: String::from(message),
}
}
}
impl<'a> Display for GenericError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Store error: {}", self.message)
}
}
impl<'a> error::Error for GenericError {
fn description(&self) -> &str {
self.message.as_str()
}
fn cause(&self) -> Option<&dyn error::Error> {
None
}
}