diff --git a/oars/Cargo.toml b/oars/Cargo.toml index 8a44225..1372c0e 100644 --- a/oars/Cargo.toml +++ b/oars/Cargo.toml @@ -24,6 +24,7 @@ serde = { version = "1.0", optional = true } serde_derive = { version = "1.0", optional = true } rayon = { version = "1.3", optional = true } oars_proc_macro = { version = "0.1.1", path = "../oars_proc_macro" } +thiserror = "1.0" [features] default = [] diff --git a/oars/src/constructors/bose.rs b/oars/src/constructors/bose.rs index 3557431..5b8cfe3 100644 --- a/oars/src/constructors/bose.rs +++ b/oars/src/constructors/bose.rs @@ -41,17 +41,11 @@ impl BoseChecked { if self.dimensions < T::from(2).unwrap() || self.dimensions > self.prime_base + T::from(1).unwrap() { - return Err(OarsError::new( - ErrorKind::InvalidParams, - "Invalid dimensions", - )); + return Err(OarsError::InvalidParams("Invalid dimensions".into())); } if !is_prime(self.prime_base.to_u64().unwrap()) { - return Err(OarsError::new( - ErrorKind::InvalidParams, - "Base is not prime", - )); + return Err(OarsError::InvalidParams("Base is not prime".into())); } Ok(Bose { prime_base: self.prime_base, diff --git a/oars/src/constructors/bush.rs b/oars/src/constructors/bush.rs index 7054448..da92124 100644 --- a/oars/src/constructors/bush.rs +++ b/oars/src/constructors/bush.rs @@ -38,25 +38,20 @@ impl BushChecked { /// ``` pub fn verify(self) -> OarsResult> { if !is_prime(self.prime_base.to_u64().unwrap()) { - return Err(OarsError::new( - ErrorKind::InvalidParams, - "Base is not prime", - )); + return Err(OarsError::InvalidParams("Base is not prime".to_owned())); } if self.dimensions < T::from(2).unwrap() || self.dimensions > self.prime_base + T::from(1).unwrap() { - return Err(OarsError::new( - ErrorKind::InvalidParams, - "Dimensions must be less than `prime_base` + 1", + return Err(OarsError::InvalidParams( + "Dimensions must be less than `prime_base` + 1".to_owned(), )); } if self.strength < T::from(1).unwrap() || self.strength > self.prime_base { - return Err(OarsError::new( - ErrorKind::InvalidParams, - "`strength` must be between 1 and `prime_base` (inclusive)", + return Err(OarsError::InvalidParams( + "`strength` must be between 1 and `prime_base` (inclusive)".to_owned(), )); } Ok(Bush { diff --git a/oars/src/oa.rs b/oars/src/oa.rs index 4f43de2..7435f81 100644 --- a/oars/src/oa.rs +++ b/oars/src/oa.rs @@ -88,16 +88,14 @@ pub fn normalize( randomize: bool, ) -> OarsResult> { if oa.points.ndim() != 2 { - return Err(OarsError::new( - ErrorKind::InvalidParams, - "The `points` array in `oa` must be two dimensional", + return Err(OarsError::InvalidParams( + "The `points` array in `oa` must be two dimensional".to_owned(), )); } if jitter.to_f64().unwrap() < 0.0 || jitter.to_f64().unwrap() > 1.0 { - return Err(OarsError::new( - ErrorKind::InvalidParams, - "`jitter` must be between 0 and 1", + return Err(OarsError::InvalidParams( + "`jitter` must be between 0 and 1".to_owned(), )); } @@ -150,9 +148,8 @@ pub fn verify(oa: &OA) -> OarsResult where { if oa.points.ndim() != 2 { - return Err(OarsError::new( - ErrorKind::InvalidParams, - "`oa.points` must be two-dimensional", + return Err(OarsError::InvalidParams( + "`oa.points` must be two-dimensional".to_owned(), )); } diff --git a/oars/src/utils.rs b/oars/src/utils.rs index b7a8027..652f7ae 100644 --- a/oars/src/utils.rs +++ b/oars/src/utils.rs @@ -4,6 +4,7 @@ use num::{self, NumCast}; use std::error::Error; use std::fmt; use std::vec::Vec; +use thiserror::Error; /// A generic integer type. /// @@ -88,14 +89,10 @@ pub enum ErrorKind { } /// An error indicating that there was some error constructing the orthogonal array. -#[derive(Debug)] -pub struct OarsError { - /// The general category of the error - error_type: ErrorKind, - - /// A user-friendly description of the array which can supply additional information about - /// the error. - desc: String, +#[derive(Debug, Error)] +pub enum OarsError { + #[error("Invalid params supplied to the constructor: {0}")] + InvalidParams(String), } /// A generic type for anything that can return an `OarsError`. @@ -103,30 +100,6 @@ pub struct OarsError { /// This type is meant for anything that isn't an orthogonal array constructor. pub type OarsResult = Result; -impl Error for OarsError { - fn description(&self) -> &str { - &self.desc - } -} - -impl fmt::Display for OarsError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "OA Construction Error: {}", &self.desc) - } -} - -impl OarsError { - pub fn new(kind: ErrorKind, msg: T) -> Self - where - T: Into, - { - Self { - error_type: kind, - desc: msg.into(), - } - } -} - #[cfg(test)] mod tests { use super::*;