Tar compression seemingly working
[ouch.git] / src / error.rs
blob9cd2223a18210389ab3ebda0516aef3479cb1ce5
1 use std::fmt;
3 use colored::Colorize;
5 #[derive(PartialEq, Eq, Debug)]
6 pub enum Error {
7     UnknownExtensionError(String),
8     MissingExtensionError(String),
9     // TODO: get rid of this error variant
10     InvalidUnicode,
11     InvalidInput,
12     IOError,
13     FileNotFound,
14     AlreadyExists,
15     InvalidZipArchive(&'static str),
16     PermissionDenied,
17     UnsupportedZipArchive(&'static str),
18     FileTooShort,
19     InputsMustHaveBeenDecompressible(String),
22 pub type OuchResult<T> = Result<T, Error>;
24 impl fmt::Display for Error {
25     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27         match self {
28             Error::InvalidInput => write!(
29                 f,
30                 "When `-o/--output` is omitted, all input files should be compressed files."
31             ),
32             Error::MissingExtensionError(filename) => {
33                 write!(f, "cannot compress to \'{}\', likely because it has an unsupported (or missing) extension.", filename)
34             },
35             Error::InputsMustHaveBeenDecompressible(file) => {
36                 write!(f, "file '{}' is not decompressible", file.red())
37             },
38             // TODO: find out a way to attach the missing file in question here
39             Error::FileNotFound => {
40                 write!(f, "file not found!")
41             }
42             err => {
43                 // TODO
44                 write!(f, "todo: missing description for error {:?}", err)
45             }
46         }
47     }
50 impl From<std::io::Error> for Error {
51     fn from(err: std::io::Error) -> Self {
52         match err.kind() {
53             std::io::ErrorKind::NotFound => Self::FileNotFound,
54             std::io::ErrorKind::PermissionDenied => Self::PermissionDenied,
55             std::io::ErrorKind::AlreadyExists => Self::AlreadyExists,
56             _other => {
57                 println!("{}: {:#?}", "IO error".red(), err);
58                 Self::IOError
59             }
60         }
61     }
64 impl From<zip::result::ZipError> for Error {
65     fn from(err: zip::result::ZipError) -> Self {
66         use zip::result::ZipError::*;
67         match err {
68             Io(io_err) => Self::from(io_err),
69             InvalidArchive(filename) => Self::InvalidZipArchive(filename),
70             FileNotFound => Self::FileNotFound,
71             UnsupportedArchive(filename) => Self::UnsupportedZipArchive(filename)
72         }
73     }
76 impl From<niffler::error::Error> for Error {
77     fn from(err: niffler::error::Error) -> Self {
78         use niffler::error::Error as NifErr;
79         match err {
80             NifErr::FeatureDisabled => {
81                 // Ouch is using Niffler with all its features so
82                 // this should be unreachable.
83                 unreachable!();
84             },
85             NifErr::FileTooShort => Self::FileTooShort,
86             NifErr::IOError(io_err) => Self::from(io_err)
87         }
88     }
91 impl From<walkdir::Error> for Error {
92     fn from(err: walkdir::Error) -> Self {
93         eprintln!("{}: {}", "error".red(), err);
95         Self::InvalidInput
96     }