Skip to main content

crabka_compression/
error.rs

1//! Errors returned by `compress` and `decompress`.
2
3use thiserror::Error;
4
5/// Errors that can occur during compression or decompression.
6#[derive(Debug, Error)]
7#[non_exhaustive]
8pub enum CompressionError {
9    /// The requested codec was not enabled at compile time. The codec name
10    /// is one of `"gzip"`, `"snappy"`, `"lz4"`, `"zstd"`.
11    #[error("compression feature `{0}` not enabled at compile time")]
12    FeatureDisabled(&'static str),
13
14    /// The compressed payload could not be parsed (truncated input, bad
15    /// framing, invalid checksum, etc.).
16    #[error("invalid compressed data: {0}")]
17    InvalidData(String),
18
19    /// Decompression produced (or would produce) more output than the caller's
20    /// `max_output` cap allows. Guards against decompression bombs: a small
21    /// compressed payload that expands to gigabytes.
22    #[error("decompressed output exceeds limit of {limit} bytes")]
23    TooLarge { limit: usize },
24
25    /// I/O error from one of the codec libraries.
26    #[error("I/O error: {0}")]
27    Io(#[from] std::io::Error),
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use assert2::assert;
34
35    #[test]
36    fn feature_disabled_display() {
37        let e = CompressionError::FeatureDisabled("snappy");
38        assert!(e.to_string() == "compression feature `snappy` not enabled at compile time");
39    }
40}