From 2f6ac5e54ca254ea2ed4822572195ebd7996091b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vin=C3=ADcius=20Miguel?= Date: Thu, 8 Apr 2021 00:26:02 -0300 Subject: [PATCH] error: Save std::io::Error as a crate::Error variant --- src/cli.rs | 3 +-- src/error.rs | 17 +++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index d9340f4..5015dd4 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -64,8 +64,7 @@ where if !path.as_ref().exists() { Err(crate::Error::FileNotFound(PathBuf::from(path.as_ref()))) } else { - eprintln!("[ERROR] {}", io_err); - Err(crate::Error::IoError) + Err(crate::Error::IoError(io_err)) } } } diff --git a/src/error.rs b/src/error.rs index ea6d69e..3e1bec5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,20 +1,20 @@ use std::{fmt, path::PathBuf}; use crate::utils::colors; -#[derive(PartialEq, Eq)] pub enum Error { UnknownExtensionError(String), MissingExtensionError(PathBuf), // TODO: get rid of this error variant InvalidUnicode, InvalidInput, - IoError, + IoError(std::io::Error), FileNotFound(PathBuf), AlreadyExists, InvalidZipArchive(&'static str), PermissionDenied, UnsupportedZipArchive(&'static str), InternalError, + OofError, CompressingRootFolder, MissingArgumentsForCompression, WalkdirError, @@ -75,6 +75,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::IoError(io_err) => { + write!(f, "{}[ERROR]{} {}", colors::red(), colors::reset(), io_err) + } _err => { // TODO write!(f, "") @@ -90,8 +93,7 @@ impl From for Error { std::io::ErrorKind::PermissionDenied => Self::PermissionDenied, std::io::ErrorKind::AlreadyExists => Self::AlreadyExists, _other => { - println!("{}[IO error]{} {}", colors::red(), colors::reset(), err); - Self::IoError + Self::IoError(err) } } } @@ -117,7 +119,10 @@ impl From for Error { } impl<'t> From> for Error { - fn from(_err: oof::OofError) -> Self { - todo!("We need to implement this properly"); + 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 } } -- 2.11.4.GIT