1 use std::{fmt, path::PathBuf};
5 #[derive(PartialEq, Eq)]
7 UnknownExtensionError(String),
8 MissingExtensionError(String),
9 // TODO: get rid of this error variant
13 FileNotFound(PathBuf),
15 InvalidZipArchive(&'static str),
17 UnsupportedZipArchive(&'static str),
18 InputsMustHaveBeenDecompressible(PathBuf),
20 CompressingRootFolder,
24 pub type Result<T> = std::result::Result<T, Error>;
26 // impl std::error::Error for Error {
27 // fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
28 // // TODO: get rid of PartialEq and Eq in self::Error in order to
29 // // correctly use `source`.
34 impl fmt::Debug for Error {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 impl fmt::Display for Error {
41 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43 Error::MissingExtensionError(filename) => {
44 write!(f, "{} ", "[ERROR]".red())?;
45 write!(f, "cannot compress to \'{}\', likely because it has an unsupported (or missing) extension.", filename)
47 Error::InputsMustHaveBeenDecompressible(file) => {
48 write!(f, "{} ", "[ERROR]".red())?;
49 write!(f, "file '{:?}' is not decompressible", file)
51 Error::WalkdirError => {
52 // Already printed in the From block
55 Error::FileNotFound(file) => {
56 write!(f, "{} ", "[ERROR]".red())?;
57 // TODO: check if file == ""
58 write!(f, "file {:?} not found!", file)
60 Error::CompressingRootFolder => {
61 write!(f, "{} ", "[ERROR]".red())?;
63 writeln!(f, "It seems you're trying to compress the root folder.")?;
66 "{}This is unadvisable since ouch does compressions in-memory.",
71 "{}Use a more appropriate tool for this, such as {}.",
76 Error::InternalError => {
77 write!(f, "{} ", "[ERROR]".red())?;
78 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())
88 impl From<std::io::Error> for Error {
89 fn from(err: std::io::Error) -> Self {
91 std::io::ErrorKind::NotFound => Self::FileNotFound("".into()),
92 std::io::ErrorKind::PermissionDenied => Self::PermissionDenied,
93 std::io::ErrorKind::AlreadyExists => Self::AlreadyExists,
95 println!("{} {}", "[IO error]".red(), err);
102 impl From<zip::result::ZipError> for Error {
103 fn from(err: zip::result::ZipError) -> Self {
104 use zip::result::ZipError::*;
106 Io(io_err) => Self::from(io_err),
107 InvalidArchive(filename) => Self::InvalidZipArchive(filename),
108 FileNotFound => Self::FileNotFound("".into()),
109 UnsupportedArchive(filename) => Self::UnsupportedZipArchive(filename),
114 impl From<walkdir::Error> for Error {
115 fn from(err: walkdir::Error) -> Self {
116 eprintln!("{} {}", "[ERROR]".red(), err);
121 impl From<oof::OofError> for Error {
122 fn from(err: oof::OofError) -> Self {
123 todo!("We need to implement this properly");