tony / oxwm

move `window_manager::new` off anyhow result Browse files
- All error paths are defined in the `WindowManagerError` type
- Some generic bs involved, this might change
emzywastaken committed 2025-10-13 parent 618817d commit 407417fb6c26e5f503dd076d08543926063a3c2b
Showing 1 changed file with 98 additions and 1 deletions. UnifiedSplit
+98 −1 src/window_manager.rs
@@ -36,8 +36,105 @@ pub struct WindowManager {
36 bar: Bar, 36 bar: Bar,
37 } 37 }
38 38
39 +#[derive(Debug)]
40 +#[non_exhaustive]
41 +pub enum WindowManagerError {
42 + X11(X11Error),
43 + Io(std::io::Error),
44 + Anyhow(anyhow::Error),
45 +}
46 +
47 +#[derive(Debug)]
48 +#[non_exhaustive]
49 +pub enum X11Error {
50 + ConnectError(x11rb::errors::ConnectError),
51 + ConnectionError(x11rb::errors::ConnectionError),
52 + ReplyError(x11rb::errors::ReplyError),
53 + ReplyOrIdError(x11rb::errors::ReplyOrIdError),
54 +}
55 +
56 +impl std::fmt::Display for WindowManagerError {
57 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58 + match self {
59 + // Self::X11(x11_error) => write!(f, "{:?}", x11_error),
60 + Self::X11(error) => write!(f, "{}", error),
61 + Self::Io(error) => write!(f, "{}", error),
62 + Self::Anyhow(error) => write!(f, "{}", error),
63 + }
64 + }
65 +}
66 +
67 +impl std::error::Error for WindowManagerError {}
68 +
69 +impl std::fmt::Display for X11Error {
70 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71 + match self {
72 + X11Error::ConnectError(err) => write!(f, "{}", err),
73 + X11Error::ConnectionError(err) => write!(f, "{}", err),
74 + X11Error::ReplyError(err) => write!(f, "{}", err),
75 + X11Error::ReplyOrIdError(err) => write!(f, "{}", err),
76 + }
77 + }
78 +}
79 +
80 +// impl<T: std::error::Error + Send + Sync + 'static> From<T> for WindowManagerError {
81 +// fn from(value: T) -> Self {
82 +// Self::Anyhow(value.into())
83 +// }
84 +// }
85 +
86 +// TODO: Eliminate all library level `anyhow::Error`
87 +// impl<E: Into<anyhow::Error>> From<E> for WindowManagerError {
88 +// fn from(value: E) -> Self {
89 +// Self::Anyhow(value.into())
90 +// }
91 +// }
92 +
93 +impl From<X11Error> for WindowManagerError {
94 + fn from(value: X11Error) -> Self {
95 + Self::X11(value)
96 + }
97 +}
98 +
99 +impl From<x11rb::errors::ConnectError> for WindowManagerError {
100 + fn from(value: x11rb::errors::ConnectError) -> Self {
101 + Self::X11(X11Error::ConnectError(value))
102 + }
103 +}
104 +
105 +impl From<x11rb::errors::ConnectionError> for WindowManagerError {
106 + fn from(value: x11rb::errors::ConnectionError) -> Self {
107 + Self::X11(X11Error::ConnectionError(value))
108 + }
109 +}
110 +
111 +impl From<x11rb::errors::ReplyError> for WindowManagerError {
112 + fn from(value: x11rb::errors::ReplyError) -> Self {
113 + Self::X11(X11Error::ReplyError(value))
114 + }
115 +}
116 +
117 +impl From<x11rb::errors::ReplyOrIdError> for WindowManagerError {
118 + fn from(value: x11rb::errors::ReplyOrIdError) -> Self {
119 + Self::X11(X11Error::ReplyOrIdError(value))
120 + }
121 +}
122 +
123 +impl From<anyhow::Error> for WindowManagerError {
124 + fn from(value: anyhow::Error) -> Self {
125 + Self::Anyhow(value)
126 + }
127 +}
128 +
129 +// Potentiel Errors:
130 +// ::new -> x11rb::errors::ConnectError
131 +// ::new -> x11rb::errors::ReplyError
132 +// ::new -> x11rb::errors::ConnectionError
133 +// ::new -> x11rb::errors::ReplyOrIdError
134 +//
135 +// ::needs_recompile -> io::Result
39 impl WindowManager { 136 impl WindowManager {
40 - pub fn new(config: Config) -> Result<Self> { 137 + pub fn new(config: Config) -> std::result::Result<Self, WindowManagerError> {
41 let (connection, screen_number) = x11rb::connect(None)?; 138 let (connection, screen_number) = x11rb::connect(None)?;
42 let root = connection.setup().roots[screen_number].root; 139 let root = connection.setup().roots[screen_number].root;
43 let screen = connection.setup().roots[screen_number].clone(); 140 let screen = connection.setup().roots[screen_number].clone();