From 42773aa2a2f2bb2bd53c486a4382ce3c47268b2d Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Sat, 1 Jul 2023 14:44:51 +0200 Subject: [PATCH] add error handling --- src/errors.rs | 6 +++++- src/worldstate.rs | 10 ++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index ff40f33..1173dbe 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -9,7 +9,8 @@ pub type LJResult = Result>; pub enum LJError { Config(ConfigError), RedisConnect(RedisError), - HeliosDeviceMissing + HeliosDeviceMissing, + BadEDH } impl fmt::Display for LJError { @@ -26,6 +27,9 @@ impl fmt::Display for LJError { }, HeliosDeviceMissing => { write!(f, "helios device not found") + }, + BadEDH => { + write!(f, "EDH matrix is not a 3x3 matrix") } } } diff --git a/src/worldstate.rs b/src/worldstate.rs index 38b1bd0..d51536b 100644 --- a/src/worldstate.rs +++ b/src/worldstate.rs @@ -1,14 +1,20 @@ use crate::point::Color; use nalgebra::base::Matrix3; -use crate::errors::LJResult; +use crate::errors::{LJError,LJResult}; + #[derive(Debug, Default)] pub struct EDH { pub matrix: Matrix3 } - impl EDH { pub fn new(vec: Vec>) -> LJResult { + if vec.len() != 3 || + vec[0].len() != 3 || + vec[1].len() != 3 || + vec[2].len() != 3 { + return Err(Box::new(LJError::BadEDH)); + } let matrix = Matrix3::new(vec[0][0], vec[0][1], vec[0][2], vec[1][0], vec[1][1], vec[1][2], vec[2][0], vec[2][1], vec[2][2]);