#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CompressionError {
InsufficientSpace,
Stopped(enough::StopReason),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum DecompressionError {
BadData,
InvalidHeader,
ChecksumMismatch,
InsufficientSpace,
OutputLimitExceeded,
StallLimitExceeded,
Stopped(enough::StopReason),
}
impl core::fmt::Display for CompressionError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::InsufficientSpace => write!(f, "output buffer too small for compressed data"),
Self::Stopped(reason) => write!(f, "{reason}"),
}
}
}
impl From<enough::StopReason> for CompressionError {
fn from(reason: enough::StopReason) -> Self {
Self::Stopped(reason)
}
}
impl core::fmt::Display for DecompressionError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::BadData => write!(f, "invalid or corrupt compressed data"),
Self::InvalidHeader => write!(f, "invalid zlib or gzip header"),
Self::ChecksumMismatch => write!(f, "checksum mismatch"),
Self::InsufficientSpace => write!(f, "output buffer too small for decompressed data"),
Self::OutputLimitExceeded => {
write!(f, "decompressed output exceeded configured maximum size")
}
Self::StallLimitExceeded => {
write!(f, "too many blocks processed without output progress")
}
Self::Stopped(reason) => write!(f, "{reason}"),
}
}
}
impl From<enough::StopReason> for DecompressionError {
fn from(reason: enough::StopReason) -> Self {
Self::Stopped(reason)
}
}
#[cfg(feature = "alloc")]
#[derive(Debug)]
#[non_exhaustive]
pub enum StreamError<E> {
Decompress(DecompressionError),
Source(E),
}
#[cfg(feature = "alloc")]
impl<E: core::fmt::Display> core::fmt::Display for StreamError<E> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Decompress(e) => write!(f, "{e}"),
Self::Source(e) => write!(f, "source error: {e}"),
}
}
}
#[cfg(feature = "alloc")]
impl<E> From<DecompressionError> for StreamError<E> {
fn from(e: DecompressionError) -> Self {
Self::Decompress(e)
}
}
#[cfg(feature = "std")]
impl std::error::Error for CompressionError {}
#[cfg(feature = "std")]
impl std::error::Error for DecompressionError {}
#[cfg(feature = "std")]
impl<E: std::error::Error + 'static> std::error::Error for StreamError<E> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Decompress(e) => Some(e),
Self::Source(e) => Some(e),
}
}
}