1use std::fmt::{self, Display};
6
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub enum ErrorKind {
9 IoError,
10 ParameterError,
11 Utf8Error,
12 SyscallError,
13 InvalidArchError,
14 ParseNumError,
15
16 PwdError,
17 PwdGroupError,
18}
19
20#[derive(Debug, Clone, PartialEq)]
21pub struct Error {
22 pub kind: ErrorKind,
23 pub message: String,
24}
25
26impl Error {
27 pub fn new(kind: ErrorKind, message: &str) -> Self {
28 Error {
29 kind,
30 message: message.to_owned(),
31 }
32 }
33
34 pub fn from_string(kind: ErrorKind, message: String) -> Self {
35 Error { kind, message }
36 }
37}
38
39impl Display for Error {
40 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41 write!(f, "{:?}: {}", self.kind, self.message)
42 }
43}
44
45impl std::error::Error for Error {}
46
47impl From<std::io::Error> for Error {
48 fn from(err: std::io::Error) -> Self {
49 Error::from_string(ErrorKind::IoError, format!("IoError {}", err))
50 }
51}
52
53impl From<std::string::FromUtf8Error> for Error {
54 fn from(err: std::string::FromUtf8Error) -> Self {
55 Error::from_string(ErrorKind::Utf8Error, format!("err: {}", err))
56 }
57}
58
59impl From<std::str::Utf8Error> for Error {
60 fn from(err: std::str::Utf8Error) -> Self {
61 Error::from_string(ErrorKind::Utf8Error, format!("err: {}", err))
62 }
63}
64
65impl From<std::ffi::IntoStringError> for Error {
66 fn from(err: std::ffi::IntoStringError) -> Self {
67 Error::from_string(ErrorKind::Utf8Error, format!("err: {}", err))
68 }
69}
70
71impl From<std::num::ParseIntError> for Error {
72 fn from(err: std::num::ParseIntError) -> Self {
73 Error::from_string(ErrorKind::ParseNumError, format!("err: {}", err))
74 }
75}
76
77impl From<std::num::ParseFloatError> for Error {
78 fn from(err: std::num::ParseFloatError) -> Self {
79 Error::from_string(ErrorKind::ParseNumError, format!("err: {}", err))
80 }
81}
82
83impl From<nc::Errno> for Error {
84 fn from(errno: nc::Errno) -> Self {
85 Error::from_string(
86 ErrorKind::SyscallError,
87 format!("error: {:?}", nc::strerror(errno)),
88 )
89 }
90}