refactor Conf to be object oriented

This commit is contained in:
Marc Planard 2023-06-04 12:08:44 +02:00
parent 49830795e6
commit ec15004085
3 changed files with 16 additions and 13 deletions

View file

@ -16,11 +16,14 @@ pub struct Conf {
pub dac_id: Option<u8>,
pub dac_url: Option<String>,
}
pub fn load_config(path: &str) -> Result<Conf, Box<dyn std::error::Error>> {
let settings = Config::builder()
.add_source(config::File::with_name(path))
.build()?;
let conf : Conf = settings.try_deserialize()?;
Ok(conf)
impl Conf {
pub fn new(path: &str) -> Result<Conf, Box<dyn std::error::Error>> {
let settings = Config::builder()
.add_source(config::File::with_name(path))
.build()?;
let conf : Conf = settings.try_deserialize()?;
Ok(conf)
}
}

View file

@ -18,7 +18,7 @@ use helios_dac::{
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use redis_ctrl::{RedisCtrl,Order};
use conf::{load_config,Conf};
use conf::Conf;
use errors::LJError;
const CENTER : (u16,u16) = (2000, 2000);
@ -36,7 +36,7 @@ fn run_all() -> Result<(), Box<dyn std::error::Error>> {
let Some(filename) = std::env::args().nth(1) else {
return Err(Box::new(LJError::ConfigFileMissing));
};
let config = load_config(&filename)?;
let config = Conf::new(&filename)?;
let mut rs = RedisCtrl::new()?;
let running = Arc::new(AtomicBool::new(true));