summaryrefslogtreecommitdiffstats
path: root/src/tmux/error.rs
blob: c857e5b3a896884f812fffd7a1419b10fcc03adb (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
#[derive(Debug)]
pub enum FormatError {
    Path(String),
    Timestamp(String),
}

#[derive(Debug)]
pub enum Error {
    IO(std::io::Error),
    Utf8(std::str::Utf8Error),
    PathFormat(String),
    TimestampFormat(String),
    ParseInt(std::num::ParseIntError),
    NotFound,
}

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

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::IO(err) => write!(f, "IO error: {}", err),
            Error::Utf8(err) => write!(f, "Failed to parse UTF8: {}", err),

            Error::PathFormat(s) => write!(f, "Invalid path format: {}", s),
            Error::TimestampFormat(s) => write!(f, "Invalid timestamp format: {}", s),
            Error::ParseInt(err) => write!(f, "Failed to parse int: {}", err),
            Error::NotFound => write!(f, "Tmux session not found"),
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(value: std::io::Error) -> Self {
        Self::IO(value)
    }
}

impl From<std::str::Utf8Error> for Error {
    fn from(value: std::str::Utf8Error) -> Self {
        Self::Utf8(value)
    }
}

impl From<std::num::ParseIntError> for Error {
    fn from(value: std::num::ParseIntError) -> Self {
        Self::ParseInt(value)
    }
}