Yi Kong | f19c3f2 | 2021-02-13 04:04:00 +0800 | [diff] [blame] | 1 | //! Error types that can be emitted from this library |
| 2 | |
David LeGare | 132eccb | 2022-04-11 16:44:12 +0000 | [diff] [blame] | 3 | use std::error::Error; |
| 4 | use std::fmt; |
Yi Kong | f19c3f2 | 2021-02-13 04:04:00 +0800 | [diff] [blame] | 5 | use std::io; |
| 6 | |
Yi Kong | f19c3f2 | 2021-02-13 04:04:00 +0800 | [diff] [blame] | 7 | /// Generic result type with ZipError as its error variant |
| 8 | pub type ZipResult<T> = Result<T, ZipError>; |
| 9 | |
| 10 | /// The given password is wrong |
David LeGare | 132eccb | 2022-04-11 16:44:12 +0000 | [diff] [blame] | 11 | #[derive(Debug)] |
Yi Kong | f19c3f2 | 2021-02-13 04:04:00 +0800 | [diff] [blame] | 12 | pub struct InvalidPassword; |
| 13 | |
David LeGare | 132eccb | 2022-04-11 16:44:12 +0000 | [diff] [blame] | 14 | impl fmt::Display for InvalidPassword { |
| 15 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 16 | write!(fmt, "invalid password for file in archive") |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | impl Error for InvalidPassword {} |
| 21 | |
Yi Kong | f19c3f2 | 2021-02-13 04:04:00 +0800 | [diff] [blame] | 22 | /// Error type for Zip |
David LeGare | 132eccb | 2022-04-11 16:44:12 +0000 | [diff] [blame] | 23 | #[derive(Debug)] |
Yi Kong | f19c3f2 | 2021-02-13 04:04:00 +0800 | [diff] [blame] | 24 | pub enum ZipError { |
| 25 | /// An Error caused by I/O |
David LeGare | 132eccb | 2022-04-11 16:44:12 +0000 | [diff] [blame] | 26 | Io(io::Error), |
Yi Kong | f19c3f2 | 2021-02-13 04:04:00 +0800 | [diff] [blame] | 27 | |
| 28 | /// This file is probably not a zip archive |
Yi Kong | f19c3f2 | 2021-02-13 04:04:00 +0800 | [diff] [blame] | 29 | InvalidArchive(&'static str), |
| 30 | |
| 31 | /// This archive is not supported |
Yi Kong | f19c3f2 | 2021-02-13 04:04:00 +0800 | [diff] [blame] | 32 | UnsupportedArchive(&'static str), |
| 33 | |
| 34 | /// The requested file could not be found in the archive |
Yi Kong | f19c3f2 | 2021-02-13 04:04:00 +0800 | [diff] [blame] | 35 | FileNotFound, |
| 36 | } |
| 37 | |
David LeGare | 132eccb | 2022-04-11 16:44:12 +0000 | [diff] [blame] | 38 | impl From<io::Error> for ZipError { |
| 39 | fn from(err: io::Error) -> ZipError { |
| 40 | ZipError::Io(err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl fmt::Display for ZipError { |
| 45 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 46 | match self { |
Jeff Vander Stoep | 486ff5a | 2023-02-17 09:53:43 +0100 | [diff] [blame] | 47 | ZipError::Io(err) => write!(fmt, "{err}"), |
| 48 | ZipError::InvalidArchive(err) => write!(fmt, "invalid Zip archive: {err}"), |
| 49 | ZipError::UnsupportedArchive(err) => write!(fmt, "unsupported Zip archive: {err}"), |
David LeGare | 132eccb | 2022-04-11 16:44:12 +0000 | [diff] [blame] | 50 | ZipError::FileNotFound => write!(fmt, "specified file not found in archive"), |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl Error for ZipError { |
| 56 | fn source(&self) -> Option<&(dyn Error + 'static)> { |
| 57 | match self { |
| 58 | ZipError::Io(err) => Some(err), |
| 59 | _ => None, |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Joel Galenson | 4558110 | 2021-06-21 14:24:55 -0700 | [diff] [blame] | 64 | impl ZipError { |
| 65 | /// The text used as an error when a password is required and not supplied |
| 66 | /// |
| 67 | /// ```rust,no_run |
| 68 | /// # use zip::result::ZipError; |
| 69 | /// # let mut archive = zip::ZipArchive::new(std::io::Cursor::new(&[])).unwrap(); |
| 70 | /// match archive.by_index(1) { |
| 71 | /// Err(ZipError::UnsupportedArchive(ZipError::PASSWORD_REQUIRED)) => eprintln!("a password is needed to unzip this file"), |
| 72 | /// _ => (), |
| 73 | /// } |
| 74 | /// # () |
| 75 | /// ``` |
| 76 | pub const PASSWORD_REQUIRED: &'static str = "Password required to decrypt file"; |
| 77 | } |
| 78 | |
Yi Kong | f19c3f2 | 2021-02-13 04:04:00 +0800 | [diff] [blame] | 79 | impl From<ZipError> for io::Error { |
| 80 | fn from(err: ZipError) -> io::Error { |
| 81 | io::Error::new(io::ErrorKind::Other, err) |
| 82 | } |
| 83 | } |
Jeff Vander Stoep | 486ff5a | 2023-02-17 09:53:43 +0100 | [diff] [blame] | 84 | |
| 85 | /// Error type for time parsing |
| 86 | #[derive(Debug)] |
| 87 | pub struct DateTimeRangeError; |
| 88 | |
| 89 | impl fmt::Display for DateTimeRangeError { |
| 90 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 91 | write!( |
| 92 | fmt, |
| 93 | "a date could not be represented within the bounds the MS-DOS date range (1980-2107)" |
| 94 | ) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | impl Error for DateTimeRangeError {} |