tony / oxwm

Branch: a8b90f5
Switch branches/tags
master
emzywastaken format files bb500f4 2025-12-12
183 lines · 5.2 KB Raw Blame History
1 use std::io;
2
3 #[derive(Debug)]
4 pub enum WmError {
5 X11(X11Error),
6 Io(io::Error),
7 Config(ConfigError),
8 Block(BlockError),
9 Autostart(String, io::Error),
10 }
11
12 #[derive(Debug)]
13 pub enum X11Error {
14 ConnectError(x11rb::errors::ConnectError),
15 ConnectionError(x11rb::errors::ConnectionError),
16 ReplyError(x11rb::errors::ReplyError),
17 ReplyOrIdError(x11rb::errors::ReplyOrIdError),
18 DisplayOpenFailed,
19 FontLoadFailed(String),
20 DrawCreateFailed,
21 }
22
23 #[derive(Debug)]
24 pub enum ConfigError {
25 LuaError(String),
26 InvalidModkey(String),
27 UnknownKey(String),
28 UnknownAction(String),
29 UnknownBlockCommand(String),
30 MissingCommandArg { command: String, field: String },
31 ValidationError(String),
32 }
33
34 #[derive(Debug)]
35 pub enum BlockError {
36 Io(io::Error),
37 ParseInt(std::num::ParseIntError),
38 MissingFile(String),
39 InvalidData(String),
40 CommandFailed(String),
41 }
42
43 impl std::fmt::Display for WmError {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 match self {
46 Self::X11(error) => write!(f, "{}", error),
47 Self::Io(error) => write!(f, "{}", error),
48 Self::Config(error) => write!(f, "{}", error),
49 Self::Block(error) => write!(f, "{}", error),
50 Self::Autostart(command, error) => write!(
51 f,
52 "Failed to spawn autostart command '{}': {}",
53 command, error
54 ),
55 }
56 }
57 }
58
59 impl std::error::Error for WmError {}
60
61 impl std::fmt::Display for X11Error {
62 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63 match self {
64 Self::ConnectError(err) => write!(f, "{}", err),
65 Self::ConnectionError(err) => write!(f, "{}", err),
66 Self::ReplyError(err) => write!(f, "{}", err),
67 Self::ReplyOrIdError(err) => write!(f, "{}", err),
68 Self::DisplayOpenFailed => write!(f, "failed to open X11 display"),
69 Self::FontLoadFailed(font_name) => write!(f, "failed to load Xft font: {}", font_name),
70 Self::DrawCreateFailed => write!(f, "failed to create XftDraw"),
71 }
72 }
73 }
74
75 impl std::error::Error for X11Error {}
76
77 impl std::fmt::Display for ConfigError {
78 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79 match self {
80 Self::LuaError(msg) => write!(f, "{}", msg),
81 Self::InvalidModkey(msg) => write!(f, "{}", msg),
82 Self::UnknownKey(msg) => write!(f, "{}", msg),
83 Self::UnknownAction(msg) => write!(f, "{}", msg),
84 Self::UnknownBlockCommand(msg) => write!(f, "{}", msg),
85 Self::MissingCommandArg { command, field } => {
86 write!(f, "{} command requires {}", command, field)
87 }
88 Self::ValidationError(msg) => write!(f, "{}", msg),
89 }
90 }
91 }
92
93 impl std::error::Error for ConfigError {}
94
95 impl std::fmt::Display for BlockError {
96 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97 match self {
98 Self::Io(err) => write!(f, "Block I/O error: {}", err),
99 Self::ParseInt(err) => write!(f, "Block parse error: {}", err),
100 Self::MissingFile(path) => write!(f, "Block missing file: {}", path),
101 Self::InvalidData(msg) => write!(f, "Block invalid data: {}", msg),
102 Self::CommandFailed(msg) => write!(f, "Block command failed: {}", msg),
103 }
104 }
105 }
106
107 impl std::error::Error for BlockError {}
108
109 impl<T: Into<X11Error>> From<T> for WmError {
110 fn from(value: T) -> Self {
111 Self::X11(value.into())
112 }
113 }
114
115 impl From<io::Error> for WmError {
116 fn from(value: io::Error) -> Self {
117 Self::Io(value)
118 }
119 }
120
121 impl From<ConfigError> for WmError {
122 fn from(value: ConfigError) -> Self {
123 Self::Config(value)
124 }
125 }
126
127 impl From<BlockError> for WmError {
128 fn from(value: BlockError) -> Self {
129 Self::Block(value)
130 }
131 }
132
133 impl From<io::Error> for BlockError {
134 fn from(value: io::Error) -> Self {
135 BlockError::Io(value)
136 }
137 }
138
139 impl From<std::num::ParseIntError> for BlockError {
140 fn from(value: std::num::ParseIntError) -> Self {
141 BlockError::ParseInt(value)
142 }
143 }
144
145 impl From<x11rb::errors::ConnectError> for X11Error {
146 fn from(value: x11rb::errors::ConnectError) -> Self {
147 X11Error::ConnectError(value)
148 }
149 }
150
151 impl From<x11rb::errors::ConnectionError> for X11Error {
152 fn from(value: x11rb::errors::ConnectionError) -> Self {
153 X11Error::ConnectionError(value)
154 }
155 }
156
157 impl From<x11rb::errors::ReplyError> for X11Error {
158 fn from(value: x11rb::errors::ReplyError) -> Self {
159 X11Error::ReplyError(value)
160 }
161 }
162
163 impl From<x11rb::errors::ReplyOrIdError> for X11Error {
164 fn from(value: x11rb::errors::ReplyOrIdError) -> Self {
165 X11Error::ReplyOrIdError(value)
166 }
167 }
168
169 impl From<mlua::Error> for ConfigError {
170 fn from(err: mlua::Error) -> Self {
171 ConfigError::LuaError(err.to_string())
172 }
173 }
174
175 pub trait LuaResultExt<T> {
176 fn lua_context(self, context: &str) -> Result<T, ConfigError>;
177 }
178
179 impl<T> LuaResultExt<T> for Result<T, mlua::Error> {
180 fn lua_context(self, context: &str) -> Result<T, ConfigError> {
181 self.map_err(|e| ConfigError::LuaError(format!("{}: {}", context, e)))
182 }
183 }