From f67cfcd5353bf112760f89a9451aafc2ba2d9fde Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 4 Oct 2024 19:10:20 +0000 Subject: [PATCH] cleanup Config::load() Signed-off-by: Jason Volk --- src/core/config/mod.rs | 42 +++++++++++++++++------------------------- src/main/server.rs | 2 +- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/core/config/mod.rs b/src/core/config/mod.rs index 64e1c9ba..40c900e5 100644 --- a/src/core/config/mod.rs +++ b/src/core/config/mod.rs @@ -1,3 +1,6 @@ +pub mod check; +pub mod proxy; + use std::{ collections::{BTreeMap, BTreeSet}, fmt, @@ -22,10 +25,7 @@ use url::Url; pub use self::check::check; use self::proxy::ProxyConfig; -use crate::{error::Error, utils::sys, Err, Result}; - -pub mod check; -pub mod proxy; +use crate::{err, error::Error, utils::sys, Result}; /// all the config options for conduwuit #[config_example_generator] @@ -441,34 +441,26 @@ const DEPRECATED_KEYS: &[&str; 9] = &[ impl Config { /// Pre-initialize config - pub fn load(paths: &Option>) -> Result { - let raw_config = if let Some(config_file_env) = Env::var("CONDUIT_CONFIG") { - Figment::new().merge(Toml::file(config_file_env).nested()) - } else if let Some(config_file_arg) = Env::var("CONDUWUIT_CONFIG") { - Figment::new().merge(Toml::file(config_file_arg).nested()) - } else if let Some(config_file_args) = paths { - let mut figment = Figment::new(); + pub fn load(paths: Option<&[PathBuf]>) -> Result { + let paths_files = paths.into_iter().flatten().map(Toml::file); - for config in config_file_args { - figment = figment.merge(Toml::file(config).nested()); - } + let envs = [Env::var("CONDUIT_CONFIG"), Env::var("CONDUWUIT_CONFIG")]; + let envs_files = envs.into_iter().flatten().map(Toml::file); - figment - } else { - Figment::new() - }; - - Ok(raw_config + let config = envs_files + .chain(paths_files) + .fold(Figment::new(), |config, file| config.merge(file.nested())) .merge(Env::prefixed("CONDUIT_").global().split("__")) - .merge(Env::prefixed("CONDUWUIT_").global().split("__"))) + .merge(Env::prefixed("CONDUWUIT_").global().split("__")); + + Ok(config) } /// Finalize config pub fn new(raw_config: &Figment) -> Result { - let config = match raw_config.extract::() { - Err(e) => return Err!("There was a problem with your configuration file: {e}"), - Ok(config) => config, - }; + let config = raw_config + .extract::() + .map_err(|e| err!("There was a problem with your configuration file: {e}"))?; // don't start if we're listening on both UNIX sockets and TCP at same time check::is_dual_listening(raw_config)?; diff --git a/src/main/server.rs b/src/main/server.rs index e435b2f4..4813d586 100644 --- a/src/main/server.rs +++ b/src/main/server.rs @@ -24,7 +24,7 @@ pub(crate) struct Server { impl Server { pub(crate) fn build(args: &Args, runtime: Option<&runtime::Handle>) -> Result, Error> { - let raw_config = Config::load(&args.config)?; + let raw_config = Config::load(args.config.as_deref())?; let raw_config = crate::clap::update(raw_config, args)?; let config = Config::new(&raw_config)?;