summaryrefslogtreecommitdiff
path: root/src/error.rs
blob: dcbcfe927560e6dbe375f86f6a59a508b5f49be6 (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 {
            Error::NoSuchScreen => write!(f, "xcb returned a screen that doesn't exist"),
            Error::OtherWMRunning => write!(f, "another window manager is running"),
            Error::Xcb(e) => write!(f, "generic xcb error: {}", e),
            Error::Connection(e) => write!(f, "connection error: {}", e),
            Error::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)
    }
}