Skip to main content

async_deflate_zip/
error.rs

1//! Custom error types for async-deflate-zip.
2
3use std::fmt;
4use std::io;
5
6/// Errors that can occur when creating a ZIP archive.
7#[derive(Debug)]
8pub enum ZipError {
9    /// An underlying I/O error occurred.
10    Io(io::Error),
11
12    /// A filename or extra field exceeds the maximum allowed length.
13    FieldTooLong {
14        /// Name of the field (e.g., "filename", "extra").
15        field: &'static str,
16        /// Actual length in bytes.
17        len: usize,
18        /// Maximum allowed length.
19        max: usize,
20    },
21
22    /// The archive is in an inconsistent state because a previous entry
23    /// was dropped without being properly closed.
24    Poisoned(String),
25
26    /// The archive writer is in a state that does not allow the requested operation.
27    InvalidState(String),
28}
29
30impl fmt::Display for ZipError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::Io(e) => write!(f, "I/O error: {e}"),
34            Self::FieldTooLong { field, len, max } => {
35                write!(f, "{field} too long: {len} bytes (max {max})")
36            }
37            Self::Poisoned(msg) => write!(f, "archive corrupted: {msg}"),
38            Self::InvalidState(msg) => write!(f, "invalid state: {msg}"),
39        }
40    }
41}
42
43impl std::error::Error for ZipError {
44    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
45        match self {
46            Self::Io(e) => Some(e),
47            _ => None,
48        }
49    }
50}
51
52impl From<io::Error> for ZipError {
53    fn from(err: io::Error) -> Self {
54        Self::Io(err)
55    }
56}
57
58// Allow seamless conversion back to io::Error for backward compatibility
59// or for use in contexts that require io::Result.
60impl From<ZipError> for io::Error {
61    fn from(err: ZipError) -> Self {
62        match err {
63            ZipError::Io(e) => e,
64            ZipError::FieldTooLong { .. } => io::Error::new(io::ErrorKind::InvalidInput, err),
65            ZipError::Poisoned(_) => io::Error::other(err),
66            ZipError::InvalidState(_) => io::Error::new(io::ErrorKind::InvalidInput, err),
67        }
68    }
69}