Add a sad Python script for Ouch testing
[ouch.git] / src / error.rs
blobf1b99edba7fe72d01adcd99d608074b99099f7a3
1 use std::{fmt, path::PathBuf};
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(PathBuf),
14     AlreadyExists,
15     InvalidZipArchive(&'static str),
16     PermissionDenied,
17     UnsupportedZipArchive(&'static str),
18     InputsMustHaveBeenDecompressible(PathBuf),
21 pub type OuchResult<T> = Result<T, Error>;
23 impl fmt::Display for Error {
24     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25         write!(f, "{} ", "[ERROR]".red())?;
26         match self {
27             Error::MissingExtensionError(filename) => {
28                 write!(f, "cannot compress to \'{}\', likely because it has an unsupported (or missing) extension.", filename)
29             },
30             Error::InputsMustHaveBeenDecompressible(file) => {
31                 write!(f, "file '{:?}' is not decompressible", file)
32             },
33             Error::FileNotFound(file) => {
34                 // TODO: check if file == ""
35                 write!(f, "file {:?} not found!", file)
36             }
37             _err => {
38                 // TODO
39                 write!(f, "")
40             }
41         }
42     }
45 impl From<std::io::Error> for Error {
46     fn from(err: std::io::Error) -> Self {
47         match err.kind() {
48             std::io::ErrorKind::NotFound => Self::FileNotFound("".into()),
49             std::io::ErrorKind::PermissionDenied => Self::PermissionDenied,
50             std::io::ErrorKind::AlreadyExists => Self::AlreadyExists,
51             _other => {
52                 println!("{}: {}", "IO error".red(), err);
53                 Self::IOError
54             }
55         }
56     }
59 impl From<zip::result::ZipError> for Error {
60     fn from(err: zip::result::ZipError) -> Self {
61         use zip::result::ZipError::*;
62         match err {
63             Io(io_err) => Self::from(io_err),
64             InvalidArchive(filename) => Self::InvalidZipArchive(filename),
65             FileNotFound => Self::FileNotFound("".into()),
66             UnsupportedArchive(filename) => Self::UnsupportedZipArchive(filename)
67         }
68     }
71 impl From<walkdir::Error> for Error {
72     fn from(err: walkdir::Error) -> Self {
73         eprintln!("{}: {}", "error".red(), err);
75         Self::InvalidInput
76     }