refacto error handling

This commit is contained in:
Marc Planard 2023-06-03 18:11:55 +02:00
parent 6bcfc831a6
commit 263ccf3210
3 changed files with 34 additions and 34 deletions

View File

@ -1,4 +1,4 @@
laser_id = 1 laser_id = 0
debug = "true" debug = "true"
redis_url = "127.0.0.1" redis_url = "127.0.0.1"
dac_family = "Helios" dac_family = "Helios"

View File

@ -10,7 +10,7 @@ pub enum DacFamily {
} }
#[derive(Deserialize,Debug)] #[derive(Deserialize,Debug)]
pub struct Conf{ pub struct Conf {
laser_id : u8, laser_id : u8,
debug : bool, debug : bool,
redis_url : String, redis_url : String,
@ -21,27 +21,12 @@ pub struct Conf{
pub fn load_config( path : &str )-> Result<Conf, Box<dyn std::error::Error>> { pub fn load_config( path : &str )-> Result<Conf, Box<dyn std::error::Error>> {
let settings = Config::builder()
let conf_builder = Config::builder() .add_source(config::File::with_name(path))
.add_source(config::File::with_name(path)); .build()?;
let settings = match conf_builder.build() {
Ok(conf) => conf,
Err(err) => {
println!("Invalid configuration file / missing file: {:?}", err);
return Err(Box::new(err))
}
};
let conf = settings let conf = settings
.try_deserialize::<Conf>()?; .try_deserialize::<Conf>()?;
// .try_deserialize::<HashMap<String, String>>()?;
println!(
"{:?}",
conf
);
Ok(conf) Ok(conf)
} }

View File

@ -16,31 +16,46 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use redis_ctrl::{RedisCtrl,Order}; use redis_ctrl::{RedisCtrl,Order};
use conf::{load_config}; use conf::{load_config,Conf};
const CENTER : (u16,u16) = (2000, 2000); const CENTER : (u16,u16) = (2000, 2000);
pub fn main() -> Result<(), Box<dyn std::error::Error>> { pub fn main() {
let config = load_config("Settings")?; let config = match load_config("Settings") {
Ok(c) => c,
Err(err) => {
panic!("Unable to load config file: {:?}", err)
}
};
let rs = match RedisCtrl::new() {
Ok(rs) => rs,
Err(err) => {
panic!("Unable to connect to redis: {:?}", err);
}
};
match run_dac(config, rs) {
Ok(()) => {},
Err(err) => {
panic!("Error: {:?}", err)
}
}
}
fn run_dac(
config: Conf,
mut rs: RedisCtrl
) -> Result<(), Box<dyn std::error::Error>> {
let running = Arc::new(AtomicBool::new(true)); let running = Arc::new(AtomicBool::new(true));
let r = running.clone(); let r = running.clone();
ctrlc::set_handler(move || { ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst); r.store(false, Ordering::SeqCst);
}).expect("Error setting Ctrl-C handler"); }).expect("Error setting Ctrl-C handler");
let mut rs = match RedisCtrl::new() {
Ok(rs) => rs,
Err(err) => {
println!("Unable to connect to redis: {:?}", err);
return Ok(())
}
};
let controller = NativeHeliosDacController::new()?; let controller = NativeHeliosDacController::new()?;
let devices = controller.list_devices()?; let devices = controller.list_devices()?;