tmux_interface/
error.rs

1use std::fmt;
2
3//pub type Result<T> = std::result::Result<T, Error>;
4
5// XXX: mb separate errors by modules for eventual lib splitting
6#[derive(Debug)]
7pub enum Error {
8    Hook,
9    // options parse errors
10    ParseStatusKeys,
11    ParseVersion,
12    ParseWindowFlags,
13    ParseSwitch,
14    ParseSetClipboard,
15    ParseActivity,
16    ParseAction,
17    ParseStatus,
18    ParseWindowSize,
19    ParseStatusJustify,
20    ParseStatusPosition,
21    ParseClockModeStyle,
22    ParsePaneBorderStatus,
23    ParseRemainOnExit,
24    ParseExtendedKeys,
25    ParseTerminalFeatures,
26    ParseModeMouse,
27    ParseDetachOnDestroy,
28
29    /// Tmux error message
30    Tmux(String),
31    /// IO error
32    IO(std::io::Error),
33
34    ParseInt(std::num::ParseIntError),
35    Parse(std::string::ParseError),
36    CMParseNum,
37    CMParseStr,
38}
39
40//impl Error {
41//pub fn new(error: &str) -> Self {
42//}
43//}
44
45// FIXME: all branches, review enum
46impl std::error::Error for Error {
47    fn cause(&self) -> Option<&dyn std::error::Error> {
48        match *self {
49            Self::IO(ref err) => Some(err),
50            Self::ParseInt(ref err) => Some(err),
51            Self::Parse(ref err) => Some(err),
52            _ => None,
53        }
54    }
55}
56
57impl fmt::Display for Error {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        match self {
60            Self::Tmux(ref msg) => write!(f, "{}", msg),
61            Self::IO(ref err) => err.fmt(f),
62            Self::ParseInt(ref err) => err.fmt(f),
63            Self::Parse(ref err) => err.fmt(f),
64            _ => "".fmt(f),
65        }
66    }
67}
68
69impl From<std::io::Error> for Error {
70    fn from(err: std::io::Error) -> Self {
71        Self::IO(err)
72    }
73}
74
75impl From<std::num::ParseIntError> for Error {
76    fn from(err: std::num::ParseIntError) -> Self {
77        Self::ParseInt(err)
78    }
79}
80
81impl From<std::string::ParseError> for Error {
82    fn from(err: std::string::ParseError) -> Self {
83        Self::Parse(err)
84    }
85}