use crate::parse::Method;
use super::encoding;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("format: {0}")]
Format(#[from] FormatError),
#[error("unsupported: {0}")]
Unsupported(#[from] UnsupportedError),
#[error("encoding: {0:?}")]
Encoding(#[from] encoding::DecodingError),
#[error("io: {0}")]
IO(#[from] std::io::Error),
#[error("{method:?} decompression error: {msg}")]
Decompression {
method: Method,
msg: String,
},
#[error("size must be known to open zip file")]
UnknownSize,
}
impl Error {
pub fn method_not_supported(method: Method) -> Self {
Self::Unsupported(UnsupportedError::MethodNotSupported(method))
}
pub fn method_not_enabled(method: Method) -> Self {
Self::Unsupported(UnsupportedError::MethodNotEnabled(method))
}
}
#[derive(Debug, thiserror::Error)]
pub enum UnsupportedError {
#[error("compression method not supported: {0:?}")]
MethodNotSupported(Method),
#[error("compression method supported, but not enabled in this build: {0:?}")]
MethodNotEnabled(Method),
#[error("only LZMA2.0 is supported, found LZMA{minor}.{major}")]
LzmaVersionUnsupported {
major: u8,
minor: u8,
},
#[error("LZMA properties header wrong size: expected {expected} bytes, got {actual} bytes")]
LzmaPropertiesHeaderWrongSize {
expected: u16,
actual: u16,
},
}
#[derive(Debug, thiserror::Error)]
pub enum FormatError {
#[error("end of central directory record not found")]
DirectoryEndSignatureNotFound,
#[error("zip64 end of central directory record not found")]
Directory64EndRecordInvalid,
#[error("directory offset points outside of file")]
DirectoryOffsetPointsOutsideFile,
#[error("invalid central record: expected to read {expected} files, got {actual}")]
InvalidCentralRecord {
expected: u16,
actual: u16,
},
#[error("could not decode extra field")]
InvalidExtraField,
#[error("invalid header offset")]
InvalidHeaderOffset,
#[error("impossible number of files: claims to have {claimed_records_count}, but zip size is {zip_size}")]
ImpossibleNumberOfFiles {
claimed_records_count: u64,
zip_size: u64,
},
#[error("invalid local file header")]
InvalidLocalHeader,
#[error("invalid data descriptor")]
InvalidDataDescriptor,
#[error("uncompressed size didn't match: expected {expected}, got {actual}")]
WrongSize {
expected: u64,
actual: u64,
},
#[error("checksum didn't match: expected {expected:x?}, got {actual:x?}")]
WrongChecksum {
expected: u32,
actual: u32,
},
}
impl From<Error> for std::io::Error {
fn from(e: Error) -> Self {
match e {
Error::IO(e) => e,
e => std::io::Error::new(std::io::ErrorKind::Other, e),
}
}
}