From db2c4419deda3f977b3b6e10cf31c9d5ab5e9c0e Mon Sep 17 00:00:00 2001 From: Lars Berger Date: Wed, 31 Jul 2024 13:39:50 +0800 Subject: [PATCH] refactor: change user config to use non-async fs operations --- .../wm/src/common/commands/reload_config.rs | 5 +--- packages/wm/src/main.rs | 2 +- packages/wm/src/user_config.rs | 28 ++++++++----------- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/packages/wm/src/common/commands/reload_config.rs b/packages/wm/src/common/commands/reload_config.rs index 18517803..1324a21c 100644 --- a/packages/wm/src/common/commands/reload_config.rs +++ b/packages/wm/src/common/commands/reload_config.rs @@ -20,10 +20,7 @@ pub fn reload_config( let old_config = config.value.clone(); // Re-evaluate user config file and set its values in state. - tokio::task::block_in_place(|| { - let rt = tokio::runtime::Handle::current(); - rt.block_on(config.reload()) - })?; + config.reload()?; // Re-run window rules on all active windows. for window in state.windows() { diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rs index 7261cd8e..26cf0f3f 100644 --- a/packages/wm/src/main.rs +++ b/packages/wm/src/main.rs @@ -104,7 +104,7 @@ async fn start_wm( let _single_instance = Platform::new_single_instance()?; // Parse and validate user config. - let mut config = UserConfig::new(config_path).await?; + let mut config = UserConfig::new(config_path)?; // Start watcher process for restoring hidden windows on crash. start_watcher_process()?; diff --git a/packages/wm/src/user_config.rs b/packages/wm/src/user_config.rs index f1732ef5..ff824bc2 100644 --- a/packages/wm/src/user_config.rs +++ b/packages/wm/src/user_config.rs @@ -1,8 +1,7 @@ -use std::{collections::HashMap, path::PathBuf}; +use std::{collections::HashMap, fs, path::PathBuf}; use anyhow::{Context, Result}; use serde::{Deserialize, Serialize}; -use tokio::fs; use crate::{ app_command::InvokeCommand, @@ -38,13 +37,13 @@ impl UserConfig { /// config from the given path. /// /// Creates a new config file from sample if it doesn't exist. - pub async fn new(config_path: Option) -> anyhow::Result { + pub fn new(config_path: Option) -> anyhow::Result { let default_config_path = home::home_dir() .context("Unable to get home directory.")? .join(".glzr/glazewm/config.yaml"); let config_path = config_path.unwrap_or(default_config_path); - let (config_value, config_str) = Self::read(&config_path).await?; + let (config_value, config_str) = Self::read(&config_path)?; let window_rules_by_event = Self::window_rules_by_event(&config_value); @@ -59,15 +58,14 @@ impl UserConfig { /// Reads and validates the user config from the given path. /// /// Creates a new config file from sample if it doesn't exist. - async fn read( + fn read( config_path: &PathBuf, ) -> anyhow::Result<(ParsedConfig, String)> { if !config_path.exists() { - Self::create_sample(config_path.clone()).await?; + Self::create_sample(config_path.clone())?; } let config_str = fs::read_to_string(&config_path) - .await .context("Unable to read config file.")?; // TODO: Improve error formatting of serde_yaml errors. Something @@ -78,25 +76,23 @@ impl UserConfig { } /// Initializes a new config file from the sample config resource. - async fn create_sample(config_path: PathBuf) -> Result<()> { + fn create_sample(config_path: PathBuf) -> Result<()> { let parent_dir = config_path.parent().context("Invalid config path.")?; - fs::create_dir_all(parent_dir).await.with_context(|| { + fs::create_dir_all(parent_dir).with_context(|| { format!("Unable to create directory {}.", &config_path.display()) })?; - fs::write(&config_path, SAMPLE_CONFIG) - .await - .with_context(|| { - format!("Unable to write to {}.", config_path.display()) - })?; + fs::write(&config_path, SAMPLE_CONFIG).with_context(|| { + format!("Unable to write to {}.", config_path.display()) + })?; Ok(()) } - pub async fn reload(&mut self) -> anyhow::Result<()> { - let (config_value, config_str) = Self::read(&self.path).await?; + pub fn reload(&mut self) -> anyhow::Result<()> { + let (config_value, config_str) = Self::read(&self.path)?; self.window_rules_by_event = Self::window_rules_by_event(&config_value);