From b4766a3301a7b6a2a07ac7d13fcced90bb80f863 Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Sun, 4 Jun 2023 11:28:43 +0200 Subject: [PATCH] start to implement proper custom errors --- src/conf.rs | 3 +-- src/errors.rs | 33 +++++++++++++++++++++++++++++++++ src/main.rs | 51 +++++++++++++++++++++++++-------------------------- 3 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 src/errors.rs diff --git a/src/conf.rs b/src/conf.rs index 7cbf585..f7768de 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -25,8 +25,7 @@ pub fn load_config( path : &str )-> Result> { .add_source(config::File::with_name(path)) .build()?; - let conf = settings - .try_deserialize::()?; + let conf : Conf = settings.try_deserialize()?; Ok(conf) } diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..b3a4a3a --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,33 @@ +use std::error::Error; +use std::fmt; + +use config::ConfigError; + +#[derive(Debug)] +pub enum LJError { + ConfigFileMissing, + +} + +impl fmt::Display for LJError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use LJError::*; + + match self { + ConfigFileMissing => { + write!(f, "first argument must be a TOML config file \ + (see copyme.Settings.toml)") + }, + } + } +} + +impl Error for LJError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + use LJError::*; + + match self { + ConfigFileMissing => None, + } + } +} diff --git a/src/main.rs b/src/main.rs index e1a22a5..c386386 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod redis_ctrl; mod conf; +mod errors; use helios_dac::{Frame, Point, @@ -17,34 +18,31 @@ use std::sync::Arc; use redis_ctrl::{RedisCtrl,Order}; use conf::{load_config,Conf}; - +use errors::LJError; const CENTER : (u16,u16) = (2000, 2000); pub fn main() { - - 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) { + match run_all() { Ok(()) => {}, Err(err) => { - panic!("Error: {:?}", err) + println!("Error: {}", err); } } } +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 rs = RedisCtrl::new()?; + + run_dac(config, rs) +} + fn run_dac( config: Conf, mut rs: RedisCtrl @@ -54,7 +52,7 @@ fn run_dac( ctrlc::set_handler(move || { r.store(false, Ordering::SeqCst); - }).expect("Error setting Ctrl-C handler"); + })?; let controller = NativeHeliosDacController::new()?; let devices = controller.list_devices()?; @@ -63,7 +61,7 @@ fn run_dac( println!("Unable to find an helios device"); return Ok(()); }; - + let mut device = device.open()?; while running.load(Ordering::SeqCst) { @@ -72,7 +70,7 @@ fn run_dac( println!("{:?}", order); } - let frame = get_next_frame(1000, &mut rs)?; + let frame = get_next_frame(1000, &mut rs, order == Order::Black)?; while let Ok(DeviceStatus::NotReady) = device.status() { } @@ -86,9 +84,10 @@ fn run_dac( fn get_next_frame( speed: u32, - rs: &mut RedisCtrl + rs: &mut RedisCtrl, + black: bool ) -> Result> { - + let line = rs.get("/pl/0/0")?; let line : Vec = line.iter() .map(tuple_to_point) @@ -100,7 +99,7 @@ fn get_next_frame( line2.push(*p); } } - + println!("{:?}", line2); Ok(Frame::new(speed, line2)) } @@ -111,8 +110,8 @@ fn tuple_to_point(tpl: &(f32,f32,u32)) -> Point { let g = ((col >> 8) & 255) as u8 ; let b = (col & 255) as u8 ; - let x = CENTER.0 + *x as u16; - let y = CENTER.1 + *y as u16; + let x = CENTER.0 + *x as u16 * 2; + let y = CENTER.1 + *y as u16 * 2; if x >= 4096 || y >= 4096 { println!("WARN: coordinate out of range: {} {}", x, y);