Skip to main content

krabka_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 codec could not parse the compressed payload. Examples are truncated
15    /// input, bad framing, and an invalid checksum.
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. This guards against decompression bombs, that
21    /// is a small compressed payload that expands to gigabytes.
22    #[error("decompressed output exceeds limit of {limit} bytes")]
23    TooLarge { limit: usize },
24
25    /// The compression level is outside the range that Kafka accepts for the
26    /// codec, or the codec has no levels. `reason` is the message of Kafka's
27    /// level validator (`CompressionType.levelValidator`).
28    #[error("invalid {codec} compression level {level}: {reason}")]
29    InvalidLevel {
30        /// The codec name: `"none"`, `"gzip"`, `"snappy"`, `"lz4"` or `"zstd"`.
31        codec: &'static str,
32        /// The level that the caller gave.
33        level: i32,
34        /// Kafka's message for the level.
35        reason: String,
36    },
37
38    /// I/O error from one of the codec libraries.
39    #[error("I/O error: {0}")]
40    Io(#[from] std::io::Error),
41}
42
43#[cfg(test)]
44mod tests {
45
46    use super::*;
47
48    #[test]
49    fn feature_disabled_display() {
50        let e = CompressionError::FeatureDisabled("snappy");
51        assert2::assert!(
52            e.to_string() == "compression feature `snappy` not enabled at compile time"
53        );
54    }
55}