From 180e6c3eb892a1ae551040ad1a3066d6f37b1f92 Mon Sep 17 00:00:00 2001 From: HigherOrderLogic <73709188+HigherOrderLogic@users.noreply.github.com> Date: Tue, 15 Sep 2026 21:44:32 +1000 Subject: [PATCH] config: extract expand home path helper --- niri-config/src/lib.rs | 22 +++++----------------- niri-config/src/utils.rs | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index bcab6558..e8ae3cc1 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -58,7 +58,7 @@ pub use crate::output::{Output, OutputName, Outputs, Position, Vrr}; use crate::recent_windows::RecentWindowsPart; pub use crate::recent_windows::{MruDirection, MruFilter, MruPreviews, MruScope, RecentWindows}; pub use crate::utils::FloatOrInt; -use crate::utils::{Flag, MergeWith as _}; +use crate::utils::{expand_home_path, Flag, MergeWith as _}; pub use crate::window_rule::{ FloatingPosition, OnXdgActivate, PopupsRule, RelativeTo, ResolvedPopupsRules, WindowRule, }; @@ -303,8 +303,6 @@ where "additional argument for include path is required", ) })?; - let path: PathBuf = knuffel::traits::DecodeScalar::decode(path_val, ctx)?; - // Check for extra arguments if let Some(val) = iter_args.next() { ctx.emit_error(DecodeError::unexpected( @@ -343,21 +341,11 @@ where // We use DecodeError::Missing throughout this block because it results in the // least confusing error messages while still allowing to provide a span. + let decoded_path = knuffel::traits::DecodeScalar::decode(path_val, ctx)?; + // Expand ~ into the home dir - let path = if let Ok(rest) = path.strip_prefix("~") { - let Some(home) = std::env::home_dir() else { - ctx.emit_error(DecodeError::missing( - node, - format!("error retrieving home directory to expand {path:?}"), - )); - continue; - }; - - home.join(rest) - } else { - // Otherwise, use the current include base dir - let base = ctx.get::().unwrap(); - base.0.join(path) + let Some(path) = expand_home_path(decoded_path, node, ctx) else { + continue; }; let recursion = ctx.get::().unwrap().0 + 1; diff --git a/niri-config/src/utils.rs b/niri-config/src/utils.rs index f0318882..3da335cd 100644 --- a/niri-config/src/utils.rs +++ b/niri-config/src/utils.rs @@ -1,5 +1,8 @@ +use std::env::home_dir; +use std::path::PathBuf; use std::str::FromStr; +use knuffel::ast::SpannedNode; use knuffel::errors::DecodeError; use miette::miette; use regex::Regex; @@ -7,6 +10,8 @@ use regex::Regex; mod merge_with; pub use merge_with::*; +use crate::BasePath; + #[derive(Debug, Clone, Copy, PartialEq)] pub struct Percent(pub f64); @@ -205,3 +210,23 @@ pub fn parse_arg_node( + path: PathBuf, + spanned: &SpannedNode, + ctx: &mut knuffel::decode::Context, +) -> Option { + if let Ok(rest) = path.strip_prefix("~") { + let Some(home) = home_dir() else { + ctx.emit_error(DecodeError::missing( + spanned, + format!("error retrieving home directory to expand {:?}", path), + )); + return None; + }; + home.join(rest).into() + } else { + let base = ctx.get::().unwrap(); + base.0.join(path).into() + } +}