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))
|
.add_source(config::File::with_name(path))
|
||||||
.build()?;
|
.build()?;
|
||||||
|
|
||||||
let conf = settings
|
let conf : Conf = settings.try_deserialize()?;
|
||||||
.try_deserialize::<Conf>()?;
|
|
||||||
|
|
||||||
Ok(conf)
|
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 redis_ctrl;
|
||||||
mod conf;
|
mod conf;
|
||||||
|
mod errors;
|
||||||
|
|
||||||
use helios_dac::{Frame,
|
use helios_dac::{Frame,
|
||||||
Point,
|
Point,
|
||||||
@ -17,34 +18,31 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use redis_ctrl::{RedisCtrl,Order};
|
use redis_ctrl::{RedisCtrl,Order};
|
||||||
use conf::{load_config,Conf};
|
use conf::{load_config,Conf};
|
||||||
|
use errors::LJError;
|
||||||
|
|
||||||
const CENTER : (u16,u16) = (2000, 2000);
|
const CENTER : (u16,u16) = (2000, 2000);
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
match run_all() {
|
||||||
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(()) => {},
|
Ok(()) => {},
|
||||||
Err(err) => {
|
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(
|
fn run_dac(
|
||||||
config: Conf,
|
config: Conf,
|
||||||
mut rs: RedisCtrl
|
mut rs: RedisCtrl
|
||||||
@ -54,7 +52,7 @@ fn run_dac(
|
|||||||
|
|
||||||
ctrlc::set_handler(move || {
|
ctrlc::set_handler(move || {
|
||||||
r.store(false, Ordering::SeqCst);
|
r.store(false, Ordering::SeqCst);
|
||||||
}).expect("Error setting Ctrl-C handler");
|
})?;
|
||||||
|
|
||||||
let controller = NativeHeliosDacController::new()?;
|
let controller = NativeHeliosDacController::new()?;
|
||||||
let devices = controller.list_devices()?;
|
let devices = controller.list_devices()?;
|
||||||
@ -63,7 +61,7 @@ fn run_dac(
|
|||||||
println!("Unable to find an helios device");
|
println!("Unable to find an helios device");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut device = device.open()?;
|
let mut device = device.open()?;
|
||||||
|
|
||||||
while running.load(Ordering::SeqCst) {
|
while running.load(Ordering::SeqCst) {
|
||||||
@ -72,7 +70,7 @@ fn run_dac(
|
|||||||
println!("{:?}", order);
|
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() {
|
while let Ok(DeviceStatus::NotReady) = device.status() {
|
||||||
}
|
}
|
||||||
@ -86,9 +84,10 @@ fn run_dac(
|
|||||||
|
|
||||||
fn get_next_frame(
|
fn get_next_frame(
|
||||||
speed: u32,
|
speed: u32,
|
||||||
rs: &mut RedisCtrl
|
rs: &mut RedisCtrl,
|
||||||
|
black: bool
|
||||||
) -> Result<Frame, Box<dyn std::error::Error>> {
|
) -> Result<Frame, Box<dyn std::error::Error>> {
|
||||||
|
|
||||||
let line = rs.get("/pl/0/0")?;
|
let line = rs.get("/pl/0/0")?;
|
||||||
let line : Vec<Point> = line.iter()
|
let line : Vec<Point> = line.iter()
|
||||||
.map(tuple_to_point)
|
.map(tuple_to_point)
|
||||||
@ -100,7 +99,7 @@ fn get_next_frame(
|
|||||||
line2.push(*p);
|
line2.push(*p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
println!("{:?}", line2);
|
||||||
Ok(Frame::new(speed, 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 g = ((col >> 8) & 255) as u8 ;
|
||||||
let b = (col & 255) as u8 ;
|
let b = (col & 255) as u8 ;
|
||||||
|
|
||||||
let x = CENTER.0 + *x as u16;
|
let x = CENTER.0 + *x as u16 * 2;
|
||||||
let y = CENTER.1 + *y as u16;
|
let y = CENTER.1 + *y as u16 * 2;
|
||||||
|
|
||||||
if x >= 4096 || y >= 4096 {
|
if x >= 4096 || y >= 4096 {
|
||||||
println!("WARN: coordinate out of range: {} {}", x, y);
|
println!("WARN: coordinate out of range: {} {}", x, y);
|
||||||
|
Loading…
Reference in New Issue
Block a user