tony / oxwm

Branch: be13d76
Switch branches/tags
master
Scott DeJong Removed the use of strings in the MainError Debug implementation 19b2885 2025-12-19
212 lines · 6.3 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 pub enum MainError {
44 CouldNotCreateConfigDir(std::io::Error),
45 CouldNotWriteConfig(std::io::Error),
46 FailedCheckExist(std::io::Error),
47 FailedReadConfig(std::io::Error),
48 FailedReadConfigTemplate(ConfigError),
49 CouldNotStartWm(WmError),
50 BadRestartStatus(WmError),
51 BadConfigPath,
52 NoConfigPath,
53 InvalidArguments,
54 NoProgramName,
55 NoConfigDir,
56 }
57
58 impl std::fmt::Display for WmError {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 match self {
61 Self::X11(error) => write!(f, "{}", error),
62 Self::Io(error) => write!(f, "{}", error),
63 Self::Config(error) => write!(f, "{}", error),
64 Self::Block(error) => write!(f, "{}", error),
65 Self::Autostart(command, error) => write!(
66 f,
67 "Failed to spawn autostart command '{}': {}",
68 command, error
69 ),
70 }
71 }
72 }
73
74 impl std::fmt::Display for X11Error {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 match self {
77 Self::ConnectError(err) => write!(f, "{}", err),
78 Self::ConnectionError(err) => write!(f, "{}", err),
79 Self::ReplyError(err) => write!(f, "{}", err),
80 Self::ReplyOrIdError(err) => write!(f, "{}", err),
81 Self::DisplayOpenFailed => write!(f, "failed to open X11 display"),
82 Self::FontLoadFailed(font_name) => write!(f, "failed to load Xft font: {}", font_name),
83 Self::DrawCreateFailed => write!(f, "failed to create XftDraw"),
84 }
85 }
86 }
87
88 impl std::fmt::Display for ConfigError {
89 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90 match self {
91 Self::LuaError(msg) => write!(f, "{}", msg),
92 Self::InvalidModkey(msg) => write!(f, "{}", msg),
93 Self::UnknownKey(msg) => write!(f, "{}", msg),
94 Self::UnknownAction(msg) => write!(f, "{}", msg),
95 Self::UnknownBlockCommand(msg) => write!(f, "{}", msg),
96 Self::MissingCommandArg { command, field } => {
97 write!(f, "{} command requires {}", command, field)
98 }
99 Self::ValidationError(msg) => write!(f, "{}", msg),
100 }
101 }
102 }
103
104 impl std::fmt::Display for BlockError {
105 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106 match self {
107 Self::Io(err) => write!(f, "Block I/O error: {}", err),
108 Self::ParseInt(err) => write!(f, "Block parse error: {}", err),
109 Self::MissingFile(path) => write!(f, "Block missing file: {}", path),
110 Self::InvalidData(msg) => write!(f, "Block invalid data: {}", msg),
111 Self::CommandFailed(msg) => write!(f, "Block command failed: {}", msg),
112 }
113 }
114 }
115
116 impl<T: Into<X11Error>> From<T> for WmError {
117 fn from(value: T) -> Self {
118 Self::X11(value.into())
119 }
120 }
121
122 impl From<io::Error> for WmError {
123 fn from(value: io::Error) -> Self {
124 Self::Io(value)
125 }
126 }
127
128 impl From<ConfigError> for WmError {
129 fn from(value: ConfigError) -> Self {
130 Self::Config(value)
131 }
132 }
133
134 impl From<BlockError> for WmError {
135 fn from(value: BlockError) -> Self {
136 Self::Block(value)
137 }
138 }
139
140 impl From<io::Error> for BlockError {
141 fn from(value: io::Error) -> Self {
142 BlockError::Io(value)
143 }
144 }
145
146 impl From<std::num::ParseIntError> for BlockError {
147 fn from(value: std::num::ParseIntError) -> Self {
148 BlockError::ParseInt(value)
149 }
150 }
151
152 impl From<x11rb::errors::ConnectError> for X11Error {
153 fn from(value: x11rb::errors::ConnectError) -> Self {
154 X11Error::ConnectError(value)
155 }
156 }
157
158 impl From<x11rb::errors::ConnectionError> for X11Error {
159 fn from(value: x11rb::errors::ConnectionError) -> Self {
160 X11Error::ConnectionError(value)
161 }
162 }
163
164 impl From<x11rb::errors::ReplyError> for X11Error {
165 fn from(value: x11rb::errors::ReplyError) -> Self {
166 X11Error::ReplyError(value)
167 }
168 }
169
170 impl From<x11rb::errors::ReplyOrIdError> for X11Error {
171 fn from(value: x11rb::errors::ReplyOrIdError) -> Self {
172 X11Error::ReplyOrIdError(value)
173 }
174 }
175
176 impl From<mlua::Error> for ConfigError {
177 fn from(err: mlua::Error) -> Self {
178 ConfigError::LuaError(err.to_string())
179 }
180 }
181
182 pub trait LuaResultExt<T> {
183 fn lua_context(self, context: &str) -> Result<T, ConfigError>;
184 }
185
186 impl<T> LuaResultExt<T> for Result<T, mlua::Error> {
187 fn lua_context(self, context: &str) -> Result<T, ConfigError> {
188 self.map_err(|e| ConfigError::LuaError(format!("{}: {}", context, e)))
189 }
190 }
191
192 impl std::fmt::Debug for MainError {
193 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
194 use MainError::*;
195
196 match self {
197 CouldNotCreateConfigDir(e)
198 | CouldNotWriteConfig(e)
199 | FailedCheckExist(e)
200 | FailedReadConfig(e) => {
201 write!(f, "{e}")
202 }
203 FailedReadConfigTemplate(e) => write!(f, "{e}"),
204 CouldNotStartWm(e) | BadRestartStatus(e) => write!(f, "{e}"),
205 BadConfigPath => write!(f, "Given config path does not exist"),
206 NoConfigPath => write!(f, "The --config switch requires a path value"),
207 InvalidArguments => write!(f, "The arguments given are invalid try --help"),
208 NoProgramName => write!(f, "Could not get the program name from the environment"),
209 NoConfigDir => write!(f, "Could not get the config dir"),
210 }
211 }
212 }