start to implement proper custom errors
This commit is contained in:
parent
263ccf3210
commit
b4766a3301
@ -25,8 +25,7 @@ pub fn load_config( path : &str )-> Result<Conf, Box<dyn std::error::Error>> {
|
||||
.add_source(config::File::with_name(path))
|
||||
.build()?;
|
||||
|
||||
let conf = settings
|
||||
.try_deserialize::<Conf>()?;
|
||||
let conf : Conf = settings.try_deserialize()?;
|
||||
|
||||
Ok(conf)
|
||||
}
|
||||
|
33
src/errors.rs
Normal file
33
src/errors.rs
Normal file
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
51
src/main.rs
51
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<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 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<Frame, Box<dyn std::error::Error>> {
|
||||
|
||||
|
||||
let line = rs.get("/pl/0/0")?;
|
||||
let line : Vec<Point> = 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);
|
||||
|
Loading…
Reference in New Issue
Block a user