summaryrefslogtreecommitdiffstats
path: root/src/errors.rs
blob: fa58e790817860d491a23a732f474ac2fd3fffb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::path::PathBuf;

#[derive(Debug)]
pub enum FxError {
    Arg(String),
    TerminalSizeDetection,
    Io(String),
    Dirs(String),
    GetItem,
    OpenItem(String),
    OpenNewWindow(String),
    DefaultEditor,
    Yaml(String),
    WalkDir(String),
    Encode,
    PutItem(PathBuf),
    RemoveItem(PathBuf),
    TooSmallWindowSize,
    Log(String),
    Unpack(String),
    InvalidPath,
    Panic,
    #[cfg(any(target_os = "linux", target_os = "netbsd"))]
    Nix(String),
}

impl std::error::Error for FxError {}

impl std::fmt::Display for FxError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let printable = match self {
            FxError::Arg(s) => s.to_owned(),
            FxError::TerminalSizeDetection => "Error: Cannot detect terminal size".to_owned(),
            FxError::Io(s) => s.to_owned(),
            FxError::Dirs(s) => s.to_owned(),
            FxError::GetItem => "Error: Cannot get item info".to_owned(),
            FxError::OpenItem(s) => s.to_owned(),
            FxError::OpenNewWindow(s) => s.to_owned(),
            FxError::DefaultEditor => {
                "$EDITOR may not be set, or config file may be invalid.".to_owned()
            }
            FxError::Yaml(s) => s.to_owned(),
            FxError::WalkDir(s) => s.to_owned(),
            FxError::Encode => "Error: Incorrect encoding".to_owned(),
            FxError::PutItem(s) => format!("Error: Cannot copy -> {:?}", s),
            FxError::RemoveItem(s) => format!("Error: Cannot remove -> {:?}", s),
            FxError::TooSmallWindowSize => "Error: Too small window size".to_owned(),
            FxError::Log(s) => s.to_owned(),
            FxError::Unpack(s) => s.to_owned(),
            FxError::InvalidPath => "Error: Path may contain invalid unicode".to_owned(),
            FxError::Panic => "Error: felix panicked".to_owned(),
            #[cfg(any(target_os = "linux", target_os = "netbsd"))]
            FxError::Nix(s) => s.to_owned(),
        };
        write!(f, "{}", printable)
    }
}

impl From<std::io::Error> for FxError {
    fn from(err: std::io::Error) -> Self {
        FxError::Io(err.to_string())
    }
}
impl From<serde_yaml::Error> for FxError {
    fn from(err: serde_yaml::Error) -> Self {
        FxError::Yaml(err.to_string())
    }
}

impl From<walkdir::Error> for FxError {
    fn from(err: walkdir::Error) -> Self {
        FxError::WalkDir(err.to_string())
    }
}

impl From<log::SetLoggerError> for FxError {
    fn from(err: log::SetLoggerError) -> Self {
        FxError::Log(err.to_string())
    }
}

impl From<std::string::FromUtf8Error> for FxError {
    fn from(_err: std::string::FromUtf8Error) -> Self {
        FxError::Encode
    }
}

impl From<zip::result::ZipError> for FxError {
    fn from(err: zip::result::ZipError) -> Self {
        FxError::Unpack(err.to_string())
    }
}

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
impl From<nix::errno::Errno> for FxError {
    fn from(err: nix::errno::Errno) -> Self {
        FxError::Nix(err.to_string())
    }
}