refactor: import cleanup for config

This commit is contained in:
Greg Heartsfield 2022-02-26 11:06:23 -06:00
parent 225c8f762e
commit 414e83f696

View File

@ -1,4 +1,5 @@
//! Configuration file and settings management //! Configuration file and settings management
use config::{Config, ConfigError, File};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::*; use log::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -138,27 +139,25 @@ pub struct Settings {
impl Settings { impl Settings {
pub fn new() -> Self { pub fn new() -> Self {
let d = Self::default(); let default_settings = Self::default();
// attempt to construct settings with file // attempt to construct settings with file
// Self::new_from_default(&d).unwrap_or(d) let from_file = Self::new_from_default(&default_settings);
let from_file = Self::new_from_default(&d);
match from_file { match from_file {
Ok(f) => f, Ok(f) => f,
Err(e) => { Err(e) => {
warn!("Error reading config file ({:?})", e); warn!("Error reading config file ({:?})", e);
d default_settings
} }
} }
} }
fn new_from_default(default: &Settings) -> Result<Self, config::ConfigError> { fn new_from_default(default: &Settings) -> Result<Self, ConfigError> {
// let config: config::Config = config::Config::new(); let builder = Config::builder();
let builder = config::Config::builder(); let config: Config = builder
let config: config::Config = builder
// use defaults // use defaults
.add_source(config::Config::try_from(default)?) .add_source(Config::try_from(default)?)
// override with file contents // override with file contents
.add_source(config::File::with_name("config")) .add_source(File::with_name("config"))
.build()? .build()?
.try_into() .try_into()
.unwrap(); .unwrap();