tony / oxwm

Added native errors for config parser, cleaned up imports. Browse files
tonybtw committed 2025-10-15 parent 52cdcac commit ed311f0d2fab6c9e0743ba65d727251813577b4b
Showing 2 changed files with 74 additions and 44 deletions. UnifiedSplit
+33 −44 src/config/mod.rs
@@ -1,12 +1,13 @@
1 -use anyhow::{Context, Result, bail};
1 +use crate::bar::{BlockCommand, BlockConfig};
2 +use crate::errors::ConfigError;
3 +use crate::keyboard::handlers::Key;
4 +use crate::keyboard::keycodes;
5 +use crate::keyboard::{Arg, KeyAction};
2 6 use serde::Deserialize;
7 +use x11rb::protocol::xproto::{KeyButMask, Keycode};
3 8
4 -pub fn parse_config(input: &str) -> Result<crate::Config> {
5 - let config_data: ConfigData = ron::from_str(input).map_err(|e| {
6 - eprintln!("RON Parse Error Details: {}", e);
7 - anyhow::anyhow!("Failed to parse RON config: {}", e)
8 - })?;
9 -
9 +pub fn parse_config(input: &str) -> Result<crate::Config, ConfigError> {
10 + let config_data: ConfigData = ron::from_str(input)?;
10 11 config_data_to_config(config_data)
11 12 }
12 13
@@ -86,22 +87,16 @@ struct ColorSchemeData {
86 87 underline: u32,
87 88 }
88 89
89 -fn config_data_to_config(data: ConfigData) -> Result<crate::Config> {
90 - use crate::keyboard::handlers::Key;
91 - use crate::keyboard::{Arg, KeyAction};
92 - use x11rb::protocol::xproto::KeyButMask;
93 -
94 - // Parse modkey
90 +fn config_data_to_config(data: ConfigData) -> Result<crate::Config, ConfigError> {
95 91 let modkey = parse_modkey(&data.modkey)?;
96 92
97 - // Parse keybindings
98 93 let mut keybindings = Vec::new();
99 94 for kb_data in data.keybindings {
100 95 let modifiers = kb_data
101 96 .modifiers
102 97 .iter()
103 98 .map(|s| parse_modkey(s))
104 - .collect::<Result<Vec<_>>>()?;
99 + .collect::<Result<Vec<_>, _>>()?;
105 100
106 101 let modifiers_static: &'static [KeyButMask] = Box::leak(modifiers.into_boxed_slice());
107 102
@@ -112,25 +107,28 @@ fn config_data_to_config(data: ConfigData) -> Result<crate::Config> {
112 107 keybindings.push(Key::new(modifiers_static, key, action, arg));
113 108 }
114 109
115 - // Parse status blocks
116 110 let mut status_blocks = Vec::new();
117 111 for block_data in data.status_blocks {
118 - use crate::bar::{BlockCommand, BlockConfig};
119 -
120 112 let format_static: &'static str = Box::leak(block_data.format.into_boxed_str());
121 113
122 114 let command = match block_data.command.as_str() {
123 115 "DateTime" => {
124 116 let fmt = block_data
125 117 .command_arg
126 - .context("DateTime command requires command_arg")?;
118 + .ok_or_else(|| ConfigError::MissingCommandArg {
119 + command: "DateTime".to_string(),
120 + field: "command_arg".to_string(),
121 + })?;
127 122 let fmt_static: &'static str = Box::leak(fmt.into_boxed_str());
128 123 BlockCommand::DateTime(fmt_static)
129 124 }
130 125 "Shell" => {
131 126 let cmd = block_data
132 127 .command_arg
133 - .context("Shell command requires command_arg")?;
128 + .ok_or_else(|| ConfigError::MissingCommandArg {
129 + command: "Shell".to_string(),
130 + field: "command_arg".to_string(),
131 + })?;
134 132 let cmd_static: &'static str = Box::leak(cmd.into_boxed_str());
135 133 BlockCommand::Shell(cmd_static)
136 134 }
@@ -141,16 +139,20 @@ fn config_data_to_config(data: ConfigData) -> Result<crate::Config> {
141 139 BlockCommand::Static(text_static)
142 140 }
143 141 "Battery" => {
144 - let formats = block_data
145 - .battery_formats
146 - .context("Battery command requires battery_formats")?;
142 + let formats =
143 + block_data
144 + .battery_formats
145 + .ok_or_else(|| ConfigError::MissingCommandArg {
146 + command: "Battery".to_string(),
147 + field: "battery_formats".to_string(),
148 + })?;
147 149 BlockCommand::Battery {
148 150 format_charging: Box::leak(formats.charging.into_boxed_str()),
149 151 format_discharging: Box::leak(formats.discharging.into_boxed_str()),
150 152 format_full: Box::leak(formats.full.into_boxed_str()),
151 153 }
152 154 }
153 - _ => bail!("Unknown block command: {}", block_data.command),
155 + _ => return Err(ConfigError::UnknownBlockCommand(block_data.command)),
154 156 };
155 157
156 158 status_blocks.push(BlockConfig {
@@ -195,9 +197,7 @@ fn config_data_to_config(data: ConfigData) -> Result<crate::Config> {
195 197 })
196 198 }
197 199
198 -fn parse_modkey(s: &str) -> Result<x11rb::protocol::xproto::KeyButMask> {
199 - use x11rb::protocol::xproto::KeyButMask;
200 -
200 +fn parse_modkey(s: &str) -> Result<KeyButMask, ConfigError> {
201 201 match s {
202 202 "Mod1" => Ok(KeyButMask::MOD1),
203 203 "Mod2" => Ok(KeyButMask::MOD2),
@@ -206,13 +206,11 @@ fn parse_modkey(s: &str) -> Result<x11rb::protocol::xproto::KeyButMask> {
206 206 "Mod5" => Ok(KeyButMask::MOD5),
207 207 "Shift" => Ok(KeyButMask::SHIFT),
208 208 "Control" => Ok(KeyButMask::CONTROL),
209 - _ => bail!("Invalid modkey: {}", s),
209 + _ => Err(ConfigError::InvalidModkey(s.to_string())),
210 210 }
211 211 }
212 212
213 -fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
214 - use crate::keyboard::keycodes;
215 -
213 +fn string_to_keycode(s: &str) -> Result<Keycode, ConfigError> {
216 214 match s.to_lowercase().as_str() {
217 215 "return" => Ok(keycodes::RETURN),
218 216 "q" => Ok(keycodes::Q),
@@ -222,7 +220,6 @@ fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
222 220 "backspace" => Ok(keycodes::BACKSPACE),
223 221 "delete" => Ok(keycodes::DELETE),
224 222
225 - // Function keys
226 223 "f1" => Ok(keycodes::F1),
227 224 "f2" => Ok(keycodes::F2),
228 225 "f3" => Ok(keycodes::F3),
@@ -236,7 +233,6 @@ fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
236 233 "f11" => Ok(keycodes::F11),
237 234 "f12" => Ok(keycodes::F12),
238 235
239 - // Letters
240 236 "a" => Ok(keycodes::A),
241 237 "b" => Ok(keycodes::B),
242 238 "c" => Ok(keycodes::C),
@@ -263,7 +259,6 @@ fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
263 259 "y" => Ok(keycodes::Y),
264 260 "z" => Ok(keycodes::Z),
265 261
266 - // Numbers
267 262 "0" => Ok(keycodes::KEY_0),
268 263 "1" => Ok(keycodes::KEY_1),
269 264 "2" => Ok(keycodes::KEY_2),
@@ -275,7 +270,6 @@ fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
275 270 "8" => Ok(keycodes::KEY_8),
276 271 "9" => Ok(keycodes::KEY_9),
277 272
278 - // Arrows
279 273 "left" => Ok(keycodes::LEFT),
280 274 "right" => Ok(keycodes::RIGHT),
281 275 "up" => Ok(keycodes::UP),
@@ -286,7 +280,6 @@ fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
286 280 "pagedown" => Ok(keycodes::PAGE_DOWN),
287 281 "insert" => Ok(keycodes::INSERT),
288 282
289 - // Symbols
290 283 "minus" | "-" => Ok(keycodes::MINUS),
291 284 "equal" | "=" => Ok(keycodes::EQUAL),
292 285 "bracketleft" | "[" => Ok(keycodes::LEFT_BRACKET),
@@ -299,13 +292,11 @@ fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
299 292 "period" | "." => Ok(keycodes::PERIOD),
300 293 "slash" | "/" => Ok(keycodes::SLASH),
301 294
302 - _ => bail!("Unknown key: {}", s),
295 + _ => Err(ConfigError::UnknownKey(s.to_string())),
303 296 }
304 297 }
305 298
306 -fn parse_key_action(s: &str) -> Result<crate::keyboard::KeyAction> {
307 - use crate::keyboard::KeyAction;
308 -
299 +fn parse_key_action(s: &str) -> Result<crate::keyboard::KeyAction, ConfigError> {
309 300 match s {
310 301 "Spawn" => Ok(KeyAction::Spawn),
311 302 "KillClient" => Ok(KeyAction::KillClient),
@@ -317,13 +308,11 @@ fn parse_key_action(s: &str) -> Result<crate::keyboard::KeyAction> {
317 308 "ToggleGaps" => Ok(KeyAction::ToggleGaps),
318 309 "ToggleFullScreen" => Ok(KeyAction::ToggleFullScreen),
319 310 "ToggleFloating" => Ok(KeyAction::ToggleFloating),
320 - _ => bail!("Unknown action: {}", s),
311 + _ => Err(ConfigError::UnknownAction(s.to_string())),
321 312 }
322 313 }
323 314
324 -fn arg_data_to_arg(data: ArgData) -> Result<crate::keyboard::Arg> {
325 - use crate::keyboard::Arg;
326 -
315 +fn arg_data_to_arg(data: ArgData) -> Result<crate::keyboard::Arg, ConfigError> {
327 316 match data {
328 317 ArgData::None => Ok(Arg::None),
329 318 ArgData::String(s) => {
+41 −0 src/errors.rs
@@ -3,6 +3,7 @@ pub enum WmError {
3 3 X11(X11Error),
4 4 Io(std::io::Error),
5 5 Anyhow(anyhow::Error),
6 + Config(ConfigError),
6 7 }
7 8
8 9 #[derive(Debug)]
@@ -16,12 +17,23 @@ pub enum X11Error {
16 17 DrawCreateFailed,
17 18 }
18 19
20 +#[derive(Debug)]
21 +pub enum ConfigError {
22 + ParseError(ron::error::SpannedError),
23 + InvalidModkey(String),
24 + UnknownKey(String),
25 + UnknownAction(String),
26 + UnknownBlockCommand(String),
27 + MissingCommandArg { command: String, field: String },
28 +}
29 +
19 30 impl std::fmt::Display for WmError {
20 31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21 32 match self {
22 33 Self::X11(error) => write!(f, "{}", error),
23 34 Self::Io(error) => write!(f, "{}", error),
24 35 Self::Anyhow(error) => write!(f, "{}", error),
36 + Self::Config(error) => write!(f, "{}", error),
25 37 }
26 38 }
27 39 }
@@ -44,6 +56,23 @@ impl std::fmt::Display for X11Error {
44 56
45 57 impl std::error::Error for X11Error {}
46 58
59 +impl std::fmt::Display for ConfigError {
60 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 + match self {
62 + Self::ParseError(err) => write!(f, "Failed to parse RON config: {}", err),
63 + Self::InvalidModkey(key) => write!(f, "Invalid modkey: {}", key),
64 + Self::UnknownKey(key) => write!(f, "Unknown key: {}", key),
65 + Self::UnknownAction(action) => write!(f, "Unknown action: {}", action),
66 + Self::UnknownBlockCommand(cmd) => write!(f, "Unknown block command: {}", cmd),
67 + Self::MissingCommandArg { command, field } => {
68 + write!(f, "{} command requires {}", command, field)
69 + }
70 + }
71 + }
72 +}
73 +
74 +impl std::error::Error for ConfigError {}
75 +
47 76 impl<T: Into<X11Error>> From<T> for WmError {
48 77 fn from(value: T) -> Self {
49 78 Self::X11(value.into())
@@ -62,6 +91,18 @@ impl From<anyhow::Error> for WmError {
62 91 }
63 92 }
64 93
94 +impl From<ConfigError> for WmError {
95 + fn from(value: ConfigError) -> Self {
96 + Self::Config(value)
97 + }
98 +}
99 +
100 +impl From<ron::error::SpannedError> for ConfigError {
101 + fn from(value: ron::error::SpannedError) -> Self {
102 + ConfigError::ParseError(value)
103 + }
104 +}
105 +
65 106 impl From<x11rb::errors::ConnectError> for X11Error {
66 107 fn from(value: x11rb::errors::ConnectError) -> Self {
67 108 X11Error::ConnectError(value)