Skip to main content

krabka_compression/
error.rs

1//! Errors returned by `compress` and `decompress`.
2
3use std::io;
4
5use thiserror::Error;
6
7/// Errors that can occur during compression or decompression.
8#[derive(Debug, Error)]
9#[non_exhaustive]
10pub enum CompressionError {
11    /// The requested codec was not enabled at compile time. The codec name
12    /// is one of `"gzip"`, `"snappy"`, `"lz4"`, `"zstd"`.
13    #[error("compression feature `{0}` not enabled at compile time")]
14    FeatureDisabled(&'static str),
15
16    /// The codec could not parse the compressed payload. Examples are truncated
17    /// input, bad framing, and an invalid checksum.
18    #[error("invalid compressed data: {0}")]
19    InvalidData(String),
20
21    /// Decompression produced, or would produce, more output than the caller's
22    /// `max_output` cap allows. This guards against decompression bombs, that
23    /// is a small compressed payload that expands to gigabytes.
24    #[error("decompressed output exceeds limit of {limit} bytes")]
25    TooLarge { limit: usize },
26
27    /// The compression level is outside the range that Kafka accepts for the
28    /// codec, or the codec has no levels. `reason` is the message of Kafka's
29    /// level validator (`CompressionType.levelValidator`).
30    #[error("invalid {codec} compression level {level}: {reason}")]
31    InvalidLevel {
32        /// The codec name: `"none"`, `"gzip"`, `"snappy"`, `"lz4"` or `"zstd"`.
33        codec: &'static str,
34        /// The level that the caller gave.
35        level: i32,
36        /// Kafka's message for the level.
37        reason: String,
38    },
39
40    /// I/O error from one of the codec libraries.
41    #[error("I/O error: {0}")]
42    Io(#[from] io::Error),
43}
44
45#[cfg(test)]
46mod tests {
47
48    use super::*;
49
50    #[test]
51    fn feature_disabled_display() {
52        let e = CompressionError::FeatureDisabled("snappy");
53        assert2::assert!(
54            e.to_string() == "compression feature `snappy` not enabled at compile time"
55        );
56    }
57}