http_box/http2/
error_code.rs

1// +-----------------------------------------------------------------------------------------------+
2// | Copyright 2016 Sean Kerr                                                                      |
3// |                                                                                               |
4// | Licensed under the Apache License, Version 2.0 (the "License");                               |
5// | you may not use this file except in compliance with the License.                              |
6// | You may obtain a copy of the License at                                                       |
7// |                                                                                               |
8// |  http://www.apache.org/licenses/LICENSE-2.0                                                   |
9// |                                                                                               |
10// | Unless required by applicable law or agreed to in writing, software                           |
11// | distributed under the License is distributed on an "AS IS" BASIS,                             |
12// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.                      |
13// | See the License for the specific language governing permissions and                           |
14// | limitations under the License.                                                                |
15// +-----------------------------------------------------------------------------------------------+
16
17use std::fmt;
18
19/// Cancel error code.
20pub const E_CANCEL: u8 = 0x8;
21
22/// Compression error code.
23pub const E_COMPRESSION: u8 = 0x9;
24
25/// Connect error code.
26pub const E_CONNECT: u8 = 0xA;
27
28/// Enhance your calm error code.
29pub const E_ENHANCE_YOUR_CALM: u8 = 0xB;
30
31/// Flow control error code.
32pub const E_FLOW_CONTROL: u8 = 0x3;
33
34/// Frame size error code.
35pub const E_FRAME_SIZE: u8 = 0x6;
36
37/// HTTP/1.1 required error code.
38pub const E_HTTP_1_1_REQUIRED: u8 = 0xD;
39
40/// Inadequate security error code.
41pub const E_INADEQUATE_SECURITY: u8 = 0xC;
42
43/// No error code.
44pub const E_NO_ERROR: u8 = 0x0;
45
46/// Internal error code.
47pub const E_INTERNAL: u8 = 0x2;
48
49/// Protocol error code.
50pub const E_PROTOCOL: u8 = 0x1;
51
52/// Refused stream error code.
53pub const E_REFUSED_STREAM: u8 = 0x7;
54
55/// Settings timeout error code.
56pub const E_SETTINGS_TIMEOUT: u8 = 0x4;
57
58/// Stream closed error code.
59pub const E_STREAM_CLOSED: u8 = 0x5;
60
61/// Unsupported error code.
62pub const E_UNSUPPORTED: u8 = 0xFF;
63
64/// Error codes.
65#[derive(Clone,Copy,PartialEq)]
66#[repr(u8)]
67pub enum ErrorCode {
68    /// Cancel error.
69    Cancel = E_CANCEL,
70
71    /// Compression error.
72    Compression = E_COMPRESSION,
73
74    /// Connect error.
75    Connect = E_CONNECT,
76
77    /// Enhance your calm error.
78    EnhanceYourCalm = E_ENHANCE_YOUR_CALM,
79
80    /// Flow control error.
81    FlowControl = E_FLOW_CONTROL,
82
83    /// Frame size error.
84    FrameSize = E_FRAME_SIZE,
85
86    /// HTTP/1.1 required error.
87    Http11Required = E_HTTP_1_1_REQUIRED,
88
89    /// Inadequate security error.
90    InadequateSecurity = E_INADEQUATE_SECURITY,
91
92    /// No error.
93    NoError = E_NO_ERROR,
94
95    /// Internal error.
96    Internal = E_INTERNAL,
97
98    /// Protocol error.
99    Protocol = E_PROTOCOL,
100
101    /// Refused stream error.
102    RefusedStream = E_REFUSED_STREAM,
103
104    /// Settings timeout error.
105    SettingsTimeout = E_SETTINGS_TIMEOUT,
106
107    /// Stream closed error.
108    StreamClosed = E_STREAM_CLOSED,
109
110    /// Unsupported error.
111    Unsupported = E_UNSUPPORTED
112}
113
114impl ErrorCode {
115    /// Create a new `ErrorCode` from a `u8`.
116    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    /// Convert this error code to byte value.
137    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    /// Format this for debug and display purposes.
158    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    /// Indicates that this an `ErrorCode::Cancel`.
254    pub fn is_cancel(&self) -> bool {
255        *self == ErrorCode::Cancel
256    }
257
258    /// Indicates that this an `ErrorCode::Compression`.
259    pub fn is_compression(&self) -> bool {
260        *self == ErrorCode::Compression
261    }
262
263    /// Indicates that this an `ErrorCode::Connect`.
264    pub fn is_connect(&self) -> bool {
265        *self == ErrorCode::Connect
266    }
267
268    /// Indicates that this an `ErrorCode::EnhanceYourCalm`.
269    pub fn is_enhance_your_calm(&self) -> bool {
270        *self == ErrorCode::EnhanceYourCalm
271    }
272
273    /// Indicates that this an `ErrorCode::FlowControl`.
274    pub fn is_flow_control(&self) -> bool {
275        *self == ErrorCode::FlowControl
276    }
277
278    /// Indicates that this an `ErrorCode::FrameSize`.
279    pub fn is_frame_size(&self) -> bool {
280        *self == ErrorCode::FrameSize
281    }
282
283    /// Indicates that this an `ErrorCode::Http11Required`.
284    pub fn is_http_1_1_required(&self) -> bool {
285        *self == ErrorCode::Http11Required
286    }
287
288    /// Indicates that this an `ErrorCode::InadequateSecurity`.
289    pub fn is_inadequate_security(&self) -> bool {
290        *self == ErrorCode::InadequateSecurity
291    }
292
293    /// Indicates that this an `ErrorCode::Internal`.
294    pub fn is_internal(&self) -> bool {
295        *self == ErrorCode::Internal
296    }
297
298    /// Indicates that this an `ErrorCode::NoError`.
299    pub fn is_no_error(&self) -> bool {
300        *self == ErrorCode::NoError
301    }
302
303    /// Indicates that this an `ErrorCode::Protocol`.
304    pub fn is_protocol(&self) -> bool {
305        *self == ErrorCode::Protocol
306    }
307
308    /// Indicates that this an `ErrorCode::RefusedStream`.
309    pub fn is_refused_stream(&self) -> bool {
310        *self == ErrorCode::RefusedStream
311    }
312
313    /// Indicates that this an `ErrorCode::SettingsTimeout`.
314    pub fn is_settings_timeout(&self) -> bool {
315        *self == ErrorCode::SettingsTimeout
316    }
317
318    /// Indicates that this an `ErrorCode::StreamClosed`.
319    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}