use thiserror::Error;
use super::config::SizeOverflow;
use super::core::error::CoreError;
#[derive(Error, Debug, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum EncodeError {
#[error("a destination of {provided} bytes cannot hold the compressed frame")]
OutputTooSmall {
provided: usize,
},
#[error("the stream was declared as {declared} bytes but received a different amount")]
InputSizeMismatch {
declared: u64,
},
#[error("the session cannot accept this operation in its current state")]
InvalidState,
#[error(transparent)]
Bound(#[from] SizeOverflow),
#[error("internal encoder failure")]
Internal,
#[error("the dictionary was prepared for a different encoder configuration")]
DictionaryMismatch,
}
impl EncodeError {
pub(crate) fn from_core(error: CoreError, provided: usize, declared: Option<u64>) -> Self {
match error {
CoreError::DstSizeTooSmall => Self::OutputTooSmall { provided },
CoreError::SrcSizeWrong => Self::InputSizeMismatch {
declared: declared.unwrap_or(0),
},
CoreError::StageWrong => Self::InvalidState,
CoreError::Generic
| CoreError::TableLogTooLarge
| CoreError::MaxSymbolValueTooLarge
| CoreError::MaxSymbolValueTooSmall => Self::Internal,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn core_errors_map_to_public_variants() {
assert_eq!(
EncodeError::from_core(CoreError::DstSizeTooSmall, 3, None),
EncodeError::OutputTooSmall { provided: 3 }
);
assert_eq!(
EncodeError::from_core(CoreError::SrcSizeWrong, 0, Some(9)),
EncodeError::InputSizeMismatch { declared: 9 }
);
assert_eq!(
EncodeError::from_core(CoreError::StageWrong, 0, None),
EncodeError::InvalidState
);
assert_eq!(
EncodeError::from_core(CoreError::Generic, 0, None),
EncodeError::Internal
);
assert_eq!(
EncodeError::from(SizeOverflow),
EncodeError::Bound(SizeOverflow)
);
}
}