1use std::fmt;
2
3#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5#[non_exhaustive]
6pub enum Error {
7 InvalidFrameSize,
9 InvalidMetadata,
11 UnsupportedVersion,
13 InvalidRange,
15 FrameAfterFinal,
17 InvalidStoredLength,
19 ProfileMismatch,
21 UsageLimit,
23 Overflow,
25 Allocation,
27 CompressionFailed,
29 DecompressionFailed,
31 EncryptionFailed,
33 AuthenticationFailed,
35}
36
37impl fmt::Display for Error {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 f.write_str(match self {
40 Self::InvalidFrameSize => "invalid or unsupported frame size",
41 Self::InvalidMetadata => "inconsistent frame metadata",
42 Self::UnsupportedVersion => "unsupported metadata version",
43 Self::InvalidRange => "logical range outside the object or reversed",
44 Self::FrameAfterFinal => "frame submitted after a short final frame",
45 Self::InvalidStoredLength => "incorrect stored frame length",
46 Self::ProfileMismatch => "frame and decoder configurations differ",
47 Self::UsageLimit => "cryptographic usage limit exceeded",
48 Self::Overflow => "length exceeds integer or platform limits",
49 Self::Allocation => "buffer allocation failed",
50 Self::CompressionFailed => "compression failed",
51 Self::DecompressionFailed => "invalid compressed frame",
52 Self::EncryptionFailed => "encryption failed",
53 Self::AuthenticationFailed => "frame authentication failed",
54 })
55 }
56}
57
58impl std::error::Error for Error {}
59
60pub type Result<T> = std::result::Result<T, Error>;
62
63pub(crate) fn buffer_len(len: u64) -> Result<usize> {
64 let len = usize::try_from(len).map_err(|_| Error::Overflow)?;
65 if len > isize::MAX as usize {
66 return Err(Error::Overflow);
67 }
68 Ok(len)
69}
70
71#[cfg(feature = "lz4")]
72pub(crate) fn resize(buffer: &mut Vec<u8>, len: usize) -> Result<()> {
73 if len > isize::MAX as usize {
74 return Err(Error::Overflow);
75 }
76 if len > buffer.len() {
77 buffer
78 .try_reserve(len - buffer.len())
79 .map_err(|_| Error::Allocation)?;
80 }
81 buffer.resize(len, 0);
82 Ok(())
83}