error: Save std::io::Error as a crate::Error variant

This commit is contained in:
Vinícius Miguel 2021-04-08 00:26:02 -03:00
parent 613074dff1
commit 2f6ac5e54c
2 changed files with 12 additions and 8 deletions

View File

@ -64,8 +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 {
eprintln!("[ERROR] {}", io_err); Err(crate::Error::IoError(io_err))
Err(crate::Error::IoError)
} }
} }
} }

View File

@ -1,20 +1,20 @@
use std::{fmt, path::PathBuf}; use std::{fmt, path::PathBuf};
use crate::utils::colors; use crate::utils::colors;
#[derive(PartialEq, Eq)]
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, IoError(std::io::Error),
FileNotFound(PathBuf), FileNotFound(PathBuf),
AlreadyExists, AlreadyExists,
InvalidZipArchive(&'static str), InvalidZipArchive(&'static str),
PermissionDenied, PermissionDenied,
UnsupportedZipArchive(&'static str), UnsupportedZipArchive(&'static str),
InternalError, InternalError,
OofError,
CompressingRootFolder, CompressingRootFolder,
MissingArgumentsForCompression, MissingArgumentsForCompression,
WalkdirError, WalkdirError,
@ -75,6 +75,9 @@ 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) => {
write!(f, "{}[ERROR]{} {}", colors::red(), colors::reset(), io_err)
}
_err => { _err => {
// TODO // TODO
write!(f, "") write!(f, "")
@ -90,8 +93,7 @@ impl From<std::io::Error> for Error {
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 => { _other => {
println!("{}[IO error]{} {}", colors::red(), colors::reset(), err); Self::IoError(err)
Self::IoError
} }
} }
} }
@ -117,7 +119,10 @@ impl From<walkdir::Error> for Error {
} }
impl<'t> From<oof::OofError<'t>> for Error { impl<'t> From<oof::OofError<'t>> for Error {
fn from(_err: oof::OofError) -> Self { fn from(err: oof::OofError) -> Self {
todo!("We need to implement this properly"); // 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
} }
} }