x11_clipboard/
error.rs

1use std::fmt;
2use std::sync::mpsc::SendError;
3use std::error::Error as StdError;
4use x11rb::errors::{ConnectError, ConnectionError, ReplyError, ReplyOrIdError};
5use x11rb::protocol::xproto::Atom;
6
7#[must_use]
8#[derive(Debug)]
9#[non_exhaustive]
10pub 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
24impl fmt::Display for Error {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26        use self::Error::*;
27        match self {
28            Set(e) => write!(f, "XCB - couldn't set atom: {:?}", e),
29            XcbConnect(e) => write!(f, "XCB - couldn't establish conection: {:?}", e),
30            XcbConnection(e) => write!(f, "XCB connection error: {:?}", e),
31            XcbReplyOrId(e) => write!(f, "XCB reply error: {:?}", e),
32            XcbReply(e) => 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) => write!(f, "Unexpected Reply type: {:?}", target),
37            EventFdCreate => write!(f, "Failed to create eventfd"),
38        }
39    }
40}
41
42impl StdError for Error {
43    fn source(&self) -> Option<&(dyn StdError + 'static)> {
44        use self::Error::*;
45        match self {
46            Set(e) => Some(e),
47            XcbConnection(e) => Some(e),
48            XcbReply(e) => Some(e),
49            XcbReplyOrId(e) => Some(e),
50            XcbConnect(e) => Some(e),
51            Lock | Timeout | Owner | UnexpectedType(_) | EventFdCreate => None,
52        }
53    }
54}
55
56macro_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
66define_from!(Set from SendError<Atom>);
67define_from!(XcbConnect from ConnectError);
68define_from!(XcbConnection from ConnectionError);
69define_from!(XcbReply from ReplyError);
70define_from!(XcbReplyOrId from ReplyOrIdError);