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