http_box/http2/
error_code.rs1use std::fmt;
18
19pub const E_CANCEL: u8 = 0x8;
21
22pub const E_COMPRESSION: u8 = 0x9;
24
25pub const E_CONNECT: u8 = 0xA;
27
28pub const E_ENHANCE_YOUR_CALM: u8 = 0xB;
30
31pub const E_FLOW_CONTROL: u8 = 0x3;
33
34pub const E_FRAME_SIZE: u8 = 0x6;
36
37pub const E_HTTP_1_1_REQUIRED: u8 = 0xD;
39
40pub const E_INADEQUATE_SECURITY: u8 = 0xC;
42
43pub const E_NO_ERROR: u8 = 0x0;
45
46pub const E_INTERNAL: u8 = 0x2;
48
49pub const E_PROTOCOL: u8 = 0x1;
51
52pub const E_REFUSED_STREAM: u8 = 0x7;
54
55pub const E_SETTINGS_TIMEOUT: u8 = 0x4;
57
58pub const E_STREAM_CLOSED: u8 = 0x5;
60
61pub const E_UNSUPPORTED: u8 = 0xFF;
63
64#[derive(Clone,Copy,PartialEq)]
66#[repr(u8)]
67pub enum ErrorCode {
68 Cancel = E_CANCEL,
70
71 Compression = E_COMPRESSION,
73
74 Connect = E_CONNECT,
76
77 EnhanceYourCalm = E_ENHANCE_YOUR_CALM,
79
80 FlowControl = E_FLOW_CONTROL,
82
83 FrameSize = E_FRAME_SIZE,
85
86 Http11Required = E_HTTP_1_1_REQUIRED,
88
89 InadequateSecurity = E_INADEQUATE_SECURITY,
91
92 NoError = E_NO_ERROR,
94
95 Internal = E_INTERNAL,
97
98 Protocol = E_PROTOCOL,
100
101 RefusedStream = E_REFUSED_STREAM,
103
104 SettingsTimeout = E_SETTINGS_TIMEOUT,
106
107 StreamClosed = E_STREAM_CLOSED,
109
110 Unsupported = E_UNSUPPORTED
112}
113
114impl ErrorCode {
115 pub fn from_u8(byte: u8) -> ErrorCode {
117 match byte {
118 E_CANCEL => ErrorCode::Cancel,
119 E_COMPRESSION => ErrorCode::Compression,
120 E_CONNECT => ErrorCode::Connect,
121 E_ENHANCE_YOUR_CALM => ErrorCode::EnhanceYourCalm,
122 E_FLOW_CONTROL => ErrorCode::FlowControl,
123 E_FRAME_SIZE => ErrorCode::FrameSize,
124 E_HTTP_1_1_REQUIRED => ErrorCode::Http11Required,
125 E_INADEQUATE_SECURITY => ErrorCode::InadequateSecurity,
126 E_INTERNAL => ErrorCode::Internal,
127 E_NO_ERROR => ErrorCode::NoError,
128 E_PROTOCOL => ErrorCode::Protocol,
129 E_REFUSED_STREAM => ErrorCode::RefusedStream,
130 E_SETTINGS_TIMEOUT => ErrorCode::SettingsTimeout,
131 E_STREAM_CLOSED => ErrorCode::StreamClosed,
132 _ => ErrorCode::Unsupported
133 }
134 }
135
136 pub fn as_byte(&self) -> u8 {
138 match *self {
139 ErrorCode::Cancel => E_CANCEL,
140 ErrorCode::Compression => E_COMPRESSION,
141 ErrorCode::Connect => E_CONNECT,
142 ErrorCode::EnhanceYourCalm => E_ENHANCE_YOUR_CALM,
143 ErrorCode::FlowControl => E_FLOW_CONTROL,
144 ErrorCode::FrameSize => E_FRAME_SIZE,
145 ErrorCode::Http11Required => E_HTTP_1_1_REQUIRED,
146 ErrorCode::InadequateSecurity => E_INADEQUATE_SECURITY,
147 ErrorCode::Internal => E_INTERNAL,
148 ErrorCode::NoError => E_NO_ERROR,
149 ErrorCode::Protocol => E_PROTOCOL,
150 ErrorCode::RefusedStream => E_REFUSED_STREAM,
151 ErrorCode::SettingsTimeout => E_SETTINGS_TIMEOUT,
152 ErrorCode::StreamClosed => E_STREAM_CLOSED,
153 ErrorCode::Unsupported => E_UNSUPPORTED
154 }
155 }
156
157 fn format(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
159 match *self {
160 ErrorCode::Cancel => {
161 write!(
162 formatter,
163 "<ErrorCode::Cancel>"
164 )
165 },
166 ErrorCode::Compression => {
167 write!(
168 formatter,
169 "<ErrorCode::Compression>"
170 )
171 },
172 ErrorCode::Connect => {
173 write!(
174 formatter,
175 "<ErrorCode::Connect>"
176 )
177 },
178 ErrorCode::EnhanceYourCalm => {
179 write!(
180 formatter,
181 "<ErrorCode::EnhanceYourCalm>"
182 )
183 },
184 ErrorCode::FlowControl => {
185 write!(
186 formatter,
187 "<ErrorCode::FlowControl>"
188 )
189 },
190 ErrorCode::FrameSize => {
191 write!(
192 formatter,
193 "<ErrorCode::FrameSize>"
194 )
195 },
196 ErrorCode::Http11Required => {
197 write!(
198 formatter,
199 "<ErrorCode::Http11Required>"
200 )
201 },
202 ErrorCode::InadequateSecurity => {
203 write!(
204 formatter,
205 "<ErrorCode::InadequateSecurity>"
206 )
207 },
208 ErrorCode::Internal => {
209 write!(
210 formatter,
211 "<ErrorCode::Internal>"
212 )
213 },
214 ErrorCode::NoError => {
215 write!(
216 formatter,
217 "<ErrorCode::NoError>"
218 )
219 },
220 ErrorCode::Protocol => {
221 write!(
222 formatter,
223 "<ErrorCode::Protocol>"
224 )
225 },
226 ErrorCode::RefusedStream => {
227 write!(
228 formatter,
229 "<ErrorCode::RefusedStream>"
230 )
231 },
232 ErrorCode::SettingsTimeout => {
233 write!(
234 formatter,
235 "<ErrorCode::SettingsTimeout>"
236 )
237 },
238 ErrorCode::StreamClosed => {
239 write!(
240 formatter,
241 "<ErrorCode::StreamClosed>"
242 )
243 },
244 ErrorCode::Unsupported => {
245 write!(
246 formatter,
247 "<ErrorCode::Unsupported>"
248 )
249 }
250 }
251 }
252
253 pub fn is_cancel(&self) -> bool {
255 *self == ErrorCode::Cancel
256 }
257
258 pub fn is_compression(&self) -> bool {
260 *self == ErrorCode::Compression
261 }
262
263 pub fn is_connect(&self) -> bool {
265 *self == ErrorCode::Connect
266 }
267
268 pub fn is_enhance_your_calm(&self) -> bool {
270 *self == ErrorCode::EnhanceYourCalm
271 }
272
273 pub fn is_flow_control(&self) -> bool {
275 *self == ErrorCode::FlowControl
276 }
277
278 pub fn is_frame_size(&self) -> bool {
280 *self == ErrorCode::FrameSize
281 }
282
283 pub fn is_http_1_1_required(&self) -> bool {
285 *self == ErrorCode::Http11Required
286 }
287
288 pub fn is_inadequate_security(&self) -> bool {
290 *self == ErrorCode::InadequateSecurity
291 }
292
293 pub fn is_internal(&self) -> bool {
295 *self == ErrorCode::Internal
296 }
297
298 pub fn is_no_error(&self) -> bool {
300 *self == ErrorCode::NoError
301 }
302
303 pub fn is_protocol(&self) -> bool {
305 *self == ErrorCode::Protocol
306 }
307
308 pub fn is_refused_stream(&self) -> bool {
310 *self == ErrorCode::RefusedStream
311 }
312
313 pub fn is_settings_timeout(&self) -> bool {
315 *self == ErrorCode::SettingsTimeout
316 }
317
318 pub fn is_stream_closed(&self) -> bool {
320 *self == ErrorCode::StreamClosed
321 }
322}
323
324impl fmt::Debug for ErrorCode {
325 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
326 self.format(formatter)
327 }
328}
329
330impl fmt::Display for ErrorCode {
331 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
332 self.format(formatter)
333 }
334}