From f7bd28e46ec660f12e6a9bf74ab8d2cc6b112109 Mon Sep 17 00:00:00 2001 From: ppom Date: Wed, 28 May 2025 12:00:00 +0200 Subject: [PATCH] Config: cleanup code --- src/client/test_regex.rs | 2 +- src/concepts/config.rs | 50 +++++++++++++++++----------------------- src/daemon/mod.rs | 2 +- src/treedb/mod.rs | 2 +- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/src/client/test_regex.rs b/src/client/test_regex.rs index 3197106..c698235 100644 --- a/src/client/test_regex.rs +++ b/src/client/test_regex.rs @@ -15,7 +15,7 @@ pub fn test_regex( mut regex: String, line: Option, ) -> Result<(), Box> { - let config: Config = Config::from_file(&config_path)?; + let config = Config::from_path(&config_path)?; // Code close to Filter::setup() let mut used_patterns: BTreeSet> = BTreeSet::new(); diff --git a/src/concepts/config.rs b/src/concepts/config.rs index 7173c12..dc5e02e 100644 --- a/src/concepts/config.rs +++ b/src/concepts/config.rs @@ -74,7 +74,7 @@ impl Config { .and_then(|stream| stream.get_filter(&name.1)) } - pub fn merge(&mut self, mut other: Config) -> Result<(), String> { + fn merge(&mut self, mut other: Config) -> Result<(), String> { for (key, pattern) in other.patterns.into_iter() { match self.patterns.entry(key) { btree_map::Entry::Occupied(e) => { @@ -145,6 +145,26 @@ impl Config { run_commands(&self.stop, "stop") } + pub fn from_path(path: &Path) -> Result { + match Self::from_path_raw(path) { + Ok((mut cfg, files)) => { + cfg.setup().map_err(ConfigError::BadConfig).map_err(|e| { + if path.is_dir() { + format!( + "{e}\nWhile reading config from {}. List of files read, in that order:\n{}", + path.display(), + files.join("\n"), + ) + } else { + format!("{e}\nWhile reading config from {}.", path.display()) + } + })?; + Ok(cfg) + } + Err(e) => Err(e), + } + } + pub fn from_path_raw(path: &Path) -> Result<(Self, Vec), String> { match std::fs::metadata(path) { Err(e) => Err(format!("Error accessing {}: {e}", path.to_string_lossy())), @@ -171,26 +191,6 @@ impl Config { } } - pub fn from_path(path: &Path) -> Result<(Self, Vec), String> { - match Self::from_path_raw(path) { - Ok((mut cfg, files)) => { - cfg.setup().map_err(ConfigError::BadConfig).map_err(|e| { - if path.is_dir() { - format!( - "{e}\nWhile reading config from {}. List of files read, in that order:\n{}", - path.display(), - files.join("\n"), - ) - } else { - format!("{e}\nWhile reading config from {}.", path.display()) - } - })?; - Ok((cfg, files)) - } - Err(e) => Err(e), - } - } - fn _from_dir_raw(path: &Path) -> Result<(Self, Vec), String> { let dir = std::fs::read_dir(path) .map_err(|e| format!("Error accessing directory {}: {e}", path.display()))?; @@ -277,14 +277,6 @@ impl Config { } } - pub fn from_file(path: &Path) -> Result { - match Self::_from_file_raw(path) { - Ok(mut cfg) => cfg.setup().map(|_| cfg).map_err(ConfigError::BadConfig), - Err(e) => Err(e), - } - .map_err(|err| format!("Configuration file {}: {}", path.display(), err)) - } - fn _extension_to_format(extension: &str) -> Result { match extension { "yaml" | "yml" => Ok(Format::Yaml), diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs index 058165b..ed5294b 100644 --- a/src/daemon/mod.rs +++ b/src/daemon/mod.rs @@ -31,7 +31,7 @@ pub async fn daemon( config_path: PathBuf, socket: PathBuf, ) -> Result<(), Box> { - let config: &'static Config = Box::leak(Box::new(Config::from_path(&config_path)?.0)); + let config: &'static Config = Box::leak(Box::new(Config::from_path(&config_path)?)); if !config.start() { return Err("a start command failed, exiting.".into()); diff --git a/src/treedb/mod.rs b/src/treedb/mod.rs index 0cd8c2c..a12f227 100644 --- a/src/treedb/mod.rs +++ b/src/treedb/mod.rs @@ -421,7 +421,7 @@ mod tests { ) .await?; - let config = Config::from_file(&config_path).unwrap(); + let config = Config::from_path(&config_path).unwrap(); Database::open(&config).await }