diff options
| author | Ivan Molodetskikh <yalterz@gmail.com> | 2025-08-27 11:12:17 +0300 |
|---|---|---|
| committer | Ivan Molodetskikh <yalterz@gmail.com> | 2025-08-27 11:12:28 +0300 |
| commit | f64cb6c03e1bc69422edbf7731d0031ffd4f6771 (patch) | |
| tree | 2963247692191637158f398d1641418d5a38ba50 | |
| parent | 91257862e511f87fb70f954e923617bd1eb17759 (diff) | |
| download | niri-f64cb6c03e1bc69422edbf7731d0031ffd4f6771.tar.gz niri-f64cb6c03e1bc69422edbf7731d0031ffd4f6771.tar.bz2 niri-f64cb6c03e1bc69422edbf7731d0031ffd4f6771.zip | |
config: Cleanup
| -rw-r--r-- | niri-config/src/lib.rs | 74 |
1 files changed, 37 insertions, 37 deletions
diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index 8b10ee02..3210697d 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -3,10 +3,10 @@ extern crate tracing; use std::ffi::OsStr; use std::fs::{self, File}; -use std::io::Write; +use std::io::Write as _; use std::path::{Path, PathBuf}; -use miette::{Context, IntoDiagnostic}; +use miette::{Context as _, IntoDiagnostic as _}; pub mod animations; pub mod appearance; @@ -109,8 +109,41 @@ pub enum ConfigPath { }, } +impl Config { + pub fn load(path: &Path) -> miette::Result<Self> { + let contents = fs::read_to_string(path) + .into_diagnostic() + .with_context(|| format!("error reading {path:?}"))?; + + let config = Self::parse( + path.file_name() + .and_then(OsStr::to_str) + .unwrap_or("config.kdl"), + &contents, + ) + .context("error parsing")?; + debug!("loaded config from {path:?}"); + Ok(config) + } + + pub fn parse(filename: &str, text: &str) -> Result<Self, knuffel::Error> { + let _span = tracy_client::span!("Config::parse"); + knuffel::parse(filename, text) + } +} + +impl Default for Config { + fn default() -> Self { + Config::parse( + "default-config.kdl", + include_str!("../../resources/default-config.kdl"), + ) + .unwrap() + } +} + impl ConfigPath { - /// Load the config, or return an error if it doesn't exist. + /// Loads the config, returns an error if it doesn't exist. pub fn load(&self) -> miette::Result<Config> { let _span = tracy_client::span!("ConfigPath::load"); @@ -122,7 +155,7 @@ impl ConfigPath { .context("error loading config") } - /// Load the config, or create it if it doesn't exist. + /// Loads the config, or creates it if it doesn't exist. /// /// Returns a tuple containing the path that was created, if any, and the loaded config. /// @@ -199,39 +232,6 @@ impl ConfigPath { } } -impl Config { - pub fn load(path: &Path) -> miette::Result<Self> { - let contents = fs::read_to_string(path) - .into_diagnostic() - .with_context(|| format!("error reading {path:?}"))?; - - let config = Self::parse( - path.file_name() - .and_then(OsStr::to_str) - .unwrap_or("config.kdl"), - &contents, - ) - .context("error parsing")?; - debug!("loaded config from {path:?}"); - Ok(config) - } - - pub fn parse(filename: &str, text: &str) -> Result<Self, knuffel::Error> { - let _span = tracy_client::span!("Config::parse"); - knuffel::parse(filename, text) - } -} - -impl Default for Config { - fn default() -> Self { - Config::parse( - "default-config.kdl", - include_str!("../../resources/default-config.kdl"), - ) - .unwrap() - } -} - #[cfg(test)] mod tests { use insta::assert_debug_snapshot; |
