Skip to content

Commit

Permalink
refactor: change user config to use non-async fs operations
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Jul 31, 2024
1 parent 460bfe0 commit db2c441
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
5 changes: 1 addition & 4 deletions packages/wm/src/common/commands/reload_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion packages/wm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
28 changes: 12 additions & 16 deletions packages/wm/src/user_config.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<PathBuf>) -> anyhow::Result<Self> {
pub fn new(config_path: Option<PathBuf>) -> anyhow::Result<Self> {
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);

Expand All @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit db2c441

Please sign in to comment.