From 1bb400dcb258f135a3f92f6242e728f0475325c1 Mon Sep 17 00:00:00 2001 From: igorechek06 Date: Sat, 10 Aug 2024 17:58:14 +0900 Subject: Unify zip errors --- src/zip/error.rs | 68 ++++++++++++++++++-------------------------------------- 1 file changed, 22 insertions(+), 46 deletions(-) (limited to 'src/zip/error.rs') diff --git a/src/zip/error.rs b/src/zip/error.rs index dbb6bb5..fbc2ba1 100644 --- a/src/zip/error.rs +++ b/src/zip/error.rs @@ -8,27 +8,18 @@ pub type ZipResult = Result; pub enum ZipError { Io(IoError), - EocdrNotFound, - InvalidEocdr64Signature, - InvalidFileHeaderSignature, - InvalidCdrSignature, - + // Driver errors + StructNotFound(&'static str), + InvalidSignature(&'static str), + InvalidField(&'static str), + Unsupported(&'static str), Overlapping(&'static str, &'static str), - UnsupportedCompressionMethod(u16), - UnsupportedEncryptionMethod, - InvalidDate, - InvalidTime, - InvalidFileName, - InvalidExtraFields, - AesExtraFieldNotFound, - InvalidFileComment, - + // API errors FileNotFound, WrongPassword, PasswordIsNotSpecified, - CompressedDataIsUnseekable, - EncryptedDataIsUnseekable, + UnseekableFile, } impl From for ZipError { @@ -41,9 +32,10 @@ impl PartialEq for ZipError { fn eq(&self, other: &Self) -> bool { match (self, other) { (Self::Io(l0), Self::Io(r0)) => l0.kind() == r0.kind(), - (Self::UnsupportedCompressionMethod(l0), Self::UnsupportedCompressionMethod(r0)) => { - l0 == r0 - } + (Self::StructNotFound(l0), Self::StructNotFound(r0)) => l0 == r0, + (Self::InvalidSignature(l0), Self::InvalidSignature(r0)) => l0 == r0, + (Self::InvalidField(l0), Self::InvalidField(r0)) => l0 == r0, + (Self::Overlapping(l0, l1), Self::Overlapping(r0, r1)) => l0 == r0 && l1 == r1, _ => core::mem::discriminant(self) == core::mem::discriminant(other), } } @@ -56,42 +48,26 @@ impl Display for ZipError { match self { Self::Io(error) => write!(f, "{}", error), - Self::EocdrNotFound => write!(f, "End of central directory record not found"), - Self::InvalidEocdr64Signature => { - write!( - f, - "Invalid signature of zip64 end of central directory record" - ) + Self::StructNotFound(struct_name) => { + write!(f, "Struct '{}' not found", struct_name) } - Self::InvalidFileHeaderSignature => { - write!(f, "Invalid file header signature") + Self::InvalidSignature(struct_name) => { + write!(f, "Invalid signature of struct '{}'", struct_name) } - Self::InvalidCdrSignature => { - write!(f, "Invalid signature of central directory record") + Self::InvalidField(field_name) => { + write!(f, "Field '{}' has invalid data", field_name) } - - Self::Overlapping(struct1, struct2) => { - write!(f, "`{}` overlapt `{}`", struct1, struct2) - } - - Self::UnsupportedCompressionMethod(id) => { - writeln!(f, "Unsupported compression method `{}`", id) + Self::Unsupported(data_type) => { + writeln!(f, "Unsupported {}", data_type) } - Self::UnsupportedEncryptionMethod => { - writeln!(f, "Unsupported encryption method") + Self::Overlapping(struct_name1, struct_name2) => { + write!(f, "`{}` overlap `{}`", struct_name1, struct_name2) } - Self::InvalidDate => write!(f, "Invalid date"), - Self::InvalidTime => write!(f, "Invalid time"), - Self::InvalidFileName => write!(f, "Invalid file name"), - Self::InvalidExtraFields => write!(f, "Invalid extra fields"), - Self::AesExtraFieldNotFound => write!(f, "Aes extra field not found"), - Self::InvalidFileComment => write!(f, "Invalid file comment"), Self::FileNotFound => write!(f, "File not found"), Self::WrongPassword => write!(f, "Wrong password"), Self::PasswordIsNotSpecified => write!(f, "Password is not specified"), - Self::CompressedDataIsUnseekable => write!(f, "Compressed data is unseekable"), - Self::EncryptedDataIsUnseekable => write!(f, "Encrypted data is unseekable"), + Self::UnseekableFile => write!(f, "File is unseekable"), } } } -- cgit v1.2.3