Skip to content

Commit

Permalink
Use thiserror to derive errors (#9)
Browse files Browse the repository at this point in the history
Use a commonly accepted library to deal with errors. This does not
change the external API and simplifies the code.
  • Loading branch information
afnanenayet authored Dec 27, 2020
1 parent 1ecdf97 commit 5382048
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 59 deletions.
1 change: 1 addition & 0 deletions oars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
10 changes: 2 additions & 8 deletions oars/src/constructors/bose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,11 @@ impl<T: Integer> BoseChecked<T> {
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,
Expand Down
15 changes: 5 additions & 10 deletions oars/src/constructors/bush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,20 @@ impl<T: Integer> BushChecked<T> {
/// ```
pub fn verify(self) -> OarsResult<Bush<T>> {
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 {
Expand Down
15 changes: 6 additions & 9 deletions oars/src/oa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,14 @@ pub fn normalize<T: Integer, U: Float>(
randomize: bool,
) -> OarsResult<Array2<U>> {
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(),
));
}

Expand Down Expand Up @@ -150,9 +148,8 @@ pub fn verify<T: Integer>(oa: &OA<T>) -> OarsResult<bool>
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(),
));
}

Expand Down
37 changes: 5 additions & 32 deletions oars/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -88,45 +89,17 @@ 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`.
///
/// This type is meant for anything that isn't an orthogonal array constructor.
pub type OarsResult<T> = Result<T, OarsError>;

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<T>(kind: ErrorKind, msg: T) -> Self
where
T: Into<String>,
{
Self {
error_type: kind,
desc: msg.into(),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 5382048

Please sign in to comment.