1#![forbid(unsafe_code)]
2
3use core::fmt;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum ZstdError {
8 Compress(CompressError),
9 Decompress(DecompressError),
10}
11
12#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum CompressError {
15 OutputTooSmall,
17 InvalidLevel(i32),
20 InvalidDictionary,
22}
23
24#[derive(Debug, Clone, PartialEq, Eq)]
26pub enum DecompressError {
27 BadMagic,
29 BadFrameHeader,
31 BadBlockHeader,
33 BadBlockType,
35 CorruptLiterals,
37 CorruptSequences,
39 BlockTooLarge,
41 InvalidOffset,
43 FrameSizeMismatch,
45 BadFseTable,
47 BadHuffmanWeights,
49 BadHuffmanStream,
51 WindowTooLarge { requested: u64, max: u64 },
53 OutputTooSmall,
55 ChecksumMismatch { expected: u32, got: u32 },
57 DictMismatch { expected: u32, got: u32 },
59 DictRequired,
61 InvalidDictionary,
63 InputExhausted,
65 ExtraBytes,
67}
68
69impl fmt::Display for ZstdError {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 match self {
72 ZstdError::Compress(e) => write!(f, "compress: {e}"),
73 ZstdError::Decompress(e) => write!(f, "decompress: {e}"),
74 }
75 }
76}
77
78impl fmt::Display for CompressError {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 match self {
81 CompressError::OutputTooSmall => write!(f, "output buffer too small"),
82 CompressError::InvalidLevel(l) => write!(f, "invalid compression level: {l}"),
83 CompressError::InvalidDictionary => write!(f, "invalid dictionary"),
84 }
85 }
86}
87
88impl fmt::Display for DecompressError {
89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90 match self {
91 DecompressError::BadMagic => write!(f, "invalid frame magic number"),
92 DecompressError::BadFrameHeader => write!(f, "malformed frame header"),
93 DecompressError::BadBlockHeader => write!(f, "malformed block header"),
94 DecompressError::BadBlockType => write!(f, "unknown block type"),
95 DecompressError::CorruptLiterals => write!(f, "corrupt literals section"),
96 DecompressError::CorruptSequences => write!(f, "corrupt sequences section"),
97 DecompressError::BlockTooLarge => write!(f, "block size exceeds maximum"),
98 DecompressError::InvalidOffset => write!(f, "invalid match offset"),
99 DecompressError::FrameSizeMismatch => {
100 write!(f, "decoded size does not match frame content size")
101 }
102 DecompressError::BadFseTable => write!(f, "invalid FSE table description"),
103 DecompressError::BadHuffmanWeights => write!(f, "invalid Huffman weights"),
104 DecompressError::BadHuffmanStream => write!(f, "corrupt Huffman stream"),
105 DecompressError::WindowTooLarge { requested, max } => {
106 write!(f, "window size {requested} exceeds max {max}")
107 }
108 DecompressError::OutputTooSmall => write!(f, "output buffer too small"),
109 DecompressError::ChecksumMismatch { expected, got } => {
110 write!(
111 f,
112 "checksum mismatch: expected {expected:#010x}, got {got:#010x}"
113 )
114 }
115 DecompressError::DictMismatch { expected, got } => {
116 write!(f, "dictionary ID mismatch: expected {expected}, got {got}")
117 }
118 DecompressError::DictRequired => write!(f, "dictionary required but not provided"),
119 DecompressError::InvalidDictionary => write!(f, "invalid dictionary format"),
120 DecompressError::InputExhausted => write!(f, "unexpected end of input"),
121 DecompressError::ExtraBytes => write!(f, "extra bytes after frame"),
122 }
123 }
124}
125
126#[cfg(feature = "std")]
127impl std::error::Error for ZstdError {}
128#[cfg(feature = "std")]
129impl std::error::Error for CompressError {}
130#[cfg(feature = "std")]
131impl std::error::Error for DecompressError {}
132
133impl From<CompressError> for ZstdError {
134 fn from(e: CompressError) -> Self {
135 ZstdError::Compress(e)
136 }
137}
138
139impl From<DecompressError> for ZstdError {
140 fn from(e: DecompressError) -> Self {
141 ZstdError::Decompress(e)
142 }
143}