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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use super::{Completion, Error, Result};
use core::fmt::Debug;
use core::ops;
const ERROR_BIT: usize = 1 << (core::mem::size_of::<usize>() * 8 - 1);
newtype_enum! {
#[must_use]
pub enum Status: usize => {
SUCCESS = 0,
WARN_UNKNOWN_GLYPH = 1,
WARN_DELETE_FAILURE = 2,
WARN_WRITE_FAILURE = 3,
WARN_BUFFER_TOO_SMALL = 4,
WARN_STALE_DATA = 5,
WARN_FILE_SYSTEM = 6,
WARN_RESET_REQUIRED = 7,
LOAD_ERROR = ERROR_BIT | 1,
INVALID_PARAMETER = ERROR_BIT | 2,
UNSUPPORTED = ERROR_BIT | 3,
BAD_BUFFER_SIZE = ERROR_BIT | 4,
BUFFER_TOO_SMALL = ERROR_BIT | 5,
NOT_READY = ERROR_BIT | 6,
DEVICE_ERROR = ERROR_BIT | 7,
WRITE_PROTECTED = ERROR_BIT | 8,
OUT_OF_RESOURCES = ERROR_BIT | 9,
VOLUME_CORRUPTED = ERROR_BIT | 10,
VOLUME_FULL = ERROR_BIT | 11,
NO_MEDIA = ERROR_BIT | 12,
MEDIA_CHANGED = ERROR_BIT | 13,
NOT_FOUND = ERROR_BIT | 14,
ACCESS_DENIED = ERROR_BIT | 15,
NO_RESPONSE = ERROR_BIT | 16,
NO_MAPPING = ERROR_BIT | 17,
TIMEOUT = ERROR_BIT | 18,
NOT_STARTED = ERROR_BIT | 19,
ALREADY_STARTED = ERROR_BIT | 20,
ABORTED = ERROR_BIT | 21,
ICMP_ERROR = ERROR_BIT | 22,
TFTP_ERROR = ERROR_BIT | 23,
PROTOCOL_ERROR = ERROR_BIT | 24,
INCOMPATIBLE_VERSION = ERROR_BIT | 25,
SECURITY_VIOLATION = ERROR_BIT | 26,
CRC_ERROR = ERROR_BIT | 27,
END_OF_MEDIA = ERROR_BIT | 28,
END_OF_FILE = ERROR_BIT | 31,
INVALID_LANGUAGE = ERROR_BIT | 32,
COMPROMISED_DATA = ERROR_BIT | 33,
IP_ADDRESS_CONFLICT = ERROR_BIT | 34,
HTTP_ERROR = ERROR_BIT | 35,
}}
impl Status {
#[inline]
pub fn is_success(self) -> bool {
self == Status::SUCCESS
}
#[inline]
pub fn is_warning(self) -> bool {
(self != Status::SUCCESS) && (self.0 & ERROR_BIT == 0)
}
#[inline]
pub fn is_error(self) -> bool {
self.0 & ERROR_BIT != 0
}
#[inline]
#[allow(clippy::clippy::result_unit_err)]
pub fn into_with_val<T>(self, val: impl FnOnce() -> T) -> Result<T, ()> {
if !self.is_error() {
Ok(Completion::new(self, val()))
} else {
Err(self.into())
}
}
#[inline]
pub fn into_with_err<ErrData: Debug>(
self,
err: impl FnOnce(Status) -> ErrData,
) -> Result<(), ErrData> {
if !self.is_error() {
Ok(self.into())
} else {
Err(Error::new(self, err(self)))
}
}
#[inline]
pub fn into_with<T, ErrData: Debug>(
self,
val: impl FnOnce() -> T,
err: impl FnOnce(Status) -> ErrData,
) -> Result<T, ErrData> {
if !self.is_error() {
Ok(Completion::new(self, val()))
} else {
Err(Error::new(self, err(self)))
}
}
}
impl From<Status> for Result<(), ()> {
#[inline]
fn from(status: Status) -> Result<(), ()> {
status.into_with(|| (), |_| ())
}
}
impl ops::Try for Status {
type Ok = Completion<()>;
type Error = Error<()>;
fn into_result(self) -> Result<(), ()> {
self.into()
}
fn from_error(error: Self::Error) -> Self {
error.status()
}
fn from_ok(ok: Self::Ok) -> Self {
ok.status()
}
}
impl From<ucs2::Error> for Status {
fn from(other: ucs2::Error) -> Self {
use ucs2::Error;
match other {
Error::BufferOverflow => Status::BUFFER_TOO_SMALL,
Error::MultiByte => Status::UNSUPPORTED,
}
}
}