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
use std::{fmt, path::PathBuf};
use crate::ExitCode;
#[derive(Debug)]
pub enum CliError {
Config,
CreateFile(PathBuf),
InputData,
InputFileNotFound(PathBuf),
NoUser(String),
NoHost(String),
OperationPermission(String),
OsErr,
OsFileNotFound(PathBuf),
ReadFile(PathBuf),
ResourceNotFound(String),
Protocol,
Temporary,
Usage,
WriteFile(PathBuf),
}
impl std::error::Error for CliError {}
impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use CliError::*;
match self {
Config => write!(f, "invalid configuration"),
CreateFile(file) => write!(f, "cannot create file: {}", file.display()),
InputData => write!(f, "invalid input data"),
InputFileNotFound(file) => write!(f, "file not found: {}", file.display()),
NoUser(user) => write!(f, "user not found: {}", user),
NoHost(host) => write!(f, "host not found: {}", host),
OperationPermission(op) => write!(f, "no permission for operation: {}", op),
OsErr => write!(f, "operating system error"),
OsFileNotFound(file) => write!(f, "system file not found: {}", file.display()),
ReadFile(file) => write!(f, "cannot read file: {}", file.display()),
ResourceNotFound(resource) => write!(f, "resource not found: {}", resource),
Protocol => write!(f, "protocol not possible"),
Temporary => write!(f, "temporary failure"),
Usage => write!(f, "incorrect usage"),
WriteFile(file) => write!(f, "cannot write to file: {}", file.display()),
}
}
}
impl ExitCode for CliError {
fn exit_code(&self) -> i32 {
self.into()
}
}
impl From<CliError> for i32 {
fn from(err: CliError) -> Self {
(&err).into()
}
}
impl From<&CliError> for i32 {
fn from(err: &CliError) -> Self {
use exitcode::*;
use CliError::*;
match err {
Config => CONFIG,
CreateFile(_) => CANTCREAT,
InputData | ResourceNotFound(_) => DATAERR,
InputFileNotFound(_) => NOINPUT,
NoUser(_) => NOUSER,
NoHost(_) => NOHOST,
OperationPermission(_) => NOPERM,
OsErr => OSERR,
OsFileNotFound(_) => OSFILE,
ReadFile(_) | WriteFile(_) => IOERR,
Protocol => PROTOCOL,
Temporary => TEMPFAIL,
Usage => USAGE,
}
}
}