use std::error::Error; use std::fmt::Display; use std::io::Error as IoError; pub type ZipResult = Result; #[derive(Debug)] pub enum ZipError { Io(IoError), // Driver errors StructNotFound(&'static str), InvalidSignature(&'static str), InvalidField(&'static str), Unsupported(&'static str), Overlapping(&'static str, &'static str), // API errors FileNotFound, WrongPassword, PasswordIsNotSpecified, UnseekableFile, } impl From for ZipError { fn from(value: IoError) -> Self { Self::Io(value) } } impl PartialEq for ZipError { fn eq(&self, other: &Self) -> bool { match (self, other) { (Self::Io(l0), Self::Io(r0)) => l0.kind() == r0.kind(), (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), } } } impl Eq for ZipError {} impl Display for ZipError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Io(error) => write!(f, "{}", error), Self::StructNotFound(struct_name) => { write!(f, "Struct '{}' not found", struct_name) } Self::InvalidSignature(struct_name) => { write!(f, "Invalid signature of struct '{}'", struct_name) } Self::InvalidField(field_name) => { write!(f, "Field '{}' has invalid data", field_name) } Self::Unsupported(data_type) => { writeln!(f, "Unsupported {}", data_type) } Self::Overlapping(struct_name1, struct_name2) => { write!(f, "`{}` overlap `{}`", struct_name1, struct_name2) } Self::FileNotFound => write!(f, "File not found"), Self::WrongPassword => write!(f, "Wrong password"), Self::PasswordIsNotSpecified => write!(f, "Password is not specified"), Self::UnseekableFile => write!(f, "File is unseekable"), } } } impl Error for ZipError {}