summaryrefslogtreecommitdiff
path: root/src/error.rs
blob: d0253affa53bb4ce80ba833852e16b61c1cf817b (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
use std::fmt::{Display, Formatter};

/// The Result type used throughout
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// All errors that can be encountered when running
#[derive(Debug)]
pub enum Error {
    // #[error("xcb returned a screen that doesn't exist")]
    NoSuchScreen,

    // #[error("another wm is running")]
    OtherWMRunning,

    // #[error("generic xcb error: {0}")]
    Xcb(xcb::Error),

    // #[error("connection error: {0}")]
    Connection(xcb::ConnError),

    // #[error("protocol error: {0}")]
    Protocol(xcb::ProtocolError),
}

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

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NoSuchScreen => write!(f, "xcb returned a screen that doesn't exist"),
            Self::OtherWMRunning => write!(f, "another window manager is running"),
            Self::Xcb(e) => write!(f, "generic xcb error: {e}"),
            Self::Connection(e) => write!(f, "connection error: {e}"),
            Self::Protocol(e) => write!(f, "protocol error: {e}"),
        }
    }
}

impl From<xcb::Error> for Error {
    fn from(e: xcb::Error) -> Self {
        Self::Xcb(e)
    }
}
impl From<xcb::ConnError> for Error {
    fn from(e: xcb::ConnError) -> Self {
        Self::Connection(e)
    }
}
impl From<xcb::ProtocolError> for Error {
    fn from(e: xcb::ProtocolError) -> Self {
        Self::Protocol(e)
    }
}