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
148
149
150
151
152
153
154
155
156
pub use anyhow::{Context, Error};
use std::fmt;
#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ErrorKind {
#[error("TooBig: Argument list too long")]
TooBig,
#[error("Badf: Bad file descriptor")]
Badf,
#[error("Ilseq: Illegal byte sequence")]
Ilseq,
#[error("Io: I/O error")]
Io,
#[error("Nametoolong: Filename too long")]
Nametoolong,
#[error("Notdir: Not a directory or a symbolic link to a directory")]
Notdir,
#[error("Notsup: Not supported, or operation not supported on socket")]
Notsup,
#[error("Overflow: Value too large to be stored in data type")]
Overflow,
#[error("Range: Result too large")]
Range,
#[error("Spipe: Invalid seek")]
Spipe,
#[error("Permission denied")]
Perm,
}
pub trait ErrorExt {
fn trap(msg: impl Into<String>) -> Self;
fn not_found() -> Self;
fn too_big() -> Self;
fn badf() -> Self;
fn exist() -> Self;
fn illegal_byte_sequence() -> Self;
fn invalid_argument() -> Self;
fn io() -> Self;
fn name_too_long() -> Self;
fn not_dir() -> Self;
fn not_supported() -> Self;
fn overflow() -> Self;
fn range() -> Self;
fn seek_pipe() -> Self;
fn perm() -> Self;
}
impl ErrorExt for Error {
fn trap(msg: impl Into<String>) -> Self {
anyhow::anyhow!(msg.into())
}
fn not_found() -> Self {
std::io::Error::from(std::io::ErrorKind::NotFound).into()
}
fn too_big() -> Self {
ErrorKind::TooBig.into()
}
fn badf() -> Self {
ErrorKind::Badf.into()
}
fn exist() -> Self {
std::io::Error::from(std::io::ErrorKind::AlreadyExists).into()
}
fn illegal_byte_sequence() -> Self {
ErrorKind::Ilseq.into()
}
fn invalid_argument() -> Self {
std::io::Error::from(std::io::ErrorKind::InvalidInput).into()
}
fn io() -> Self {
ErrorKind::Io.into()
}
fn name_too_long() -> Self {
ErrorKind::Nametoolong.into()
}
fn not_dir() -> Self {
ErrorKind::Notdir.into()
}
fn not_supported() -> Self {
ErrorKind::Notsup.into()
}
fn overflow() -> Self {
ErrorKind::Overflow.into()
}
fn range() -> Self {
ErrorKind::Range.into()
}
fn seek_pipe() -> Self {
ErrorKind::Spipe.into()
}
fn perm() -> Self {
ErrorKind::Perm.into()
}
}
#[derive(Debug)]
pub struct I32Exit(pub i32);
impl fmt::Display for I32Exit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Exited with i32 exit status {}", self.0)
}
}
impl std::error::Error for I32Exit {}