aboutsummaryrefslogtreecommitdiff
path: root/src/zip/error.rs
blob: 40d45fe37cd6fba53038b9a2df33219368fe4062 (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
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),

    // 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<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(l), Self::Io(r)) => l.kind() == r.kind(),
            (Self::StructNotFound(l), Self::StructNotFound(r)) => l == r,
            (Self::InvalidSignature(l), Self::InvalidSignature(r)) => l == r,
            (Self::InvalidField(l), Self::InvalidField(r)) => l == r,
            (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 {}