Skip to main content

zstd_rs/
error.rs

1//! The single error type returned by every fallible operation in the crate.
2use core::fmt;
3
4/// What went wrong. Decoding never panics on malformed input; it returns one of these.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum Error {
8    /// The input ended before a complete structure could be read.
9    Truncated,
10    /// The input does not start with a zstd or skippable frame magic number.
11    BadMagic,
12    /// A reserved bit or field was set.
13    Reserved,
14    /// The frame's window is larger than the decoder accepts.
15    WindowTooLarge,
16    /// The frame requires a dictionary but none was supplied.
17    MissingDictionary,
18    /// The frame names a dictionary ID different from the supplied dictionary's.
19    WrongDictionary,
20    /// Decoding would exceed the caller's output limit (decompression-bomb guard).
21    OutputLimit,
22    /// The frame's XXH64 checksum does not match the decoded content.
23    Checksum,
24    /// The decoded size disagrees with the frame's declared content size.
25    ContentSize,
26    /// The compressed data is internally inconsistent; the reason is static text.
27    Corrupt(&'static str),
28    /// A dictionary could not be parsed.
29    BadDictionary(&'static str),
30    /// A caller-supplied parameter is out of range; the reason is static text.
31    Parameter(&'static str),
32}
33
34impl fmt::Display for Error {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Error::Truncated => f.write_str("input truncated"),
38            Error::BadMagic => f.write_str("unknown frame magic"),
39            Error::Reserved => f.write_str("reserved bit or field set"),
40            Error::WindowTooLarge => f.write_str("window size exceeds decoder limit"),
41            Error::MissingDictionary => f.write_str("frame needs a dictionary"),
42            Error::WrongDictionary => f.write_str("frame dictionary ID does not match"),
43            Error::OutputLimit => f.write_str("output limit exceeded"),
44            Error::Checksum => f.write_str("content checksum mismatch"),
45            Error::ContentSize => f.write_str("content size mismatch"),
46            Error::Corrupt(why) => write!(f, "corrupt data: {why}"),
47            Error::BadDictionary(why) => write!(f, "bad dictionary: {why}"),
48            Error::Parameter(why) => write!(f, "invalid parameter: {why}"),
49        }
50    }
51}
52
53impl core::error::Error for Error {}
54
55/// Crate-wide result alias.
56pub type Result<T> = core::result::Result<T, Error>;