Skip to main content

shared_brotli_patch_decoder/
decode_error.rs

1use std::io::{self, ErrorKind};
2
3#[derive(Debug, Clone, PartialEq)]
4pub enum DecodeError {
5    InitFailure,
6    InvalidStream,
7    InvalidDictionary,
8    MaxSizeExceeded,
9    ExcessInputData,
10    IoError(io::ErrorKind),
11}
12
13impl DecodeError {
14    pub fn from_io_error(err: io::Error) -> Self {
15        match err.kind() {
16            ErrorKind::OutOfMemory => DecodeError::MaxSizeExceeded,
17            ErrorKind::UnexpectedEof => DecodeError::InvalidStream,
18            _ => DecodeError::IoError(err.kind()),
19        }
20    }
21}
22
23impl std::fmt::Display for DecodeError {
24    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
25        match self {
26            DecodeError::InitFailure => write!(f, "Failed to initialize the brotli decoder."),
27            DecodeError::InvalidStream => {
28                write!(f, "Brotli compressed stream is invalid, decoding failed.")
29            }
30            DecodeError::InvalidDictionary => write!(f, "Shared dictionary format is invalid."),
31            DecodeError::MaxSizeExceeded => write!(f, "Decompressed size greater than maximum."),
32            DecodeError::ExcessInputData => write!(
33                f,
34                "There is unconsumed data in the input stream after decoding."
35            ),
36            DecodeError::IoError(kind) => write!(f, "Generic IO error: {}", kind),
37        }
38    }
39}
40
41impl std::error::Error for DecodeError {}