1#[derive(Debug, Copy, Eq, PartialEq, Clone)]
18pub enum MsgCodeClass {
19 Method = 0,
21
22 Success = 2,
24
25 ClientError = 4,
27
28 ServerError = 5,
30
31 Signal = 7,
33}
34
35impl MsgCodeClass {
36 pub fn try_from(x: u8) -> Option<MsgCodeClass> {
38 match x {
39 0 => Some(MsgCodeClass::Method),
40 2 => Some(MsgCodeClass::Success),
41 4 => Some(MsgCodeClass::ClientError),
42 5 => Some(MsgCodeClass::ServerError),
43 7 => Some(MsgCodeClass::Signal),
44 _ => None,
45 }
46 }
47
48 pub fn contains(self, code: MsgCode) -> bool {
50 let code_u8 = code as u8;
51
52 code_u8 != 0 && (code_u8 >> 5) == self as u8
53 }
54}
55
56const fn calc_code(class: u8, detail: u8) -> isize {
58 (((class & 0x7) << 5) + detail) as isize
59}
60
61#[derive(Debug, Copy, Eq, PartialEq, Clone)]
63pub enum MsgCode {
64 Empty = 0x00,
66
67 MethodGet = 0x01,
69
70 MethodPost = 0x02,
72
73 MethodPut = 0x03,
75
76 MethodDelete = 0x04,
78
79 MethodFetch = 0x05,
81
82 MethodPatch = 0x06,
84
85 MethodIPatch = 0x07,
87
88 SuccessCreated = 0x41,
90
91 SuccessDeleted = 0x42,
93
94 SuccessValid = 0x43,
96
97 SuccessChanged = 0x44,
99
100 SuccessContent = 0x45,
102
103 SuccessContinue = 0x5F,
105
106 ClientErrorBadRequest = 0x80,
108
109 ClientErrorUnauthorized = 0x81,
111
112 ClientErrorBadOption = 0x82,
114
115 ClientErrorForbidden = 0x83,
117
118 ClientErrorNotFound = 0x84,
120
121 ClientErrorMethodNotAllowed = 0x85,
123
124 ClientErrorNotAcceptable = 0x86,
126
127 ClientErrorRequestEntityIncomplete = 0x88,
129
130 ClientErrorPreconditionFailed = 0x8C,
132
133 ClientErrorRequestEntityTooLarge = 0x8D,
135
136 ClientErrorUnsupportedMediaType = 0x8F,
138
139 ClientErrorTooManyRequests = calc_code(4, 29),
141
142 ServerErrorInternalServerError = 0xA0,
144
145 ServerErrorNotImplemented = 0xA1,
147
148 ServerErrorBadGateway = 0xA2,
150
151 ServerErrorServiceUnavailable = 0xA3,
153
154 ServerErrorGatewayTimeout = 0xA4,
156
157 ServerErrorProxyingNotSupported = 0xA5,
159
160 SignalCsm = 0xE1,
162
163 SignalPing = 0xE2,
165
166 SignalPong = 0xE3,
168
169 SignalRelease = 0xE4,
171
172 SignalAbort = 0xE5,
174}
175
176impl MsgCode {
177 pub fn try_from(x: u8) -> Option<MsgCode> {
180 use MsgCode::*;
181 match x {
182 0x00 => Some(Empty),
183 0x01 => Some(MethodGet),
184 0x02 => Some(MethodPost),
185 0x03 => Some(MethodPut),
186 0x04 => Some(MethodDelete),
187
188 0x41 => Some(SuccessCreated),
189 0x42 => Some(SuccessDeleted),
190 0x43 => Some(SuccessValid),
191 0x44 => Some(SuccessChanged),
192 0x45 => Some(SuccessContent),
193 0x5F => Some(SuccessContinue),
194
195 0x80 => Some(ClientErrorBadRequest),
196 0x81 => Some(ClientErrorUnauthorized),
197 0x82 => Some(ClientErrorBadOption),
198 0x83 => Some(ClientErrorForbidden),
199 0x84 => Some(ClientErrorNotFound),
200 0x85 => Some(ClientErrorMethodNotAllowed),
201 0x86 => Some(ClientErrorNotAcceptable),
202 0x88 => Some(ClientErrorRequestEntityIncomplete),
203 0x8C => Some(ClientErrorPreconditionFailed),
204 0x8D => Some(ClientErrorRequestEntityTooLarge),
205 0x8F => Some(ClientErrorUnsupportedMediaType),
206 0x9D => Some(ClientErrorTooManyRequests),
207
208 0xA0 => Some(ServerErrorInternalServerError),
209 0xA1 => Some(ServerErrorNotImplemented),
210 0xA2 => Some(ServerErrorBadGateway),
211 0xA3 => Some(ServerErrorServiceUnavailable),
212 0xA4 => Some(ServerErrorGatewayTimeout),
213 0xA5 => Some(ServerErrorProxyingNotSupported),
214
215 0xE1 => Some(SignalCsm),
216 0xE2 => Some(SignalPing),
217 0xE3 => Some(SignalPong),
218 0xE4 => Some(SignalRelease),
219 0xE5 => Some(SignalAbort),
220
221 _ => None,
222 }
223 }
224
225 pub fn to_http_code(self) -> u16 {
227 ((self as u8) >> 5) as u16 * 100 + (self as u8 as u16) & 0b11111
228 }
229
230 pub fn is_empty(self) -> bool {
232 self as u8 == 0
233 }
234
235 pub fn is_method(self) -> bool {
237 MsgCodeClass::Method.contains(self)
238 }
239
240 pub fn is_client_error(self) -> bool {
242 MsgCodeClass::ClientError.contains(self)
243 }
244
245 pub fn is_server_error(self) -> bool {
247 MsgCodeClass::ServerError.contains(self)
248 }
249
250 pub fn is_error(self) -> bool {
252 self.is_client_error() || self.is_server_error()
253 }
254
255 pub fn is_success(self) -> bool {
257 MsgCodeClass::Success.contains(self)
258 }
259
260 pub fn is_signal(self) -> bool {
262 MsgCodeClass::Signal.contains(self)
263 }
264}
265
266impl Default for MsgCode {
267 fn default() -> Self {
268 MsgCode::Empty
269 }
270}
271
272impl core::convert::From<MsgCode> for u8 {
273 fn from(code: MsgCode) -> Self {
274 code as u8
275 }
276}
277
278impl core::convert::From<MsgCode> for u16 {
279 fn from(code: MsgCode) -> Self {
280 code as u16
281 }
282}
283
284impl core::convert::From<MsgCode> for u32 {
285 fn from(code: MsgCode) -> Self {
286 code as u32
287 }
288}