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
use std::fmt::{self, Display};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ErrorKind {
IoError,
ParameterError,
Utf8Error,
SyscallError,
InvalidArchError,
ParseNumError,
PwdError,
PwdGroupError,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Error {
pub kind: ErrorKind,
pub message: String,
}
impl Error {
pub fn new(kind: ErrorKind, message: &str) -> Self {
Error {
kind,
message: message.to_owned(),
}
}
pub fn from_string(kind: ErrorKind, message: String) -> Self {
Error { kind, message }
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}: {}", self.kind, self.message)
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::from_string(ErrorKind::IoError, format!("IoError {}", err))
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(err: std::string::FromUtf8Error) -> Self {
Error::from_string(ErrorKind::Utf8Error, format!("err: {}", err))
}
}
impl From<std::str::Utf8Error> for Error {
fn from(err: std::str::Utf8Error) -> Self {
Error::from_string(ErrorKind::Utf8Error, format!("err: {}", err))
}
}
impl From<std::ffi::IntoStringError> for Error {
fn from(err: std::ffi::IntoStringError) -> Self {
Error::from_string(ErrorKind::Utf8Error, format!("err: {}", err))
}
}
impl From<std::num::ParseIntError> for Error {
fn from(err: std::num::ParseIntError) -> Self {
Error::from_string(ErrorKind::ParseNumError, format!("err: {}", err))
}
}
impl From<std::num::ParseFloatError> for Error {
fn from(err: std::num::ParseFloatError) -> Self {
Error::from_string(ErrorKind::ParseNumError, format!("err: {}", err))
}
}
impl From<nc::Errno> for Error {
fn from(errno: nc::Errno) -> Self {
Error::from_string(
ErrorKind::SyscallError,
format!("error: {:?}", nc::strerror(errno)),
)
}
}