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"
redis_url = "127.0.0.1"
dac_family = "Helios"

View File

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

View File

@ -16,15 +16,39 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use redis_ctrl::{RedisCtrl,Order};
use conf::{load_config};
use conf::{load_config,Conf};
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 r = running.clone();
@ -32,15 +56,6 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
r.store(false, Ordering::SeqCst);
}).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 devices = controller.list_devices()?;