1 use std::{fmt, path::PathBuf};
5 #[derive(PartialEq, Eq)]
7 UnknownExtensionError(String),
8 MissingExtensionError(PathBuf),
9 // TODO: get rid of this error variant
13 FileNotFound(PathBuf),
15 InvalidZipArchive(&'static str),
17 UnsupportedZipArchive(&'static str),
19 CompressingRootFolder,
20 MissingArgumentsForCompression,
24 pub type Result<T> = std::result::Result<T, Error>;
26 impl fmt::Debug for Error {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 impl fmt::Display for Error {
33 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35 Error::MissingExtensionError(filename) => {
36 write!(f, "{} ", "[ERROR]".red())?;
37 // TODO: show MIME type of the unsupported file
38 write!(f, "cannot compress to {:?}, likely because it has an unsupported (or missing) extension.", filename)
40 Error::WalkdirError => {
41 // Already printed in the From block
44 Error::FileNotFound(file) => {
45 write!(f, "{} ", "[ERROR]".red())?;
46 if file == &PathBuf::from("") {
47 return write!(f, "file not found!");
49 write!(f, "file {:?} not found!", file)
51 Error::CompressingRootFolder => {
52 write!(f, "{} ", "[ERROR]".red())?;
54 writeln!(f, "It seems you're trying to compress the root folder.")?;
57 "{}This is unadvisable since ouch does compressions in-memory.",
62 "{}Use a more appropriate tool for this, such as {}.",
67 Error::MissingArgumentsForCompression => {
68 write!(f, "{} ", "[ERROR]".red())?;
69 write!(f,"The compress subcommands demands at least 2 arguments, see usage: <TODO-USAGE>")
71 Error::InternalError => {
72 write!(f, "{} ", "[ERROR]".red())?;
73 write!(f, "You've reached an internal error! This really should not have happened.\nPlease file an issue at {}", "https://github.com/vrmiguel/ouch".green())
83 impl From<std::io::Error> for Error {
84 fn from(err: std::io::Error) -> Self {
86 std::io::ErrorKind::NotFound => panic!("{}", err),
87 std::io::ErrorKind::PermissionDenied => Self::PermissionDenied,
88 std::io::ErrorKind::AlreadyExists => Self::AlreadyExists,
90 println!("{} {}", "[IO error]".red(), err);
97 impl From<zip::result::ZipError> for Error {
98 fn from(err: zip::result::ZipError) -> Self {
99 use zip::result::ZipError::*;
101 Io(io_err) => Self::from(io_err),
102 InvalidArchive(filename) => Self::InvalidZipArchive(filename),
103 FileNotFound => Self::FileNotFound("".into()),
104 UnsupportedArchive(filename) => Self::UnsupportedZipArchive(filename),
109 impl From<walkdir::Error> for Error {
110 fn from(err: walkdir::Error) -> Self {
111 eprintln!("{} {}", "[ERROR]".red(), err);
116 impl From<oof::OofError> for Error {
117 fn from(_err: oof::OofError) -> Self {
118 todo!("We need to implement this properly");