forge
Explore
Log in

Don't you dare go Hollow

f64cb6c03e1bc69422edbf7731d0031ffd4f6771Ivan MolodetskikhParent: 9125786
niri-config/src/lib.rsmodified+37 -37
@@ -3,10 +3,10 @@ extern crate tracing;
3 3
4 use std::ffi::OsStr;4 use std::ffi::OsStr;
5 use std::fs::{self, File};5 use std::fs::{self, File};
6-use std::io::Write;6+use std::io::Write as _;
7 use std::path::{Path, PathBuf};7 use std::path::{Path, PathBuf};
8 8
9-use miette::{Context, IntoDiagnostic};9+use miette::{Context as _, IntoDiagnostic as _};
10 10
11 pub mod animations;11 pub mod animations;
12 pub mod appearance;12 pub mod appearance;
@@ -109,8 +109,41 @@ pub enum ConfigPath {
109 },109 },
110 }110 }
111 111
112+impl Config {
113+ pub fn load(path: &Path) -> miette::Result<Self> {
114+ let contents = fs::read_to_string(path)
115+ .into_diagnostic()
116+ .with_context(|| format!("error reading {path:?}"))?;
117+
118+ let config = Self::parse(
119+ path.file_name()
120+ .and_then(OsStr::to_str)
121+ .unwrap_or("config.kdl"),
122+ &contents,
123+ )
124+ .context("error parsing")?;
125+ debug!("loaded config from {path:?}");
126+ Ok(config)
127+ }
128+
129+ pub fn parse(filename: &str, text: &str) -> Result<Self, knuffel::Error> {
130+ let _span = tracy_client::span!("Config::parse");
131+ knuffel::parse(filename, text)
132+ }
133+}
134+
135+impl Default for Config {
136+ fn default() -> Self {
137+ Config::parse(
138+ "default-config.kdl",
139+ include_str!("../../resources/default-config.kdl"),
140+ )
141+ .unwrap()
142+ }
143+}
144+
112 impl ConfigPath {145 impl ConfigPath {
113- /// Load the config, or return an error if it doesn't exist.146+ /// Loads the config, returns an error if it doesn't exist.
114 pub fn load(&self) -> miette::Result<Config> {147 pub fn load(&self) -> miette::Result<Config> {
115 let _span = tracy_client::span!("ConfigPath::load");148 let _span = tracy_client::span!("ConfigPath::load");
116 149
@@ -122,7 +155,7 @@ impl ConfigPath {
122 .context("error loading config")155 .context("error loading config")
123 }156 }
124 157
125- /// Load the config, or create it if it doesn't exist.158+ /// Loads the config, or creates it if it doesn't exist.
126 ///159 ///
127 /// Returns a tuple containing the path that was created, if any, and the loaded config.160 /// Returns a tuple containing the path that was created, if any, and the loaded config.
128 ///161 ///
@@ -199,39 +232,6 @@ impl ConfigPath {
199 }232 }
200 }233 }
201 234
202-impl Config {
203- pub fn load(path: &Path) -> miette::Result<Self> {
204- let contents = fs::read_to_string(path)
205- .into_diagnostic()
206- .with_context(|| format!("error reading {path:?}"))?;
207-
208- let config = Self::parse(
209- path.file_name()
210- .and_then(OsStr::to_str)
211- .unwrap_or("config.kdl"),
212- &contents,
213- )
214- .context("error parsing")?;
215- debug!("loaded config from {path:?}");
216- Ok(config)
217- }
218-
219- pub fn parse(filename: &str, text: &str) -> Result<Self, knuffel::Error> {
220- let _span = tracy_client::span!("Config::parse");
221- knuffel::parse(filename, text)
222- }
223-}
224-
225-impl Default for Config {
226- fn default() -> Self {
227- Config::parse(
228- "default-config.kdl",
229- include_str!("../../resources/default-config.kdl"),
230- )
231- .unwrap()
232- }
233-}
234-
235 #[cfg(test)]235 #[cfg(test)]
236 mod tests {236 mod tests {
237 use insta::assert_debug_snapshot;237 use insta::assert_debug_snapshot;