tony / oxwm

updated Browse files
tonybtw committed 2025-11-08 parent 1c05ced commit daf7a1c8eaec4d072a852b103ab81ef7c4612c36
Showing 1 changed file with 49 additions and 41 deletions. UnifiedSplit
+49 −41 src/config/lua.rs
@@ -10,7 +10,8 @@ use x11rb::protocol::xproto::KeyButMask;
10 10 pub fn parse_lua_config(input: &str) -> Result<crate::Config, ConfigError> {
11 11 let lua = Lua::new();
12 12
13 - let config: Table = lua.load(input)
13 + let config: Table = lua
14 + .load(input)
14 15 .eval()
15 16 .map_err(|e| ConfigError::LuaError(format!("Failed to execute Lua config: {}", e)))?;
16 17 let border_width: u32 = get_table_field(&config, "border_width")?;
@@ -71,9 +72,9 @@ where
71 72 }
72 73
73 74 fn parse_color(table: &Table, field: &str) -> Result<u32, ConfigError> {
74 - let value: Value = table
75 - .get(field)
76 - .map_err(|e| ConfigError::LuaError(format!("Failed to get color field '{}': {}", field, e)))?;
75 + let value: Value = table.get(field).map_err(|e| {
76 + ConfigError::LuaError(format!("Failed to get color field '{}': {}", field, e))
77 + })?;
77 78
78 79 match value {
79 80 Value::String(s) => {
@@ -127,9 +128,10 @@ fn parse_tags(config: &Table) -> Result<Vec<String>, ConfigError> {
127 128 let tags_table: Table = get_table_field(config, "tags")?;
128 129 let mut tags = Vec::new();
129 130
130 - for i in 1..=tags_table.len().map_err(|e| {
131 - ConfigError::LuaError(format!("Failed to get tags length: {}", e))
132 - })? {
131 + for i in 1..=tags_table
132 + .len()
133 + .map_err(|e| ConfigError::LuaError(format!("Failed to get tags length: {}", e)))?
134 + {
133 135 let tag: String = tags_table.get(i).map_err(|e| {
134 136 ConfigError::LuaError(format!("Failed to get tag at index {}: {}", i, e))
135 137 })?;
@@ -150,7 +152,10 @@ fn parse_layout_symbols(config: &Table) -> Result<Vec<LayoutSymbolOverride>, Con
150 152 ConfigError::LuaError(format!("Failed to get layout_symbols length: {}", e))
151 153 })? {
152 154 let entry: Table = layout_symbols_table.get(i).map_err(|e| {
153 - ConfigError::LuaError(format!("Failed to get layout_symbol at index {}: {}", i, e))
155 + ConfigError::LuaError(format!(
156 + "Failed to get layout_symbol at index {}: {}",
157 + i, e
158 + ))
154 159 })?;
155 160
156 161 let name: String = get_table_field(&entry, "name")?;
@@ -161,20 +166,18 @@ fn parse_layout_symbols(config: &Table) -> Result<Vec<LayoutSymbolOverride>, Con
161 166
162 167 Ok(layout_symbols)
163 168 }
164 - Err(_) => Ok(Vec::new()), // layout_symbols is optional
169 + Err(_) => Ok(Vec::new()),
165 170 }
166 171 }
167 172
168 -fn parse_keybindings(
169 - config: &Table,
170 - modkey: KeyButMask,
171 -) -> Result<Vec<KeyBinding>, ConfigError> {
173 +fn parse_keybindings(config: &Table, modkey: KeyButMask) -> Result<Vec<KeyBinding>, ConfigError> {
172 174 let keybindings_table: Table = get_table_field(config, "keybindings")?;
173 175 let mut keybindings = Vec::new();
174 176
175 - for i in 1..=keybindings_table.len().map_err(|e| {
176 - ConfigError::LuaError(format!("Failed to get keybindings length: {}", e))
177 - })? {
177 + for i in 1..=keybindings_table
178 + .len()
179 + .map_err(|e| ConfigError::LuaError(format!("Failed to get keybindings length: {}", e)))?
180 + {
178 181 let kb_table: Table = keybindings_table.get(i).map_err(|e| {
179 182 ConfigError::LuaError(format!("Failed to get keybinding at index {}: {}", i, e))
180 183 })?;
@@ -189,19 +192,15 @@ fn parse_keybindings(
189 192 Ok(keybindings)
190 193 }
191 194
192 -fn parse_keypress_list(
193 - kb_table: &Table,
194 - modkey: KeyButMask,
195 -) -> Result<Vec<KeyPress>, ConfigError> {
196 - // Check if 'keys' field exists (for keychords)
195 +fn parse_keypress_list(kb_table: &Table, modkey: KeyButMask) -> Result<Vec<KeyPress>, ConfigError> {
197 196 let keys_result: Result<Table, _> = kb_table.get("keys");
198 197
199 198 if let Ok(keys_table) = keys_result {
200 - // Parse keychord
201 199 let mut keys = Vec::new();
202 - for i in 1..=keys_table.len().map_err(|e| {
203 - ConfigError::LuaError(format!("Failed to get keys length: {}", e))
204 - })? {
200 + for i in 1..=keys_table
201 + .len()
202 + .map_err(|e| ConfigError::LuaError(format!("Failed to get keys length: {}", e)))?
203 + {
205 204 let key_entry: Table = keys_table.get(i).map_err(|e| {
206 205 ConfigError::LuaError(format!("Failed to get key at index {}: {}", i, e))
207 206 })?;
@@ -213,7 +212,6 @@ fn parse_keypress_list(
213 212 }
214 213 Ok(keys)
215 214 } else {
216 - // Parse single key (old format)
217 215 let modifiers = parse_modifiers(kb_table, "modifiers", modkey)?;
218 216 let keysym = parse_keysym(kb_table, "key")?;
219 217
@@ -229,9 +227,10 @@ fn parse_modifiers(
229 227 let mods_table: Table = get_table_field(table, field)?;
230 228 let mut modifiers = Vec::new();
231 229
232 - for i in 1..=mods_table.len().map_err(|e| {
233 - ConfigError::LuaError(format!("Failed to get modifiers length: {}", e))
234 - })? {
230 + for i in 1..=mods_table
231 + .len()
232 + .map_err(|e| ConfigError::LuaError(format!("Failed to get modifiers length: {}", e)))?
233 + {
235 234 let mod_str: String = mods_table.get(i).map_err(|e| {
236 235 ConfigError::LuaError(format!("Failed to get modifier at index {}: {}", i, e))
237 236 })?;
@@ -387,12 +386,14 @@ fn parse_arg(kb_table: &Table) -> Result<Arg, ConfigError> {
387 386 Ok(Value::Number(n)) => Ok(Arg::Int(n as i32)),
388 387 Ok(Value::Table(t)) => {
389 388 let mut arr = Vec::new();
390 - for i in 1..=t
391 - .len()
392 - .map_err(|e| ConfigError::LuaError(format!("Failed to get arg array length: {}", e)))?
393 - {
389 + for i in 1..=t.len().map_err(|e| {
390 + ConfigError::LuaError(format!("Failed to get arg array length: {}", e))
391 + })? {
394 392 let item: String = t.get(i).map_err(|e| {
395 - ConfigError::LuaError(format!("Failed to get arg array item at index {}: {}", i, e))
393 + ConfigError::LuaError(format!(
394 + "Failed to get arg array item at index {}: {}",
395 + i, e
396 + ))
396 397 })?;
397 398 arr.push(item);
398 399 }
@@ -408,9 +409,10 @@ fn parse_status_blocks(config: &Table) -> Result<Vec<BlockConfig>, ConfigError>
408 409 let blocks_table: Table = get_table_field(config, "status_blocks")?;
409 410 let mut blocks = Vec::new();
410 411
411 - for i in 1..=blocks_table.len().map_err(|e| {
412 - ConfigError::LuaError(format!("Failed to get status_blocks length: {}", e))
413 - })? {
412 + for i in 1..=blocks_table
413 + .len()
414 + .map_err(|e| ConfigError::LuaError(format!("Failed to get status_blocks length: {}", e)))?
415 + {
414 416 let block_table: Table = blocks_table.get(i).map_err(|e| {
415 417 ConfigError::LuaError(format!("Failed to get status_block at index {}: {}", i, e))
416 418 })?;
@@ -418,7 +420,6 @@ fn parse_status_blocks(config: &Table) -> Result<Vec<BlockConfig>, ConfigError>
418 420 let format: String = get_table_field(&block_table, "format")?;
419 421 let command_str: String = get_table_field(&block_table, "command")?;
420 422
421 - // Parse interval_secs - handle both integer and number types
422 423 let interval_secs: u64 = {
423 424 let value: Value = block_table.get("interval_secs").map_err(|e| {
424 425 ConfigError::LuaError(format!("Failed to get interval_secs: {}", e))
@@ -426,7 +427,11 @@ fn parse_status_blocks(config: &Table) -> Result<Vec<BlockConfig>, ConfigError>
426 427 match value {
427 428 Value::Integer(i) => i as u64,
428 429 Value::Number(n) => n as u64,
429 - _ => return Err(ConfigError::LuaError("interval_secs must be a number".to_string())),
430 + _ => {
431 + return Err(ConfigError::LuaError(
432 + "interval_secs must be a number".to_string(),
433 + ));
434 + }
430 435 }
431 436 };
432 437
@@ -500,14 +505,17 @@ fn parse_autostart(config: &Table) -> Result<Vec<String>, ConfigError> {
500 505 ConfigError::LuaError(format!("Failed to get autostart length: {}", e))
501 506 })? {
502 507 let cmd: String = autostart_table.get(i).map_err(|e| {
503 - ConfigError::LuaError(format!("Failed to get autostart command at index {}: {}", i, e))
508 + ConfigError::LuaError(format!(
509 + "Failed to get autostart command at index {}: {}",
510 + i, e
511 + ))
504 512 })?;
505 513 autostart.push(cmd);
506 514 }
507 515
508 516 Ok(autostart)
509 517 }
510 - Err(_) => Ok(Vec::new()), // autostart is optional
518 + Err(_) => Ok(Vec::new()),
511 519 }
512 520 }
513 521