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())?;
70 writeln!(f,"The compress subcommands demands at least 2 arguments, an input file and an output file.")?;
71 writeln!(f,"{}Example: `ouch compress img.jpeg img.zip", spacing)?;
72 write!(f,"{}For more information, run `ouch --help`", spacing)
74 Error::InternalError => {
75 write!(f, "{} ", "[ERROR]".red())?;
76 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())
86 impl From<std::io::Error> for Error {
87 fn from(err: std::io::Error) -> Self {
89 std::io::ErrorKind::NotFound => panic!("{}", err),
90 std::io::ErrorKind::PermissionDenied => Self::PermissionDenied,
91 std::io::ErrorKind::AlreadyExists => Self::AlreadyExists,
93 println!("{} {}", "[IO error]".red(), err);
100 impl From<zip::result::ZipError> for Error {
101 fn from(err: zip::result::ZipError) -> Self {
102 use zip::result::ZipError::*;
104 Io(io_err) => Self::from(io_err),
105 InvalidArchive(filename) => Self::InvalidZipArchive(filename),
106 FileNotFound => Self::FileNotFound("".into()),
107 UnsupportedArchive(filename) => Self::UnsupportedZipArchive(filename),
112 impl From<walkdir::Error> for Error {
113 fn from(err: walkdir::Error) -> Self {
114 eprintln!("{} {}", "[ERROR]".red(), err);
119 impl From<oof::OofError> for Error {
120 fn from(_err: oof::OofError) -> Self {
121 todo!("We need to implement this properly");