use std::fmt;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Error {
InvalidFrameSize,
InvalidMetadata,
UnsupportedVersion,
InvalidRange,
FrameAfterFinal,
InvalidStoredLength,
ProfileMismatch,
UsageLimit,
Overflow,
Allocation,
CompressionFailed,
DecompressionFailed,
EncryptionFailed,
AuthenticationFailed,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::InvalidFrameSize => "invalid or unsupported frame size",
Self::InvalidMetadata => "inconsistent frame metadata",
Self::UnsupportedVersion => "unsupported metadata version",
Self::InvalidRange => "logical range outside the object or reversed",
Self::FrameAfterFinal => "frame submitted after a short final frame",
Self::InvalidStoredLength => "incorrect stored frame length",
Self::ProfileMismatch => "frame and decoder configurations differ",
Self::UsageLimit => "cryptographic usage limit exceeded",
Self::Overflow => "length exceeds integer or platform limits",
Self::Allocation => "buffer allocation failed",
Self::CompressionFailed => "compression failed",
Self::DecompressionFailed => "invalid compressed frame",
Self::EncryptionFailed => "encryption failed",
Self::AuthenticationFailed => "frame authentication failed",
})
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;
pub(crate) fn buffer_len(len: u64) -> Result<usize> {
let len = usize::try_from(len).map_err(|_| Error::Overflow)?;
if len > isize::MAX as usize {
return Err(Error::Overflow);
}
Ok(len)
}
#[cfg(feature = "lz4")]
pub(crate) fn resize(buffer: &mut Vec<u8>, len: usize) -> Result<()> {
if len > isize::MAX as usize {
return Err(Error::Overflow);
}
if len > buffer.len() {
buffer
.try_reserve(len - buffer.len())
.map_err(|_| Error::Allocation)?;
}
buffer.resize(len, 0);
Ok(())
}