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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use crate::ffi;
use std::borrow::{Borrow, BorrowMut};
use std::ffi::CStr;
use std::fmt;
use std::mem::zeroed;
use std::ops::{Deref, DerefMut};
use std::os::raw::c_int;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum System {
    Sys(c_int),
    Zlib(c_int),
    Unknown(c_int),
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Zip {
    Changed,
    Close,
    CompressionNotSupported,
    CompressedDataInvalid,
    Crc,
    Deleted,
    EncryptionNotSupported,
    Eof,
    Exists,
    Inconsistent,
    Internal,
    InUse,
    InvalidArgument,
    Memory,
    Multidisk,
    NoSuchFile,
    NoPassword,
    NotZip,
    Open,
    OperationNotSupported,
    ReadOnly,
    Read,
    Remove,
    Rename,
    Seek,
    Tell,
    TempFile,
    Write,
    WrongPassword,
    ZipClosed,
    Zlib,
    Unknown,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Error {
    system: Option<System>,
    zip: Option<Zip>,
    message: String,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Error: {}", self.message)
    }
}

impl std::error::Error for Error {}

pub type Result<T> = std::result::Result<T, Error>;

/// A containment structure for a zip_error_t.
/// This can be either Default instantiated for an owned version or borrowed with its From
/// implementation.
#[derive(Debug)]
pub(crate) struct ZipErrorT<T: Borrow<ffi::zip_error_t> + BorrowMut<ffi::zip_error_t>> {
    error: T,
    cleanup: bool,
}

impl Default for ZipErrorT<ffi::zip_error_t> {
    fn default() -> Self {
        unsafe {
            let mut handle: ffi::zip_error_t = zeroed();
            ffi::zip_error_init(&mut handle);
            ZipErrorT {
                error: handle,
                cleanup: true,
            }
        }
    }
}

impl From<c_int> for ZipErrorT<ffi::zip_error_t> {
    fn from(error: c_int) -> Self {
        unsafe {
            let mut handle: ffi::zip_error_t = zeroed();
            ffi::zip_error_init_with_code(&mut handle, error);
            ZipErrorT {
                error: handle,
                cleanup: true,
            }
        }
    }
}

impl<'a> From<&'a mut ffi::zip_error_t> for ZipErrorT<&'a mut ffi::zip_error_t> {
    fn from(error: &'a mut ffi::zip_error_t) -> Self {
        ZipErrorT {
            error,
            cleanup: false,
        }
    }
}

impl<T> ZipErrorT<T>
where
    T: Borrow<ffi::zip_error_t> + BorrowMut<ffi::zip_error_t>,
{
    pub fn system(&self) -> Option<System> {
        let system = unsafe { ffi::zip_error_system_type(self.deref()) };
        match system as _ {
            ffi::ZIP_ET_NONE => None,
            _ => {
                let code = unsafe { ffi::zip_error_code_system(self.deref()) };
                Some(match system as _ {
                    ffi::ZIP_ET_SYS => System::Sys(code),
                    ffi::ZIP_ET_ZLIB => System::Zlib(code),
                    _ => System::Unknown(code),
                })
            }
        }
    }
    pub fn zip(&self) -> Option<Zip> {
        let code = unsafe { ffi::zip_error_code_zip(self.deref()) };
        Some(match code as _ {
            ffi::ZIP_ER_CHANGED => Zip::Changed,
            ffi::ZIP_ER_CLOSE => Zip::Close,
            ffi::ZIP_ER_COMPNOTSUPP => Zip::CompressionNotSupported,
            ffi::ZIP_ER_COMPRESSED_DATA => Zip::CompressedDataInvalid,
            ffi::ZIP_ER_CRC => Zip::Crc,
            ffi::ZIP_ER_DELETED => Zip::Deleted,
            ffi::ZIP_ER_ENCRNOTSUPP => Zip::EncryptionNotSupported,
            ffi::ZIP_ER_EOF => Zip::Eof,
            ffi::ZIP_ER_EXISTS => Zip::Exists,
            ffi::ZIP_ER_INCONS => Zip::Inconsistent,
            ffi::ZIP_ER_INTERNAL => Zip::Internal,
            ffi::ZIP_ER_INUSE => Zip::InUse,
            ffi::ZIP_ER_INVAL => Zip::InvalidArgument,
            ffi::ZIP_ER_MEMORY => Zip::Memory,
            ffi::ZIP_ER_MULTIDISK => Zip::Multidisk,
            ffi::ZIP_ER_NOENT => Zip::NoSuchFile,
            ffi::ZIP_ER_NOPASSWD => Zip::NoPassword,
            ffi::ZIP_ER_NOZIP => Zip::NotZip,
            ffi::ZIP_ER_OK => return None,
            ffi::ZIP_ER_OPEN => Zip::Open,
            ffi::ZIP_ER_OPNOTSUPP => Zip::OperationNotSupported,
            ffi::ZIP_ER_RDONLY => Zip::ReadOnly,
            ffi::ZIP_ER_READ => Zip::Read,
            ffi::ZIP_ER_REMOVE => Zip::Remove,
            ffi::ZIP_ER_RENAME => Zip::Rename,
            ffi::ZIP_ER_SEEK => Zip::Seek,
            ffi::ZIP_ER_TELL => Zip::Tell,
            ffi::ZIP_ER_TMPOPEN => Zip::TempFile,
            ffi::ZIP_ER_WRITE => Zip::Write,
            ffi::ZIP_ER_WRONGPASSWD => Zip::WrongPassword,
            ffi::ZIP_ER_ZIPCLOSED => Zip::ZipClosed,
            ffi::ZIP_ER_ZLIB => Zip::Zlib,
            _ => Zip::Unknown,
        })
    }

    pub fn message(&mut self) -> &CStr {
        unsafe {
            let message = ffi::zip_error_strerror(self.deref_mut());
            assert!(!message.is_null());
            CStr::from_ptr(message)
        }
    }
}

impl<T> Drop for ZipErrorT<T>
where
    T: Borrow<ffi::zip_error_t> + BorrowMut<ffi::zip_error_t>,
{
    fn drop(&mut self) {
        if self.cleanup {
            unsafe { ffi::zip_error_fini(self.error.borrow_mut()) }
        }
    }
}

impl<T> Deref for ZipErrorT<T>
where
    T: Borrow<ffi::zip_error_t> + BorrowMut<ffi::zip_error_t>,
{
    type Target = ffi::zip_error_t;
    fn deref(&self) -> &Self::Target {
        self.error.borrow()
    }
}

impl<T> DerefMut for ZipErrorT<T>
where
    T: Borrow<ffi::zip_error_t> + BorrowMut<ffi::zip_error_t>,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.error.borrow_mut()
    }
}

impl<T> From<ZipErrorT<T>> for Error
where
    T: Borrow<ffi::zip_error_t> + BorrowMut<ffi::zip_error_t>,
{
    fn from(mut error: ZipErrorT<T>) -> Self {
        let system = error.system();
        let zip = error.zip();
        let message = error.message().to_string_lossy().into_owned();
        Error {
            system,
            zip,
            message,
        }
    }
}