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 use serde::Deserialize; 6 use serde::Deserialize;
7 +use x11rb::protocol::xproto::{KeyButMask, Keycode};
3 8
4 -pub fn parse_config(input: &str) -> Result<crate::Config> { 9 +pub fn parse_config(input: &str) -> Result<crate::Config, ConfigError> {
5 - let config_data: ConfigData = ron::from_str(input).map_err(|e| { 10 + let config_data: ConfigData = ron::from_str(input)?;
6 - eprintln!("RON Parse Error Details: {}", e);
7 - anyhow::anyhow!("Failed to parse RON config: {}", e)
8 - })?;
9 -
10 config_data_to_config(config_data) 11 config_data_to_config(config_data)
11 } 12 }
12 13
@@ -86,22 +87,16 @@ struct ColorSchemeData {
86 underline: u32, 87 underline: u32,
87 } 88 }
88 89
89 -fn config_data_to_config(data: ConfigData) -> Result<crate::Config> { 90 +fn config_data_to_config(data: ConfigData) -> Result<crate::Config, ConfigError> {
90 - use crate::keyboard::handlers::Key;
91 - use crate::keyboard::{Arg, KeyAction};
92 - use x11rb::protocol::xproto::KeyButMask;
93 -
94 - // Parse modkey
95 let modkey = parse_modkey(&data.modkey)?; 91 let modkey = parse_modkey(&data.modkey)?;
96 92
97 - // Parse keybindings
98 let mut keybindings = Vec::new(); 93 let mut keybindings = Vec::new();
99 for kb_data in data.keybindings { 94 for kb_data in data.keybindings {
100 let modifiers = kb_data 95 let modifiers = kb_data
101 .modifiers 96 .modifiers
102 .iter() 97 .iter()
103 .map(|s| parse_modkey(s)) 98 .map(|s| parse_modkey(s))
104 - .collect::<Result<Vec<_>>>()?; 99 + .collect::<Result<Vec<_>, _>>()?;
105 100
106 let modifiers_static: &'static [KeyButMask] = Box::leak(modifiers.into_boxed_slice()); 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 keybindings.push(Key::new(modifiers_static, key, action, arg)); 107 keybindings.push(Key::new(modifiers_static, key, action, arg));
113 } 108 }
114 109
115 - // Parse status blocks
116 let mut status_blocks = Vec::new(); 110 let mut status_blocks = Vec::new();
117 for block_data in data.status_blocks { 111 for block_data in data.status_blocks {
118 - use crate::bar::{BlockCommand, BlockConfig};
119 -
120 let format_static: &'static str = Box::leak(block_data.format.into_boxed_str()); 112 let format_static: &'static str = Box::leak(block_data.format.into_boxed_str());
121 113
122 let command = match block_data.command.as_str() { 114 let command = match block_data.command.as_str() {
123 "DateTime" => { 115 "DateTime" => {
124 let fmt = block_data 116 let fmt = block_data
125 .command_arg 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 let fmt_static: &'static str = Box::leak(fmt.into_boxed_str()); 122 let fmt_static: &'static str = Box::leak(fmt.into_boxed_str());
128 BlockCommand::DateTime(fmt_static) 123 BlockCommand::DateTime(fmt_static)
129 } 124 }
130 "Shell" => { 125 "Shell" => {
131 let cmd = block_data 126 let cmd = block_data
132 .command_arg 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 let cmd_static: &'static str = Box::leak(cmd.into_boxed_str()); 132 let cmd_static: &'static str = Box::leak(cmd.into_boxed_str());
135 BlockCommand::Shell(cmd_static) 133 BlockCommand::Shell(cmd_static)
136 } 134 }
@@ -141,16 +139,20 @@ fn config_data_to_config(data: ConfigData) -> Result<crate::Config> {
141 BlockCommand::Static(text_static) 139 BlockCommand::Static(text_static)
142 } 140 }
143 "Battery" => { 141 "Battery" => {
144 - let formats = block_data 142 + let formats =
145 - .battery_formats 143 + block_data
146 - .context("Battery command requires battery_formats")?; 144 + .battery_formats
145 + .ok_or_else(|| ConfigError::MissingCommandArg {
146 + command: "Battery".to_string(),
147 + field: "battery_formats".to_string(),
148 + })?;
147 BlockCommand::Battery { 149 BlockCommand::Battery {
148 format_charging: Box::leak(formats.charging.into_boxed_str()), 150 format_charging: Box::leak(formats.charging.into_boxed_str()),
149 format_discharging: Box::leak(formats.discharging.into_boxed_str()), 151 format_discharging: Box::leak(formats.discharging.into_boxed_str()),
150 format_full: Box::leak(formats.full.into_boxed_str()), 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 status_blocks.push(BlockConfig { 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> { 200 +fn parse_modkey(s: &str) -> Result<KeyButMask, ConfigError> {
199 - use x11rb::protocol::xproto::KeyButMask;
200 -
201 match s { 201 match s {
202 "Mod1" => Ok(KeyButMask::MOD1), 202 "Mod1" => Ok(KeyButMask::MOD1),
203 "Mod2" => Ok(KeyButMask::MOD2), 203 "Mod2" => Ok(KeyButMask::MOD2),
@@ -206,13 +206,11 @@ fn parse_modkey(s: &str) -> Result<x11rb::protocol::xproto::KeyButMask> {
206 "Mod5" => Ok(KeyButMask::MOD5), 206 "Mod5" => Ok(KeyButMask::MOD5),
207 "Shift" => Ok(KeyButMask::SHIFT), 207 "Shift" => Ok(KeyButMask::SHIFT),
208 "Control" => Ok(KeyButMask::CONTROL), 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> { 213 +fn string_to_keycode(s: &str) -> Result<Keycode, ConfigError> {
214 - use crate::keyboard::keycodes;
215 -
216 match s.to_lowercase().as_str() { 214 match s.to_lowercase().as_str() {
217 "return" => Ok(keycodes::RETURN), 215 "return" => Ok(keycodes::RETURN),
218 "q" => Ok(keycodes::Q), 216 "q" => Ok(keycodes::Q),
@@ -222,7 +220,6 @@ fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
222 "backspace" => Ok(keycodes::BACKSPACE), 220 "backspace" => Ok(keycodes::BACKSPACE),
223 "delete" => Ok(keycodes::DELETE), 221 "delete" => Ok(keycodes::DELETE),
224 222
225 - // Function keys
226 "f1" => Ok(keycodes::F1), 223 "f1" => Ok(keycodes::F1),
227 "f2" => Ok(keycodes::F2), 224 "f2" => Ok(keycodes::F2),
228 "f3" => Ok(keycodes::F3), 225 "f3" => Ok(keycodes::F3),
@@ -236,7 +233,6 @@ fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
236 "f11" => Ok(keycodes::F11), 233 "f11" => Ok(keycodes::F11),
237 "f12" => Ok(keycodes::F12), 234 "f12" => Ok(keycodes::F12),
238 235
239 - // Letters
240 "a" => Ok(keycodes::A), 236 "a" => Ok(keycodes::A),
241 "b" => Ok(keycodes::B), 237 "b" => Ok(keycodes::B),
242 "c" => Ok(keycodes::C), 238 "c" => Ok(keycodes::C),
@@ -263,7 +259,6 @@ fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
263 "y" => Ok(keycodes::Y), 259 "y" => Ok(keycodes::Y),
264 "z" => Ok(keycodes::Z), 260 "z" => Ok(keycodes::Z),
265 261
266 - // Numbers
267 "0" => Ok(keycodes::KEY_0), 262 "0" => Ok(keycodes::KEY_0),
268 "1" => Ok(keycodes::KEY_1), 263 "1" => Ok(keycodes::KEY_1),
269 "2" => Ok(keycodes::KEY_2), 264 "2" => Ok(keycodes::KEY_2),
@@ -275,7 +270,6 @@ fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
275 "8" => Ok(keycodes::KEY_8), 270 "8" => Ok(keycodes::KEY_8),
276 "9" => Ok(keycodes::KEY_9), 271 "9" => Ok(keycodes::KEY_9),
277 272
278 - // Arrows
279 "left" => Ok(keycodes::LEFT), 273 "left" => Ok(keycodes::LEFT),
280 "right" => Ok(keycodes::RIGHT), 274 "right" => Ok(keycodes::RIGHT),
281 "up" => Ok(keycodes::UP), 275 "up" => Ok(keycodes::UP),
@@ -286,7 +280,6 @@ fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
286 "pagedown" => Ok(keycodes::PAGE_DOWN), 280 "pagedown" => Ok(keycodes::PAGE_DOWN),
287 "insert" => Ok(keycodes::INSERT), 281 "insert" => Ok(keycodes::INSERT),
288 282
289 - // Symbols
290 "minus" | "-" => Ok(keycodes::MINUS), 283 "minus" | "-" => Ok(keycodes::MINUS),
291 "equal" | "=" => Ok(keycodes::EQUAL), 284 "equal" | "=" => Ok(keycodes::EQUAL),
292 "bracketleft" | "[" => Ok(keycodes::LEFT_BRACKET), 285 "bracketleft" | "[" => Ok(keycodes::LEFT_BRACKET),
@@ -299,13 +292,11 @@ fn string_to_keycode(s: &str) -> Result<x11rb::protocol::xproto::Keycode> {
299 "period" | "." => Ok(keycodes::PERIOD), 292 "period" | "." => Ok(keycodes::PERIOD),
300 "slash" | "/" => Ok(keycodes::SLASH), 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> { 299 +fn parse_key_action(s: &str) -> Result<crate::keyboard::KeyAction, ConfigError> {
307 - use crate::keyboard::KeyAction;
308 -
309 match s { 300 match s {
310 "Spawn" => Ok(KeyAction::Spawn), 301 "Spawn" => Ok(KeyAction::Spawn),
311 "KillClient" => Ok(KeyAction::KillClient), 302 "KillClient" => Ok(KeyAction::KillClient),
@@ -317,13 +308,11 @@ fn parse_key_action(s: &str) -> Result<crate::keyboard::KeyAction> {
317 "ToggleGaps" => Ok(KeyAction::ToggleGaps), 308 "ToggleGaps" => Ok(KeyAction::ToggleGaps),
318 "ToggleFullScreen" => Ok(KeyAction::ToggleFullScreen), 309 "ToggleFullScreen" => Ok(KeyAction::ToggleFullScreen),
319 "ToggleFloating" => Ok(KeyAction::ToggleFloating), 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> { 315 +fn arg_data_to_arg(data: ArgData) -> Result<crate::keyboard::Arg, ConfigError> {
325 - use crate::keyboard::Arg;
326 -
327 match data { 316 match data {
328 ArgData::None => Ok(Arg::None), 317 ArgData::None => Ok(Arg::None),
329 ArgData::String(s) => { 318 ArgData::String(s) => {
+41 −0 src/errors.rs
@@ -3,6 +3,7 @@ pub enum WmError {
3 X11(X11Error), 3 X11(X11Error),
4 Io(std::io::Error), 4 Io(std::io::Error),
5 Anyhow(anyhow::Error), 5 Anyhow(anyhow::Error),
6 + Config(ConfigError),
6 } 7 }
7 8
8 #[derive(Debug)] 9 #[derive(Debug)]
@@ -16,12 +17,23 @@ pub enum X11Error {
16 DrawCreateFailed, 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 impl std::fmt::Display for WmError { 30 impl std::fmt::Display for WmError {
20 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21 match self { 32 match self {
22 Self::X11(error) => write!(f, "{}", error), 33 Self::X11(error) => write!(f, "{}", error),
23 Self::Io(error) => write!(f, "{}", error), 34 Self::Io(error) => write!(f, "{}", error),
24 Self::Anyhow(error) => write!(f, "{}", error), 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 impl std::error::Error for X11Error {} 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 impl<T: Into<X11Error>> From<T> for WmError { 76 impl<T: Into<X11Error>> From<T> for WmError {
48 fn from(value: T) -> Self { 77 fn from(value: T) -> Self {
49 Self::X11(value.into()) 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 impl From<x11rb::errors::ConnectError> for X11Error { 106 impl From<x11rb::errors::ConnectError> for X11Error {
66 fn from(value: x11rb::errors::ConnectError) -> Self { 107 fn from(value: x11rb::errors::ConnectError) -> Self {
67 X11Error::ConnectError(value) 108 X11Error::ConnectError(value)