Skip to main content

zenity_rs/
error.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum Error {
5    #[cfg(feature = "x11")]
6    X11(X11Error),
7    #[cfg(feature = "wayland")]
8    Wayland(WaylandError),
9    NoDisplay,
10    Io(std::io::Error),
11}
12
13#[cfg(feature = "x11")]
14#[derive(Debug)]
15pub enum X11Error {
16    Connect(x11rb::errors::ConnectError),
17    Connection(x11rb::errors::ConnectionError),
18    Reply(x11rb::errors::ReplyError),
19    NoVisual,
20}
21
22#[cfg(feature = "wayland")]
23#[derive(Debug)]
24pub enum WaylandError {
25    Connect(wayland_client::ConnectError),
26    Dispatch(wayland_client::DispatchError),
27    MissingGlobal(&'static str),
28    NotConfigured,
29}
30
31impl fmt::Display for Error {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            #[cfg(feature = "x11")]
35            Error::X11(e) => write!(f, "X11 error: {e}"),
36            #[cfg(feature = "wayland")]
37            Error::Wayland(e) => write!(f, "Wayland error: {e}"),
38            Error::NoDisplay => write!(f, "no display server available"),
39            Error::Io(e) => write!(f, "IO error: {e}"),
40        }
41    }
42}
43
44impl std::error::Error for Error {}
45
46#[cfg(feature = "x11")]
47impl fmt::Display for X11Error {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        match self {
50            X11Error::Connect(e) => write!(f, "connect: {e}"),
51            X11Error::Connection(e) => write!(f, "connection: {e}"),
52            X11Error::Reply(e) => write!(f, "reply: {e}"),
53            X11Error::NoVisual => write!(f, "no suitable visual found"),
54        }
55    }
56}
57
58#[cfg(feature = "wayland")]
59impl fmt::Display for WaylandError {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match self {
62            WaylandError::Connect(e) => write!(f, "connect: {e}"),
63            WaylandError::Dispatch(e) => write!(f, "dispatch: {e}"),
64            WaylandError::MissingGlobal(name) => write!(f, "missing global: {name}"),
65            WaylandError::NotConfigured => write!(f, "surface not configured"),
66        }
67    }
68}
69
70impl From<std::io::Error> for Error {
71    fn from(e: std::io::Error) -> Self {
72        Error::Io(e)
73    }
74}
75
76#[cfg(feature = "x11")]
77impl From<x11rb::errors::ConnectError> for Error {
78    fn from(e: x11rb::errors::ConnectError) -> Self {
79        Error::X11(X11Error::Connect(e))
80    }
81}
82
83#[cfg(feature = "x11")]
84impl From<x11rb::errors::ConnectionError> for Error {
85    fn from(e: x11rb::errors::ConnectionError) -> Self {
86        Error::X11(X11Error::Connection(e))
87    }
88}
89
90#[cfg(feature = "x11")]
91impl From<x11rb::errors::ReplyError> for Error {
92    fn from(e: x11rb::errors::ReplyError) -> Self {
93        Error::X11(X11Error::Reply(e))
94    }
95}
96
97#[cfg(feature = "x11")]
98impl From<x11rb::errors::ReplyOrIdError> for Error {
99    fn from(e: x11rb::errors::ReplyOrIdError) -> Self {
100        match e {
101            x11rb::errors::ReplyOrIdError::ConnectionError(e) => {
102                Error::X11(X11Error::Connection(e))
103            }
104            x11rb::errors::ReplyOrIdError::X11Error(e) => Error::X11(X11Error::Reply(e.into())),
105            x11rb::errors::ReplyOrIdError::IdsExhausted => Error::X11(X11Error::NoVisual),
106        }
107    }
108}
109
110#[cfg(feature = "wayland")]
111impl From<wayland_client::ConnectError> for Error {
112    fn from(e: wayland_client::ConnectError) -> Self {
113        Error::Wayland(WaylandError::Connect(e))
114    }
115}
116
117#[cfg(feature = "wayland")]
118impl From<wayland_client::DispatchError> for Error {
119    fn from(e: wayland_client::DispatchError) -> Self {
120        Error::Wayland(WaylandError::Dispatch(e))
121    }
122}
123
124#[cfg(feature = "wayland")]
125impl From<wayland_client::backend::WaylandError> for Error {
126    fn from(e: wayland_client::backend::WaylandError) -> Self {
127        // Convert to IO error since WaylandError is usually an IO issue
128        Error::Io(std::io::Error::other(e.to_string()))
129    }
130}