use std::fmt;
use std::io;
#[derive(Debug)]
pub enum ZipError {
Io(io::Error),
FieldTooLong {
field: &'static str,
len: usize,
max: usize,
},
Poisoned(String),
InvalidState(String),
}
impl fmt::Display for ZipError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(e) => write!(f, "I/O error: {e}"),
Self::FieldTooLong { field, len, max } => {
write!(f, "{field} too long: {len} bytes (max {max})")
}
Self::Poisoned(msg) => write!(f, "archive corrupted: {msg}"),
Self::InvalidState(msg) => write!(f, "invalid state: {msg}"),
}
}
}
impl std::error::Error for ZipError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for ZipError {
fn from(err: io::Error) -> Self {
Self::Io(err)
}
}
impl From<ZipError> for io::Error {
fn from(err: ZipError) -> Self {
match err {
ZipError::Io(e) => e,
ZipError::FieldTooLong { .. } => io::Error::new(io::ErrorKind::InvalidInput, err),
ZipError::Poisoned(_) => io::Error::other(err),
ZipError::InvalidState(_) => io::Error::new(io::ErrorKind::InvalidInput, err),
}
}
}