1 | use std::fmt; |
2 | use std::sync::mpsc::SendError; |
3 | use std::error::Error as StdError; |
4 | use x11rb::errors::{ConnectError, ConnectionError, ReplyError, ReplyOrIdError}; |
5 | use x11rb::protocol::xproto::Atom; |
6 | |
7 | #[must_use ] |
8 | #[derive (Debug)] |
9 | #[non_exhaustive ] |
10 | pub enum Error { |
11 | Set(SendError<Atom>), |
12 | XcbConnect(ConnectError), |
13 | XcbConnection(ConnectionError), |
14 | XcbReplyOrId(ReplyOrIdError), |
15 | XcbReply(ReplyError), |
16 | Lock, |
17 | Timeout, |
18 | Owner, |
19 | UnexpectedType(Atom), |
20 | // Could change name on next major, since this uses pipes now. |
21 | EventFdCreate, |
22 | } |
23 | |
24 | impl fmt::Display for Error { |
25 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
26 | use self::Error::*; |
27 | match self { |
28 | Set(e: &SendError) => write!(f, "XCB - couldn't set atom: {:?}" , e), |
29 | XcbConnect(e: &ConnectError) => write!(f, "XCB - couldn't establish conection: {:?}" , e), |
30 | XcbConnection(e: &ConnectionError) => write!(f, "XCB connection error: {:?}" , e), |
31 | XcbReplyOrId(e: &ReplyOrIdError) => write!(f, "XCB reply error: {:?}" , e), |
32 | XcbReply(e: &ReplyError) => write!(f, "XCB reply error: {:?}" , e), |
33 | Lock => write!(f, "XCB: Lock is poisoned" ), |
34 | Timeout => write!(f, "Selection timed out" ), |
35 | Owner => write!(f, "Failed to set new owner of XCB selection" ), |
36 | UnexpectedType(target: &u32) => write!(f, "Unexpected Reply type: {:?}" , target), |
37 | EventFdCreate => write!(f, "Failed to create eventfd" ), |
38 | } |
39 | } |
40 | } |
41 | |
42 | impl StdError for Error { |
43 | fn source(&self) -> Option<&(dyn StdError + 'static)> { |
44 | use self::Error::*; |
45 | match self { |
46 | Set(e: &SendError) => Some(e), |
47 | XcbConnection(e: &ConnectionError) => Some(e), |
48 | XcbReply(e: &ReplyError) => Some(e), |
49 | XcbReplyOrId(e: &ReplyOrIdError) => Some(e), |
50 | XcbConnect(e: &ConnectError) => Some(e), |
51 | Lock | Timeout | Owner | UnexpectedType(_) | EventFdCreate => None, |
52 | } |
53 | } |
54 | } |
55 | |
56 | macro_rules! define_from { |
57 | ( $item:ident from $err:ty ) => { |
58 | impl From<$err> for Error { |
59 | fn from(err: $err) -> Error { |
60 | Error::$item(err) |
61 | } |
62 | } |
63 | } |
64 | } |
65 | |
66 | define_from!(Set from SendError<Atom>); |
67 | define_from!(XcbConnect from ConnectError); |
68 | define_from!(XcbConnection from ConnectionError); |
69 | define_from!(XcbReply from ReplyError); |
70 | define_from!(XcbReplyOrId from ReplyOrIdError); |
71 | |