oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git

fix edge case of config path being set to None if an error is encountered in config

Commit
3bd419c5675d0f8fbeefa139a6034144c5deb7b8
Parent
fa421d2
Author
emzywastaken <amiamemetoo@gmail.com>
Date
2025-12-21 17:30:19
fixes #99

Diff

diff --git a/src/bin/main.rs b/src/bin/main.rs
index d67ac47..1e4ab21 100644
--- a/src/bin/main.rs
+++ b/src/bin/main.rs
@@ -86,18 +86,21 @@ fn load_config(
 
     let config_directory = config_path.parent();
 
-    match oxwm::config::parse_lua_config(&config_string, config_directory) {
-        Ok(mut config) => {
-            config.path = Some(config_path);
-            Ok((config, false))
-        }
-        Err(_error) => {
-            let template = include_str!("../../templates/config.lua");
-            let config = oxwm::config::parse_lua_config(template, None)
-                .map_err(|error| format!("Failed to parse default template config: {}", error))?;
-            Ok((config, true))
-        }
-    }
+    let (mut config, had_error) =
+        match oxwm::config::parse_lua_config(&config_string, config_directory) {
+            Ok(config) => (config, false),
+            Err(_error) => {
+                let template = include_str!("../../templates/config.lua");
+                let config = oxwm::config::parse_lua_config(template, None).map_err(|error| {
+                    format!("Failed to parse default template config: {}", error)
+                })?;
+                (config, true)
+            }
+        };
+
+    config.path = Some(config_path);
+
+    Ok((config, had_error))
 }
 
 fn init_config() -> Result<(), Box<dyn std::error::Error>> {
diff --git a/src/window_manager.rs b/src/window_manager.rs
index 6e29e90..19caaa3 100644
--- a/src/window_manager.rs
+++ b/src/window_manager.rs
@@ -347,7 +347,7 @@ impl WindowManager {
 
     fn try_reload_config(&mut self) -> Result<(), String> {
         let lua_path =
-            self.config.path.take().ok_or(
+            self.config.path.as_ref().ok_or(
                 "Could not find config file. Config path should've been set while loading",
             )?;
 
@@ -355,7 +355,7 @@ impl WindowManager {
             return Err("Could not find config file, has it been moved?".to_string());
         }
 
-        let config_str = std::fs::read_to_string(&lua_path)
+        let config_str = std::fs::read_to_string(lua_path)
             .map_err(|e| format!("Failed to read config: {}", e))?;
 
         let config_dir = lua_path.parent();
@@ -363,8 +363,10 @@ impl WindowManager {
         let new_config = crate::config::parse_lua_config(&config_str, config_dir)
             .map_err(|e| format!("{}", e))?;
 
+        let lua_path = self.config.path.take();
+
         self.config = new_config;
-        self.config.path = Some(lua_path);
+        self.config.path = lua_path;
         self.error_message = None;
 
         for bar in &mut self.bars {