use displaydoc and thiserror to remove some boilerplate

This commit is contained in:
Danny McClanahan 2023-09-07 00:21:31 -04:00
parent 01a8ff41b1
commit a16a34f1a5
No known key found for this signature in database
GPG key ID: 6105C10F1A199CC7
3 changed files with 17 additions and 41 deletions

View file

@ -28,10 +28,12 @@ bzip2-rs = { version = "0.1.2", optional = true }
chrono = { version = "0.4.38", optional = true }
constant_time_eq = { version = "0.3.0", optional = true }
crc32fast = "1.4.0"
displaydoc = { version = "0.2.4", default-features = false }
flate2 = { version = "1.0.28", default-features = false, optional = true }
hmac = { version = "0.12.1", optional = true, features = ["reset"] }
pbkdf2 = { version = "0.12.2", optional = true }
sha1 = { version = "0.10.6", optional = true }
thiserror = "1.0.48"
time = { workspace = true, optional = true, features = [
"std",
] }

51
src/result.rs Normal file → Executable file
View file

@ -1,67 +1,38 @@
#![allow(unknown_lints)] // non_local_definitions isn't in Rust 1.70
#![allow(non_local_definitions)]
//! Error types that can be emitted from this library
use displaydoc::Display;
use thiserror::Error;
use std::error::Error;
use std::fmt;
use std::io;
use std::io::IntoInnerError;
use std::num::TryFromIntError;
/// Generic result type with ZipError as its error variant
pub type ZipResult<T> = Result<T, ZipError>;
/// Error type for Zip
#[derive(Debug)]
#[derive(Debug, Display, Error)]
#[non_exhaustive]
pub enum ZipError {
/// An Error caused by I/O
Io(io::Error),
/// i/o error: {0}
Io(#[from] io::Error),
/// This file is probably not a zip archive
/// invalid Zip archive: {0}
InvalidArchive(&'static str),
/// This archive is not supported
/// unsupported Zip archive: {0}
UnsupportedArchive(&'static str),
/// The requested file could not be found in the archive
/// specified file not found in archive
FileNotFound,
/// The password provided is incorrect
InvalidPassword,
}
impl From<io::Error> for ZipError {
fn from(err: io::Error) -> ZipError {
ZipError::Io(err)
}
}
impl<W> From<IntoInnerError<W>> for ZipError {
fn from(value: IntoInnerError<W>) -> Self {
ZipError::Io(value.into_error())
}
}
impl fmt::Display for ZipError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
ZipError::Io(err) => write!(fmt, "{err}"),
ZipError::InvalidArchive(err) => write!(fmt, "invalid Zip archive: {err}"),
ZipError::UnsupportedArchive(err) => write!(fmt, "unsupported Zip archive: {err}"),
ZipError::FileNotFound => write!(fmt, "specified file not found in archive"),
ZipError::InvalidPassword => write!(fmt, "incorrect password for encrypted file"),
}
}
}
impl Error for ZipError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
ZipError::Io(err) => Some(err),
_ => None,
}
}
}
impl ZipError {
/// The text used as an error when a password is required and not supplied
///

View file

@ -1418,7 +1418,10 @@ impl<W: Write + Seek> GenericZipWriter<W> {
#[cfg(feature = "deflate-zopfli")]
GenericZipWriter::ZopfliDeflater(w) => w.finish()?,
#[cfg(feature = "deflate-zopfli")]
GenericZipWriter::BufferedZopfliDeflater(w) => w.into_inner()?.finish()?,
GenericZipWriter::BufferedZopfliDeflater(w) => w
.into_inner()
.map_err(|e| ZipError::Io(e.into_error()))?
.finish()?,
#[cfg(feature = "bzip2")]
GenericZipWriter::Bzip2(w) => w.finish()?,
#[cfg(feature = "zstd")]