aboutsummaryrefslogtreecommitdiff
path: root/src/zip/error.rs
blob: 550817728b21acc53b3bf5a46c55c3fdfe4a91a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::error::Error;
use std::fmt::Display;
use std::io::Error as IoError;

pub type ZipResult<T> = Result<T, ZipError>;

#[derive(Debug)]
pub enum ZipError {
    Io(IoError),

    EocdrNotFound,
    InvalidEOCDR64Signature,
    InvalidFileHeaderSignature,
    InvalidCDRSignature,

    UnsupportedCompressionMethod(u16),
    UnsupportedEncryptionMethod,
    InvalidDate,
    InvalidTime,
    InvalidFileName,
    InvalidExtraFields,
    AesExtraFieldNotFound,
    InvalidFileComment,

    FileNotFound,
    IncorrectPassword,
    PasswordIsNotSpecified,
    CompressedDataIsUnseekable,
    EncryptedDataIsUnseekable,
}

impl From<IoError> 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::UnsupportedCompressionMethod(l0), Self::UnsupportedCompressionMethod(r0)) => {
                l0 == r0
            }
            _ => 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::EocdrNotFound => write!(f, "End of central directory record not found"),
            Self::InvalidEOCDR64Signature => {
                write!(
                    f,
                    "Invalid signature of zip64 end of central directory record"
                )
            }
            Self::InvalidFileHeaderSignature => {
                write!(f, "Invalid file header signature")
            }
            Self::InvalidCDRSignature => {
                write!(f, "Invalid signature of central directory record")
            }

            Self::UnsupportedCompressionMethod(id) => {
                writeln!(f, "Unsupported compression method {}", id)
            }
            Self::UnsupportedEncryptionMethod => {
                writeln!(f, "Unsupported encryption method")
            }
            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::IncorrectPassword => write!(f, "Incorrect 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"),
        }
    }
}

impl Error for ZipError {}