From ec150040851a28192e80b13c8e4479536f14cc4a Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Sun, 4 Jun 2023 12:08:44 +0200 Subject: [PATCH] refactor Conf to be object oriented --- src/conf.rs | 17 ++++++++++------- src/main.rs | 4 ++-- tests/test_conf.rs | 8 ++++---- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/conf.rs b/src/conf.rs index 2b449ec..ad1a1f3 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -16,11 +16,14 @@ pub struct Conf { pub dac_id: Option, pub dac_url: Option, } -pub fn load_config(path: &str) -> Result> { - 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> { + let settings = Config::builder() + .add_source(config::File::with_name(path)) + .build()?; + + let conf : Conf = settings.try_deserialize()?; + Ok(conf) + } } diff --git a/src/main.rs b/src/main.rs index ec5c507..2bef1f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { 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)); diff --git a/tests/test_conf.rs b/tests/test_conf.rs index a366a11..72dc44e 100644 --- a/tests/test_conf.rs +++ b/tests/test_conf.rs @@ -1,20 +1,20 @@ -use lj_rust::conf::{load_config, DacFamily}; +use lj_rust::conf::{Conf, DacFamily}; #[test] fn it_loads_a_valid_conf() { - let result = load_config("tests/settings/valid"); + let result = Conf::new("tests/settings/valid"); assert!(result.is_ok()); } #[test] fn it_fails_invalid_conf() { - let result = load_config("tests/settings/empty"); + let result = Conf::new("tests/settings/empty"); assert!(result.is_err()); } #[test] fn it_finds_struct_fields() { - let config = match load_config("tests/settings/valid") { + let config = match Conf::new("tests/settings/valid") { Ok(c) => c, Err(err) => { panic!("Unable to load config file: {:?}", err)