refactor Conf to be object oriented
This commit is contained in:
parent
49830795e6
commit
ec15004085
15
src/conf.rs
15
src/conf.rs
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user