Replace io::Error by reason: String - crate::Error

By removing io::Error from crate::Error, we can implement PartialEq in
the enum to make testing easier.
This commit is contained in:
João M. Bezerra 2021-05-28 02:15:12 -03:00
parent 77cfb48056
commit cd43f215ea
2 changed files with 7 additions and 11 deletions

View File

@ -31,6 +31,7 @@ pub fn parse_args() -> crate::Result<ParsedArgs> {
parse_args_from(args) parse_args_from(args)
} }
#[derive(Debug)]
pub struct ParsedArgs { pub struct ParsedArgs {
pub command: Command, pub command: Command,
pub flags: oof::Flags, pub flags: oof::Flags,
@ -63,7 +64,7 @@ where
if !path.as_ref().exists() { if !path.as_ref().exists() {
Err(crate::Error::FileNotFound(PathBuf::from(path.as_ref()))) Err(crate::Error::FileNotFound(PathBuf::from(path.as_ref())))
} else { } else {
Err(crate::Error::IoError(io_err)) Err(io_err.into())
} }
}, },
} }

View File

@ -2,13 +2,14 @@ use std::{fmt, path::PathBuf};
use crate::{oof, utils::colors}; use crate::{oof, utils::colors};
#[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
UnknownExtensionError(String), UnknownExtensionError(String),
MissingExtensionError(PathBuf), MissingExtensionError(PathBuf),
// TODO: get rid of this error variant // TODO: get rid of this error variant
InvalidUnicode, InvalidUnicode,
InvalidInput, InvalidInput,
IoError(std::io::Error), IoError { reason: String },
FileNotFound(PathBuf), FileNotFound(PathBuf),
AlreadyExists, AlreadyExists,
InvalidZipArchive(&'static str), InvalidZipArchive(&'static str),
@ -24,12 +25,6 @@ pub enum Error {
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self)
}
}
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
@ -77,8 +72,8 @@ impl fmt::Display for Error {
write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; 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()) 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) => { Error::IoError { reason } => {
write!(f, "{}[ERROR]{} {}", colors::red(), colors::reset(), io_err) write!(f, "{}[ERROR]{} {}", colors::red(), colors::reset(), reason)
}, },
Error::CompressionTypo => { Error::CompressionTypo => {
write!(f, "Did you mean {}ouch compress{}?", colors::magenta(), colors::reset()) write!(f, "Did you mean {}ouch compress{}?", colors::magenta(), colors::reset())
@ -96,7 +91,7 @@ impl From<std::io::Error> for Error {
std::io::ErrorKind::NotFound => panic!("{}", err), std::io::ErrorKind::NotFound => panic!("{}", err),
std::io::ErrorKind::PermissionDenied => Self::PermissionDenied, std::io::ErrorKind::PermissionDenied => Self::PermissionDenied,
std::io::ErrorKind::AlreadyExists => Self::AlreadyExists, std::io::ErrorKind::AlreadyExists => Self::AlreadyExists,
_other => Self::IoError(err), _other => Self::IoError { reason: err.to_string() },
} }
} }
} }