Debian Codegraph @

third_party/rust/neqo-qpack/src/lib.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 pub mod decoder;
8 mod decoder_instructions;
9 pub mod encoder;
10 mod encoder_instructions;
11 mod header_block;
12 pub mod huffman;
13 mod huffman_decode_helper;
14 pub mod huffman_table;
15 mod prefix;
16 mod qlog;
17 mod qpack_send_buf;
18 pub mod reader;
19 mod static_table;
20 mod stats;
21 mod table;
22
23 use std::fmt::{self, Display, Formatter};
24
25 pub use decoder::QPackDecoder;
26 pub use encoder::QPackEncoder;
27 pub use stats::Stats;
28
29 type Res<T> = Result<T, Error>;
30
31 #[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Copy)]
32 pub struct QpackSettings {
33 pub max_table_size_decoder: u64,
34 pub max_table_size_encoder: u64,
35 pub max_blocked_streams: u16,
36 }
37
38 #[derive(Clone, Debug, PartialEq, Eq)]
39 pub enum Error {
40 DecompressionFailed,
41 EncoderStream,
42 DecoderStream,
43 ClosedCriticalStream,
44 InternalError,
45
46 // These are internal errors, they will be transformed into one of the above.
47 NeedMoreData, /* Return when an input stream does not have more data that a decoder
48 * needs.(It does not mean that a stream is closed.) */
49 HeaderLookup,
50 HuffmanDecompressionFailed,
51 BadUtf8,
52 ChangeCapacity,
53 DynamicTableFull,
54 IncrementAck,
55 IntegerOverflow,
56 WrongStreamCount,
57 Decoding, // Decoding internal error that is not one of the above.
58 EncoderStreamBlocked,
59 Internal,
60
61 TransportError(neqo_transport::Error),
62 QlogError,
63 }
64
65 impl Error {
66 #[must_use]
67 pub const fn code(&self) -> neqo_transport::AppError {
68 match self {
69 Self::DecompressionFailed => 0x200,
70 Self::EncoderStream => 0x201,
71 Self::DecoderStream => 0x202,
72 Self::ClosedCriticalStream => 0x104,
73 // These are all internal errors.
74 _ => 3,
75 }
76 }
77
78 /// # Errors
79 ///
80 /// Any error is mapped to the indicated type.
81 fn map_error<R>(r: Result<R, Self>, err: Self) -> Result<R, Self> {
82 r.map_err(|e| {
83 if matches!(e, Self::ClosedCriticalStream) {
84 e
85 } else {
86 err
87 }
88 })
89 }
90 }
91
92 impl ::std::error::Error for Error {
93 fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
94 match self {
95 Self::TransportError(e) => Some(e),
96 _ => None,
97 }
98 }
99 }
100
101 impl Display for Error {
102 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
103 write!(f, "QPACK error: {self:?}")
104 }
105 }
106
107 impl From<neqo_transport::Error> for Error {
108 fn from(err: neqo_transport::Error) -> Self {
109 Self::TransportError(err)
110 }
111 }
112
113 impl From<::qlog::Error> for Error {
114 fn from(_err: ::qlog::Error) -> Self {
115 Self::QlogError
116 }
117 }