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
use std::fmt;
use std::sync::mpsc::SendError;
use std::error::Error as StdError;
use x11rb::errors::{ConnectError, ConnectionError, ReplyError, ReplyOrIdError};
use x11rb::protocol::xproto::Atom;

#[must_use]
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    Set(SendError<Atom>),
    XcbConnect(ConnectError),
    XcbConnection(ConnectionError),
    XcbReplyOrId(ReplyOrIdError),
    XcbReply(ReplyError),
    Lock,
    Timeout,
    Owner,
    UnexpectedType(Atom),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::Error::*;
        match self {
            Set(e) => write!(f, "XCB - couldn't set atom: {:?}", e),
            XcbConnect(e) => write!(f, "XCB - couldn't establish conection: {:?}", e),
            XcbConnection(e) => write!(f, "XCB connection error: {:?}", e),
            XcbReplyOrId(e) => write!(f, "XCB reply error: {:?}", e),
            XcbReply(e) => write!(f, "XCB reply error: {:?}", e),
            Lock => write!(f, "XCB: Lock is poisoned"),
            Timeout => write!(f, "Selection timed out"),
            Owner => write!(f, "Failed to set new owner of XCB selection"),
            UnexpectedType(target) => write!(f, "Unexpected Reply type: {:?}", target),
        }
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        use self::Error::*;
        match self {
            Set(e) => Some(e),
            XcbConnection(e) => Some(e),
            XcbReply(e) => Some(e),
            XcbReplyOrId(e) => Some(e),
            XcbConnect(e) => Some(e),
            Lock | Timeout | Owner | UnexpectedType(_) => None,
        }
    }
}

macro_rules! define_from {
    ( $item:ident from $err:ty ) => {
        impl From<$err> for Error {
            fn from(err: $err) -> Error {
                Error::$item(err)
            }
        }
    }
}

define_from!(Set from SendError<Atom>);
define_from!(XcbConnect from ConnectError);
define_from!(XcbConnection from ConnectionError);
define_from!(XcbReply from ReplyError);
define_from!(XcbReplyOrId from ReplyOrIdError);