config: extract expand home path helper
Browse files at this commit180e6c3eb892a1ae551040ad1a3066d6f37b1f92Parent: e5b0c1a2 changed files +30 -17
- niri-config/src/lib.rs +5 -17
- niri-config/src/utils.rs +25 -0
niri-config/src/lib.rsmodified+5 -17
| @@ -58,7 +58,7 @@ pub use crate::output::{Output, OutputName, Outputs, Position, Vrr}; | |||
| 58 | use crate::recent_windows::RecentWindowsPart; | 58 | use crate::recent_windows::RecentWindowsPart; |
| 59 | pub use crate::recent_windows::{MruDirection, MruFilter, MruPreviews, MruScope, RecentWindows}; | 59 | pub use crate::recent_windows::{MruDirection, MruFilter, MruPreviews, MruScope, RecentWindows}; |
| 60 | pub use crate::utils::FloatOrInt; | 60 | pub use crate::utils::FloatOrInt; |
| 61 | -use crate::utils::{Flag, MergeWith as _}; | 61 | +use crate::utils::{expand_home_path, Flag, MergeWith as _}; |
| 62 | pub use crate::window_rule::{ | 62 | pub use crate::window_rule::{ |
| 63 | FloatingPosition, OnXdgActivate, PopupsRule, RelativeTo, ResolvedPopupsRules, WindowRule, | 63 | FloatingPosition, OnXdgActivate, PopupsRule, RelativeTo, ResolvedPopupsRules, WindowRule, |
| 64 | }; | 64 | }; |
| @@ -303,8 +303,6 @@ where | |||
| 303 | "additional argument for include path is required", | 303 | "additional argument for include path is required", |
| 304 | ) | 304 | ) |
| 305 | })?; | 305 | })?; |
| 306 | - let path: PathBuf = knuffel::traits::DecodeScalar::decode(path_val, ctx)?; | ||
| 307 | - | ||
| 308 | // Check for extra arguments | 306 | // Check for extra arguments |
| 309 | if let Some(val) = iter_args.next() { | 307 | if let Some(val) = iter_args.next() { |
| 310 | ctx.emit_error(DecodeError::unexpected( | 308 | ctx.emit_error(DecodeError::unexpected( |
| @@ -343,21 +341,11 @@ where | |||
| 343 | // We use DecodeError::Missing throughout this block because it results in the | 341 | // We use DecodeError::Missing throughout this block because it results in the |
| 344 | // least confusing error messages while still allowing to provide a span. | 342 | // least confusing error messages while still allowing to provide a span. |
| 345 | 343 | ||
| 344 | + let decoded_path = knuffel::traits::DecodeScalar::decode(path_val, ctx)?; | ||
| 345 | + | ||
| 346 | // Expand ~ into the home dir | 346 | // Expand ~ into the home dir |
| 347 | - let path = if let Ok(rest) = path.strip_prefix("~") { | 347 | + let Some(path) = expand_home_path(decoded_path, node, ctx) else { |
| 348 | - let Some(home) = std::env::home_dir() else { | 348 | + continue; |
| 349 | - ctx.emit_error(DecodeError::missing( | ||
| 350 | - node, | ||
| 351 | - format!("error retrieving home directory to expand {path:?}"), | ||
| 352 | - )); | ||
| 353 | - continue; | ||
| 354 | - }; | ||
| 355 | - | ||
| 356 | - home.join(rest) | ||
| 357 | - } else { | ||
| 358 | - // Otherwise, use the current include base dir | ||
| 359 | - let base = ctx.get::<BasePath>().unwrap(); | ||
| 360 | - base.0.join(path) | ||
| 361 | }; | 349 | }; |
| 362 | 350 | ||
| 363 | let recursion = ctx.get::<Recursion>().unwrap().0 + 1; | 351 | let recursion = ctx.get::<Recursion>().unwrap().0 + 1; |
niri-config/src/utils.rsmodified+25 -0
| @@ -1,5 +1,8 @@ | |||
| 1 | +use std::env::home_dir; | ||
| 2 | +use std::path::PathBuf; | ||
| 1 | use std::str::FromStr; | 3 | use std::str::FromStr; |
| 2 | 4 | ||
| 5 | +use knuffel::ast::SpannedNode; | ||
| 3 | use knuffel::errors::DecodeError; | 6 | use knuffel::errors::DecodeError; |
| 4 | use miette::miette; | 7 | use miette::miette; |
| 5 | use regex::Regex; | 8 | use regex::Regex; |
| @@ -7,6 +10,8 @@ use regex::Regex; | |||
| 7 | mod merge_with; | 10 | mod merge_with; |
| 8 | pub use merge_with::*; | 11 | pub use merge_with::*; |
| 9 | 12 | ||
| 13 | +use crate::BasePath; | ||
| 14 | + | ||
| 10 | #[derive(Debug, Clone, Copy, PartialEq)] | 15 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 11 | pub struct Percent(pub f64); | 16 | pub struct Percent(pub f64); |
| 12 | 17 | ||
| @@ -205,3 +210,23 @@ pub fn parse_arg_node<S: knuffel::traits::ErrorSpan, T: knuffel::traits::DecodeS | |||
| 205 | 210 | ||
| 206 | Ok(value) | 211 | Ok(value) |
| 207 | } | 212 | } |
| 213 | + | ||
| 214 | +pub fn expand_home_path<S: knuffel::traits::ErrorSpan>( | ||
| 215 | + path: PathBuf, | ||
| 216 | + spanned: &SpannedNode<S>, | ||
| 217 | + ctx: &mut knuffel::decode::Context<S>, | ||
| 218 | +) -> Option<PathBuf> { | ||
| 219 | + if let Ok(rest) = path.strip_prefix("~") { | ||
| 220 | + let Some(home) = home_dir() else { | ||
| 221 | + ctx.emit_error(DecodeError::missing( | ||
| 222 | + spanned, | ||
| 223 | + format!("error retrieving home directory to expand {:?}", path), | ||
| 224 | + )); | ||
| 225 | + return None; | ||
| 226 | + }; | ||
| 227 | + home.join(rest).into() | ||
| 228 | + } else { | ||
| 229 | + let base = ctx.get::<BasePath>().unwrap(); | ||
| 230 | + base.0.join(path).into() | ||
| 231 | + } | ||
| 232 | +} | ||