1use core::fmt;
32
33#[must_use]
35#[repr(transparent)]
36#[derive(Copy, Clone, Debug, Eq, PartialEq)]
37pub struct Status(pub usize);
38
39impl Status {
40 pub const ERROR_BIT: usize = 1 << (usize::BITS - 1);
41
42 pub const SUCCESS: Self = Self(0);
45
46 pub const WARN_UNKNOWN_GLYPH: Self = Self(1);
50 pub const WARN_DELETE_FAILURE: Self = Self(2);
52 pub const WARN_WRITE_FAILURE: Self = Self(3);
54 pub const WARN_BUFFER_TOO_SMALL: Self = Self(4);
56 pub const WARN_STALE_DATA: Self = Self(5);
59 pub const WARN_FILE_SYSTEM: Self = Self(6);
61 pub const WARN_RESET_REQUIRED: Self = Self(7);
63
64 pub const LOAD_ERROR: Self = Self(Self::ERROR_BIT | 1);
67 pub const INVALID_PARAMETER: Self = Self(Self::ERROR_BIT | 2);
69 pub const UNSUPPORTED: Self = Self(Self::ERROR_BIT | 3);
71 pub const BAD_BUFFER_SIZE: Self = Self(Self::ERROR_BIT | 4);
73 pub const BUFFER_TOO_SMALL: Self = Self(Self::ERROR_BIT | 5);
75 pub const NOT_READY: Self = Self(Self::ERROR_BIT | 6);
77 pub const DEVICE_ERROR: Self = Self(Self::ERROR_BIT | 7);
79 pub const WRITE_PROTECTED: Self = Self(Self::ERROR_BIT | 8);
81 pub const OUT_OF_RESOURCES: Self = Self(Self::ERROR_BIT | 9);
83 pub const VOLUME_CORRUPTED: Self = Self(Self::ERROR_BIT | 10);
85 pub const VOLUME_FULL: Self = Self(Self::ERROR_BIT | 11);
87 pub const NO_MEDIA: Self = Self(Self::ERROR_BIT | 12);
89 pub const MEDIA_CHANGED: Self = Self(Self::ERROR_BIT | 13);
91 pub const NOT_FOUND: Self = Self(Self::ERROR_BIT | 14);
93 pub const ACCESS_DENIED: Self = Self(Self::ERROR_BIT | 15);
95 pub const NO_RESPONSE: Self = Self(Self::ERROR_BIT | 16);
97 pub const NO_MAPPING: Self = Self(Self::ERROR_BIT | 17);
99 pub const TIMEOUT: Self = Self(Self::ERROR_BIT | 18);
101 pub const NOT_STARTED: Self = Self(Self::ERROR_BIT | 19);
103 pub const ALREADY_STARTED: Self = Self(Self::ERROR_BIT | 20);
105 pub const ABORTED: Self = Self(Self::ERROR_BIT | 21);
107 pub const ICMP_ERROR: Self = Self(Self::ERROR_BIT | 22);
109 pub const TFTP_ERROR: Self = Self(Self::ERROR_BIT | 23);
111 pub const PROTOCOL_ERROR: Self = Self(Self::ERROR_BIT | 24);
113 pub const INCOMPATIBLE_VERSION: Self = Self(Self::ERROR_BIT | 25);
116 pub const SECURITY_VIOLATION: Self = Self(Self::ERROR_BIT | 26);
118 pub const CRC_ERROR: Self = Self(Self::ERROR_BIT | 27);
120 pub const END_OF_MEDIA: Self = Self(Self::ERROR_BIT | 28);
122 pub const END_OF_FILE: Self = Self(Self::ERROR_BIT | 31);
125 pub const INVALID_LANGUAGE: Self = Self(Self::ERROR_BIT | 32);
127 pub const COMPROMISED_DATA: Self = Self(Self::ERROR_BIT | 33);
130 pub const IP_ADDRESS_CONFLICT: Self = Self(Self::ERROR_BIT | 34);
132 pub const HTTP_ERROR: Self = Self(Self::ERROR_BIT | 35);
134
135 pub fn is_success(&self) -> bool {
137 self == &Self::SUCCESS
138 }
139
140 pub fn is_error(&self) -> bool {
142 (self.0 & Self::ERROR_BIT) == Self::ERROR_BIT
143 }
144
145 pub fn is_warning(&self) -> bool {
147 !self.is_error() && !self.is_success()
148 }
149}
150
151impl From<usize> for Status {
152 fn from(value: usize) -> Self {
153 Self(value)
154 }
155}
156
157impl fmt::Display for Status {
158 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
159 match *self {
160 Self::SUCCESS => write!(f, "success"),
161
162 Self::WARN_UNKNOWN_GLYPH => write!(f, "unknown glyph"),
163 Self::WARN_DELETE_FAILURE => write!(f, "delete failure"),
164 Self::WARN_WRITE_FAILURE => write!(f, "write failure"),
165 Self::WARN_STALE_DATA => write!(f, "stale data"),
166 Self::WARN_FILE_SYSTEM => write!(f, "file system"),
167 Self::WARN_RESET_REQUIRED => write!(f, "reset required"),
168
169 Self::LOAD_ERROR => write!(f, "load error"),
170 Self::INVALID_PARAMETER => write!(f, "invalid paramter"),
171 Self::UNSUPPORTED => write!(f, "unsupported"),
172 Self::BAD_BUFFER_SIZE => write!(f, "bad buffer size"),
173 Self::WARN_BUFFER_TOO_SMALL | Self::BUFFER_TOO_SMALL => write!(f, "buffer too small"),
174 Self::NOT_READY => write!(f, "not ready"),
175 Self::DEVICE_ERROR => write!(f, "device error"),
176 Self::WRITE_PROTECTED => write!(f, "write protected"),
177 Self::OUT_OF_RESOURCES => write!(f, "out of resources"),
178 Self::VOLUME_CORRUPTED => write!(f, "volume corrupted"),
179 Self::VOLUME_FULL => write!(f, "volume full"),
180 Self::NO_MEDIA => write!(f, "no media"),
181 Self::MEDIA_CHANGED => write!(f, "media changed"),
182 Self::NOT_FOUND => write!(f, "not found"),
183 Self::ACCESS_DENIED => write!(f, "acccess denied"),
184 Self::NO_RESPONSE => write!(f, "no response"),
185 Self::NO_MAPPING => write!(f, "no mapping"),
186 Self::TIMEOUT => write!(f, "timeout"),
187 Self::NOT_STARTED => write!(f, "not started"),
188 Self::ALREADY_STARTED => write!(f, "already started"),
189 Self::ABORTED => write!(f, "aborted"),
190 Self::ICMP_ERROR => write!(f, "ICMP error"),
191 Self::TFTP_ERROR => write!(f, "TFTP error"),
192 Self::PROTOCOL_ERROR => write!(f, "protocol error"),
193 Self::INCOMPATIBLE_VERSION => write!(f, "incompatible version"),
194 Self::SECURITY_VIOLATION => write!(f, "security violation"),
195 Self::CRC_ERROR => write!(f, "CRC error"),
196 Self::END_OF_MEDIA => write!(f, "end of media"),
197 Self::END_OF_FILE => write!(f, "end of file"),
198 Self::INVALID_LANGUAGE => write!(f, "invalid language"),
199 Self::COMPROMISED_DATA => write!(f, "compromised data"),
200 Self::IP_ADDRESS_CONFLICT => write!(f, "IP address conflict"),
201 Self::HTTP_ERROR => write!(f, "HTTP error"),
202
203 _ => write!(f, "{:#X}", self.0),
204 }
205 }
206}
207
208impl fmt::LowerHex for Status {
209 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
210 fmt::LowerHex::fmt(&self.0, f)
211 }
212}
213
214impl fmt::UpperHex for Status {
215 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
216 fmt::UpperHex::fmt(&self.0, f)
217 }
218}
219
220pub type Result<T> = core::result::Result<T, Status>;
221
222impl From<Status> for Result<()> {
223 fn from(status: Status) -> Self {
224 match status {
225 Status::SUCCESS => Ok(()),
226 e => Err(e),
227 }
228 }
229}
230
231impl<T> From<Result<T>> for Status {
232 fn from(res: Result<T>) -> Self {
233 match res {
234 Ok(_) => Status::SUCCESS,
235 Err(e) => e,
236 }
237 }
238}