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 pub fn parse_lua_config(input: &str) -> Result<crate::Config, ConfigError> { 10 pub fn parse_lua_config(input: &str) -> Result<crate::Config, ConfigError> {
11 let lua = Lua::new(); 11 let lua = Lua::new();
12 12
13 - let config: Table = lua.load(input) 13 + let config: Table = lua
14 + .load(input)
14 .eval() 15 .eval()
15 .map_err(|e| ConfigError::LuaError(format!("Failed to execute Lua config: {}", e)))?; 16 .map_err(|e| ConfigError::LuaError(format!("Failed to execute Lua config: {}", e)))?;
16 let border_width: u32 = get_table_field(&config, "border_width")?; 17 let border_width: u32 = get_table_field(&config, "border_width")?;
@@ -71,9 +72,9 @@ where
71 } 72 }
72 73
73 fn parse_color(table: &Table, field: &str) -> Result<u32, ConfigError> { 74 fn parse_color(table: &Table, field: &str) -> Result<u32, ConfigError> {
74 - let value: Value = table 75 + let value: Value = table.get(field).map_err(|e| {
75 - .get(field) 76 + ConfigError::LuaError(format!("Failed to get color field '{}': {}", field, e))
76 - .map_err(|e| ConfigError::LuaError(format!("Failed to get color field '{}': {}", field, e)))?; 77 + })?;
77 78
78 match value { 79 match value {
79 Value::String(s) => { 80 Value::String(s) => {
@@ -127,9 +128,10 @@ fn parse_tags(config: &Table) -> Result<Vec<String>, ConfigError> {
127 let tags_table: Table = get_table_field(config, "tags")?; 128 let tags_table: Table = get_table_field(config, "tags")?;
128 let mut tags = Vec::new(); 129 let mut tags = Vec::new();
129 130
130 - for i in 1..=tags_table.len().map_err(|e| { 131 + for i in 1..=tags_table
131 - ConfigError::LuaError(format!("Failed to get tags length: {}", e)) 132 + .len()
132 - })? { 133 + .map_err(|e| ConfigError::LuaError(format!("Failed to get tags length: {}", e)))?
134 + {
133 let tag: String = tags_table.get(i).map_err(|e| { 135 let tag: String = tags_table.get(i).map_err(|e| {
134 ConfigError::LuaError(format!("Failed to get tag at index {}: {}", i, e)) 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 ConfigError::LuaError(format!("Failed to get layout_symbols length: {}", e)) 152 ConfigError::LuaError(format!("Failed to get layout_symbols length: {}", e))
151 })? { 153 })? {
152 let entry: Table = layout_symbols_table.get(i).map_err(|e| { 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 let name: String = get_table_field(&entry, "name")?; 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 Ok(layout_symbols) 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( 173 +fn parse_keybindings(config: &Table, modkey: KeyButMask) -> Result<Vec<KeyBinding>, ConfigError> {
169 - config: &Table,
170 - modkey: KeyButMask,
171 -) -> Result<Vec<KeyBinding>, ConfigError> {
172 let keybindings_table: Table = get_table_field(config, "keybindings")?; 174 let keybindings_table: Table = get_table_field(config, "keybindings")?;
173 let mut keybindings = Vec::new(); 175 let mut keybindings = Vec::new();
174 176
175 - for i in 1..=keybindings_table.len().map_err(|e| { 177 + for i in 1..=keybindings_table
176 - ConfigError::LuaError(format!("Failed to get keybindings length: {}", e)) 178 + .len()
177 - })? { 179 + .map_err(|e| ConfigError::LuaError(format!("Failed to get keybindings length: {}", e)))?
180 + {
178 let kb_table: Table = keybindings_table.get(i).map_err(|e| { 181 let kb_table: Table = keybindings_table.get(i).map_err(|e| {
179 ConfigError::LuaError(format!("Failed to get keybinding at index {}: {}", i, e)) 182 ConfigError::LuaError(format!("Failed to get keybinding at index {}: {}", i, e))
180 })?; 183 })?;
@@ -189,19 +192,15 @@ fn parse_keybindings(
189 Ok(keybindings) 192 Ok(keybindings)
190 } 193 }
191 194
192 -fn parse_keypress_list( 195 +fn parse_keypress_list(kb_table: &Table, modkey: KeyButMask) -> Result<Vec<KeyPress>, ConfigError> {
193 - kb_table: &Table,
194 - modkey: KeyButMask,
195 -) -> Result<Vec<KeyPress>, ConfigError> {
196 - // Check if 'keys' field exists (for keychords)
197 let keys_result: Result<Table, _> = kb_table.get("keys"); 196 let keys_result: Result<Table, _> = kb_table.get("keys");
198 197
199 if let Ok(keys_table) = keys_result { 198 if let Ok(keys_table) = keys_result {
200 - // Parse keychord
201 let mut keys = Vec::new(); 199 let mut keys = Vec::new();
202 - for i in 1..=keys_table.len().map_err(|e| { 200 + for i in 1..=keys_table
203 - ConfigError::LuaError(format!("Failed to get keys length: {}", e)) 201 + .len()
204 - })? { 202 + .map_err(|e| ConfigError::LuaError(format!("Failed to get keys length: {}", e)))?
203 + {
205 let key_entry: Table = keys_table.get(i).map_err(|e| { 204 let key_entry: Table = keys_table.get(i).map_err(|e| {
206 ConfigError::LuaError(format!("Failed to get key at index {}: {}", i, e)) 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 Ok(keys) 213 Ok(keys)
215 } else { 214 } else {
216 - // Parse single key (old format)
217 let modifiers = parse_modifiers(kb_table, "modifiers", modkey)?; 215 let modifiers = parse_modifiers(kb_table, "modifiers", modkey)?;
218 let keysym = parse_keysym(kb_table, "key")?; 216 let keysym = parse_keysym(kb_table, "key")?;
219 217
@@ -229,9 +227,10 @@ fn parse_modifiers(
229 let mods_table: Table = get_table_field(table, field)?; 227 let mods_table: Table = get_table_field(table, field)?;
230 let mut modifiers = Vec::new(); 228 let mut modifiers = Vec::new();
231 229
232 - for i in 1..=mods_table.len().map_err(|e| { 230 + for i in 1..=mods_table
233 - ConfigError::LuaError(format!("Failed to get modifiers length: {}", e)) 231 + .len()
234 - })? { 232 + .map_err(|e| ConfigError::LuaError(format!("Failed to get modifiers length: {}", e)))?
233 + {
235 let mod_str: String = mods_table.get(i).map_err(|e| { 234 let mod_str: String = mods_table.get(i).map_err(|e| {
236 ConfigError::LuaError(format!("Failed to get modifier at index {}: {}", i, e)) 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 Ok(Value::Number(n)) => Ok(Arg::Int(n as i32)), 386 Ok(Value::Number(n)) => Ok(Arg::Int(n as i32)),
388 Ok(Value::Table(t)) => { 387 Ok(Value::Table(t)) => {
389 let mut arr = Vec::new(); 388 let mut arr = Vec::new();
390 - for i in 1..=t 389 + for i in 1..=t.len().map_err(|e| {
391 - .len() 390 + ConfigError::LuaError(format!("Failed to get arg array length: {}", e))
392 - .map_err(|e| ConfigError::LuaError(format!("Failed to get arg array length: {}", e)))? 391 + })? {
393 - {
394 let item: String = t.get(i).map_err(|e| { 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 arr.push(item); 398 arr.push(item);
398 } 399 }
@@ -408,9 +409,10 @@ fn parse_status_blocks(config: &Table) -> Result<Vec<BlockConfig>, ConfigError>
408 let blocks_table: Table = get_table_field(config, "status_blocks")?; 409 let blocks_table: Table = get_table_field(config, "status_blocks")?;
409 let mut blocks = Vec::new(); 410 let mut blocks = Vec::new();
410 411
411 - for i in 1..=blocks_table.len().map_err(|e| { 412 + for i in 1..=blocks_table
412 - ConfigError::LuaError(format!("Failed to get status_blocks length: {}", e)) 413 + .len()
413 - })? { 414 + .map_err(|e| ConfigError::LuaError(format!("Failed to get status_blocks length: {}", e)))?
415 + {
414 let block_table: Table = blocks_table.get(i).map_err(|e| { 416 let block_table: Table = blocks_table.get(i).map_err(|e| {
415 ConfigError::LuaError(format!("Failed to get status_block at index {}: {}", i, e)) 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 let format: String = get_table_field(&block_table, "format")?; 420 let format: String = get_table_field(&block_table, "format")?;
419 let command_str: String = get_table_field(&block_table, "command")?; 421 let command_str: String = get_table_field(&block_table, "command")?;
420 422
421 - // Parse interval_secs - handle both integer and number types
422 let interval_secs: u64 = { 423 let interval_secs: u64 = {
423 let value: Value = block_table.get("interval_secs").map_err(|e| { 424 let value: Value = block_table.get("interval_secs").map_err(|e| {
424 ConfigError::LuaError(format!("Failed to get interval_secs: {}", e)) 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 match value { 427 match value {
427 Value::Integer(i) => i as u64, 428 Value::Integer(i) => i as u64,
428 Value::Number(n) => n as u64, 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 ConfigError::LuaError(format!("Failed to get autostart length: {}", e)) 505 ConfigError::LuaError(format!("Failed to get autostart length: {}", e))
501 })? { 506 })? {
502 let cmd: String = autostart_table.get(i).map_err(|e| { 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 autostart.push(cmd); 513 autostart.push(cmd);
506 } 514 }
507 515
508 Ok(autostart) 516 Ok(autostart)
509 } 517 }
510 - Err(_) => Ok(Vec::new()), // autostart is optional 518 + Err(_) => Ok(Vec::new()),
511 } 519 }
512 } 520 }
513 521