From bf8ef70d161b7c529922e4bdc3c077d184ae3aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 28 May 2021 04:34:06 -0300 Subject: [PATCH] Fixing Oof and Walkdir Error Display --- src/error.rs | 20 +++++++++----------- src/oof/error.rs | 2 +- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/error.rs b/src/error.rs index d7380d1..38577ba 100644 --- a/src/error.rs +++ b/src/error.rs @@ -16,11 +16,11 @@ pub enum Error { PermissionDenied, UnsupportedZipArchive(&'static str), InternalError, - OofError, + OofError(oof::OofError), CompressingRootFolder, MissingArgumentsForCompression, CompressionTypo, - WalkdirError, + WalkdirError { reason: String }, } pub type Result = std::result::Result; @@ -33,9 +33,8 @@ impl fmt::Display for Error { // TODO: show MIME type of the unsupported file write!(f, "cannot compress to {:?}, likely because it has an unsupported (or missing) extension.", filename) }, - Error::WalkdirError => { - // Already printed in the From block - write!(f, "") + Error::WalkdirError { reason } => { + write!(f, "{}[ERROR]{} {}", colors::red(), colors::reset(), reason) }, Error::FileNotFound(file) => { write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; @@ -72,6 +71,9 @@ impl fmt::Display for Error { write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; write!(f, "You've reached an internal error! This really should not have happened.\nPlease file an issue at {}https://github.com/vrmiguel/ouch{}", colors::green(), colors::reset()) }, + Error::OofError(err) => { + write!(f, "{}[ERROR]{} {}", colors::red(), colors::reset(), err) + }, Error::IoError { reason } => { write!(f, "{}[ERROR]{} {}", colors::red(), colors::reset(), reason) }, @@ -110,16 +112,12 @@ impl From for Error { impl From for Error { fn from(err: walkdir::Error) -> Self { - eprintln!("{}[ERROR]{} {}", colors::red(), colors::reset(), err); - Self::WalkdirError + Self::WalkdirError { reason: err.to_string() } } } impl From for Error { fn from(err: oof::OofError) -> Self { - // To avoid entering a lifetime hell, we'll just print the Oof error here - // and skip saving it into a variant of Self - println!("{}[ERROR]{} {}", colors::red(), colors::reset(), err); - Self::OofError + Self::OofError(err) } } diff --git a/src/oof/error.rs b/src/oof/error.rs index a5c1161..a723798 100644 --- a/src/oof/error.rs +++ b/src/oof/error.rs @@ -2,7 +2,7 @@ use std::{error, ffi::OsString, fmt}; use super::Flag; -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum OofError { FlagValueConflict { flag: Flag,