#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
Unimplemented,
InvalidLevel,
UnexpectedEof,
BadMagic,
ReservedBitSet,
UnusedBitSet,
ReservedBlockType,
BlockTooLarge,
WindowTooLarge,
ContentSizeMismatch,
ContentSizeTooLarge,
ChecksumMismatch,
DictionaryNeeded {
id: u32,
},
DictionaryMismatch {
frame: u32,
loaded: u32,
},
TrailingBytes,
Corruption,
}
impl Error {
pub fn kind(self) -> &'static str {
match self {
Error::Unimplemented => "unimplemented",
Error::InvalidLevel => "invalid_level",
Error::UnexpectedEof => "unexpected_eof",
Error::BadMagic => "bad_magic",
Error::ReservedBitSet => "reserved_bit",
Error::UnusedBitSet => "unused_bit",
Error::ReservedBlockType => "reserved_block_type",
Error::BlockTooLarge => "block_too_large",
Error::WindowTooLarge => "window_too_large",
Error::ContentSizeMismatch => "content_size_mismatch",
Error::ContentSizeTooLarge => "content_size_too_large",
Error::ChecksumMismatch => "checksum_mismatch",
Error::DictionaryNeeded { .. } => "dictionary_needed",
Error::DictionaryMismatch { .. } => "dictionary_mismatch",
Error::TrailingBytes => "trailing_bytes",
Error::Corruption => "corruption",
}
}
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::Unimplemented => {
f.write_str("rusty_zstd: not implemented (see docs/plans/rusty-zstd-mission.md)")
}
Error::InvalidLevel => {
f.write_str("rusty_zstd: compression level out of range (-7..=22)")
}
Error::UnexpectedEof => f.write_str("rusty_zstd: unexpected end of input"),
Error::BadMagic => f.write_str("rusty_zstd: not a zstd frame"),
Error::ReservedBitSet => f.write_str("rusty_zstd: reserved bit set in frame header"),
Error::UnusedBitSet => f.write_str("rusty_zstd: unused bit set in frame header"),
Error::ReservedBlockType => f.write_str("rusty_zstd: reserved block type"),
Error::BlockTooLarge => f.write_str("rusty_zstd: block larger than window"),
Error::WindowTooLarge => f.write_str("rusty_zstd: window larger than decoder cap"),
Error::ContentSizeMismatch => {
f.write_str("rusty_zstd: regenerated size != frame content size")
}
Error::ContentSizeTooLarge => f.write_str("rusty_zstd: content size too large"),
Error::ChecksumMismatch => f.write_str("rusty_zstd: content checksum mismatch"),
Error::DictionaryNeeded { id } => {
write!(f, "rusty_zstd: dictionary id {id} required")
}
Error::DictionaryMismatch { frame, loaded } => {
write!(f, "rusty_zstd: dictionary id {frame} != loaded {loaded}")
}
Error::TrailingBytes => f.write_str("rusty_zstd: trailing bytes after last frame"),
Error::Corruption => f.write_str("rusty_zstd: corrupt zstd frame"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}