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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::error;
use std::ffi;
use std::fmt;
use std::io;
use std::num;
use std::net;
use std::result;
use std::string;
use tokio::sync::mpsc;
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub enum ErrorKind {
FromNul(ffi::NulError),
FromFfiString(ffi::IntoStringError),
Io(io::Error),
Parse,
ParseInt(num::ParseIntError),
ParseFloat(num::ParseFloatError),
ParseString(string::ParseError),
FromUtf8(string::FromUtf8Error),
Other(Box<dyn error::Error + Send + 'static>),
}
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
}
impl Error {
pub fn new(kind: ErrorKind) -> Error {
Error {
kind,
}
}
pub fn last_os_error() -> Error {
io::Error::last_os_error().into()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.kind {
ErrorKind::FromNul(e) => fmt::Display::fmt(e, f),
ErrorKind::FromFfiString(e) => fmt::Display::fmt(e, f),
ErrorKind::Io(e) => fmt::Display::fmt(e, f),
ErrorKind::Parse => f.write_str("Unable to parse"),
ErrorKind::ParseInt(e) => fmt::Display::fmt(e, f),
ErrorKind::ParseFloat(e) => fmt::Display::fmt(e, f),
ErrorKind::ParseString(e) => fmt::Display::fmt(e, f),
ErrorKind::FromUtf8(e) => fmt::Display::fmt(e, f),
ErrorKind::Other(e) => fmt::Display::fmt(e, f),
}
}
}
impl error::Error for Error {}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error {
kind: ErrorKind::Io(e),
}
}
}
impl From<num::ParseIntError> for Error {
fn from(e: num::ParseIntError) -> Self {
Error {
kind: ErrorKind::ParseInt(e),
}
}
}
impl From<num::ParseFloatError> for Error {
fn from(e: num::ParseFloatError) -> Self {
Error {
kind: ErrorKind::ParseFloat(e),
}
}
}
impl From<ffi::NulError> for Error {
fn from(e: ffi::NulError) -> Self {
Error {
kind: ErrorKind::FromNul(e),
}
}
}
impl From<ffi::IntoStringError> for Error {
fn from(e: ffi::IntoStringError) -> Self {
Error {
kind: ErrorKind::FromFfiString(e),
}
}
}
impl From<string::ParseError> for Error {
fn from(e: string::ParseError) -> Self {
Error {
kind: ErrorKind::ParseString(e),
}
}
}
impl From<string::FromUtf8Error> for Error {
fn from(e: string::FromUtf8Error) -> Self {
Error {
kind: ErrorKind::FromUtf8(e),
}
}
}
impl From<mpsc::error::UnboundedSendError> for Error {
fn from(e: mpsc::error::UnboundedSendError) -> Self {
Error {
kind: ErrorKind::Io(io::Error::new(io::ErrorKind::Other, e)),
}
}
}
impl From<net::AddrParseError> for Error {
fn from(_e: net::AddrParseError) -> Self {
Error {
kind: ErrorKind::Parse,
}
}
}
impl<T> From<Box<T>> for Error where T: error::Error + Send + 'static {
fn from(e: Box<T>) -> Self {
Error {
kind: ErrorKind::Other(e),
}
}
}
#[cfg(unix)]
impl From<nix::Error> for Error {
fn from(_e: nix::Error) -> Self {
unimplemented!()
}
}