pltrs_codec/deflate/
error.rs1use std::error::Error;
4use std::fmt;
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8pub enum HuffmanTable {
9 LiteralLength,
11 Distance,
13 CodeLength,
15}
16
17impl fmt::Display for HuffmanTable {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 f.write_str(match self {
20 Self::LiteralLength => "literal/length",
21 Self::Distance => "distance",
22 Self::CodeLength => "code-length",
23 })
24 }
25}
26
27#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29pub enum InflateError {
30 UnexpectedEof,
32 InvalidBlockType,
34 StoredLengthMismatch {
36 length: u16,
38 complement: u16,
40 },
41 TooManyCodes {
43 table: HuffmanTable,
45 count: usize,
47 },
48 OversubscribedCode(HuffmanTable),
50 IncompleteCode(HuffmanTable),
53 MissingEndOfBlock,
55 RepeatWithoutPrevious,
57 CodeLengthOverflow,
59 InvalidCode(HuffmanTable),
62 DistanceTooFarBack {
64 distance: usize,
66 available: usize,
68 },
69 OutputLimitExceeded {
71 limit: usize,
73 },
74}
75
76impl fmt::Display for InflateError {
77 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78 match *self {
79 Self::UnexpectedEof => f.write_str("DEFLATE stream ended unexpectedly"),
80 Self::InvalidBlockType => f.write_str("invalid DEFLATE block type 3"),
81 Self::StoredLengthMismatch { length, complement } => write!(
82 f,
83 "stored block length {length:#06x} does not match its complement {complement:#06x}"
84 ),
85 Self::TooManyCodes { table, count } => {
86 write!(f, "dynamic block declares {count} {table} codes")
87 }
88 Self::OversubscribedCode(table) => {
89 write!(f, "over-subscribed {table} Huffman code")
90 }
91 Self::IncompleteCode(table) => write!(f, "incomplete {table} Huffman code"),
92 Self::MissingEndOfBlock => f.write_str("dynamic block has no end-of-block code"),
93 Self::RepeatWithoutPrevious => {
94 f.write_str("code-length repeat with no previous length")
95 }
96 Self::CodeLengthOverflow => {
97 f.write_str("code-length run exceeds the number of declared codes")
98 }
99 Self::InvalidCode(table) => write!(f, "invalid {table} code"),
100 Self::DistanceTooFarBack {
101 distance,
102 available,
103 } => write!(
104 f,
105 "back-reference distance {distance} exceeds the {available} bytes decoded so far"
106 ),
107 Self::OutputLimitExceeded { limit } => {
108 write!(f, "decompressed data exceeds the limit of {limit} bytes")
109 }
110 }
111 }
112}
113
114impl Error for InflateError {}