httlib_hpack/decoder/
error.rs

1use std::fmt;
2use std::error;
3use httlib_huffman::{DecoderError as HuffmanError};
4
5/// Contains error options that can be encountered while performing the decoding
6/// of an HPACK header set.
7#[derive(Debug, PartialEq)]
8pub enum DecoderError {
9    /// Indicates that the decoder received an invalid (Huffman) buffer. This
10    /// should never happen if the input is encoded according to the HPACK spec.
11    InvalidInput,
12
13    /// Indicates that an invalid index was provided. According to the HPACK
14    /// specification, the index `0` must be treated as an invalid index number.
15    /// The first valid number is `1`.
16    InvalidIndex,
17
18    /// Indicates that an invalid prefix was provided (must be [1, 8]).
19    InvalidPrefix,
20
21    /// Indicates that the value of the integer being decoded exceeds a certain
22    /// threshold (5 bytes are chosen by this implementation).
23    IntegerOverflow,
24
25    /// Indicates that the buffer from which an integer was supposed to be
26    /// decode does not contain enough octets to complete the decoding.
27    IntegerUnderflow,
28
29    /// Indicates that the decoder received a size that do not follow external
30    /// protocol rules.
31    InvalidMaxDynamicSize,
32}
33
34impl From<HuffmanError> for DecoderError {
35    fn from(err: HuffmanError) -> Self {
36        match err {
37            HuffmanError::InvalidInput => Self::InvalidInput
38        }
39    }
40}
41
42impl fmt::Display for DecoderError {
43    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
44        match self {
45            Self::InvalidInput => write!(fmt, "Invalid input character."),
46            Self::InvalidIndex => write!(fmt, "Invalid index."),
47            Self::InvalidPrefix => write!(fmt, "Invalid prefix."),
48            Self::IntegerOverflow => write!(fmt, "Too many bytes."),
49            Self::IntegerUnderflow => write!(fmt, "Not enough bytes."),
50            Self::InvalidMaxDynamicSize => write!(fmt, "New size exceeds hard limit."),
51        }
52    }
53}
54
55impl error::Error for DecoderError {}