Skip to main content

hpack_html/
error.rs

1use std::fmt;
2
3/// Errors that can occur when decoding an .hpack packet.
4#[derive(Debug)]
5pub enum Error {
6    /// Magic bytes do not match `0x89 0x48 0x50 0x4B`.
7    InvalidMagic,
8    /// Packet version is not supported.
9    UnsupportedVersion(u8),
10    /// Packet data is truncated or malformed.
11    TruncatedPacket(String),
12    /// DEFLATE/gzip decompression failed.
13    DecompressionFailed(String),
14    /// CRC32 checksum does not match.
15    ChecksumMismatch { expected: u32, actual: u32 },
16    /// Decompressed bytes are not valid UTF-8.
17    InvalidUtf8(std::string::FromUtf8Error),
18}
19
20impl fmt::Display for Error {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Error::InvalidMagic => write!(f, "Invalid .hpack magic bytes"),
24            Error::UnsupportedVersion(v) => write!(f, "Unsupported .hpack version: {}", v),
25            Error::TruncatedPacket(msg) => write!(f, "Truncated packet: {}", msg),
26            Error::DecompressionFailed(msg) => write!(f, "Decompression failed: {}", msg),
27            Error::ChecksumMismatch { expected, actual } => {
28                write!(
29                    f,
30                    "CRC32 mismatch: expected 0x{:08X}, got 0x{:08X}",
31                    expected, actual
32                )
33            }
34            Error::InvalidUtf8(e) => write!(f, "Invalid UTF-8 in decompressed HTML: {}", e),
35        }
36    }
37}
38
39impl std::error::Error for Error {}