Skip to content

Use thiserror crate for implement Error type #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ num-traits = "0.2.11"
cauchy = "0.2.2"
num-complex = "0.2.4"
rand = "0.5"
thiserror = "1.0.20"

[dependencies.ndarray]
version = "0.13.0"
Expand Down
56 changes: 14 additions & 42 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,38 @@
//! Define Errors

use ndarray::{Ixs, ShapeError};
use std::error;
use std::fmt;
use thiserror::Error;

pub type Result<T> = ::std::result::Result<T, LinalgError>;

/// Master Error type of this crate
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum LinalgError {
/// Matrix is not square
#[error("Not square: rows({}) != cols({})", rows, cols)]
NotSquare { rows: i32, cols: i32 },

/// LAPACK subroutine returns non-zero code
#[error("LAPACK: return_code = {}", return_code)]
Lapack { return_code: i32 },

/// Strides of the array is not supported
#[error("invalid stride: s0={}, s1={}", s0, s1)]
InvalidStride { s0: Ixs, s1: Ixs },

/// Memory is not aligned continously
#[error("Memroy is not continously")]
MemoryNotCont,

/// Obj cannot be made from a (rows, cols) matrix
#[error("{} cannot be made from a ({}, {}) matrix", obj, rows, cols)]
NotStandardShape {
obj: &'static str,
rows: i32,
cols: i32,
},
/// Strides of the array is not supported
Shape(ShapeError),
}

impl fmt::Display for LinalgError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
LinalgError::NotSquare { rows, cols } => {
write!(f, "Not square: rows({}) != cols({})", rows, cols)
}
LinalgError::Lapack { return_code } => {
write!(f, "LAPACK: return_code = {}", return_code)
}
LinalgError::InvalidStride { s0, s1 } => {
write!(f, "invalid stride: s0={}, s1={}", s0, s1)
}
LinalgError::MemoryNotCont => write!(f, "Memory is not contiguous"),
LinalgError::NotStandardShape { obj, rows, cols } => write!(
f,
"{} cannot be made from a ({}, {}) matrix",
obj, rows, cols
),
LinalgError::Shape(err) => write!(f, "Shape Error: {}", err),
}
}
}

impl error::Error for LinalgError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
LinalgError::Shape(err) => Some(err),
_ => None,
}
}
}

impl From<ShapeError> for LinalgError {
fn from(error: ShapeError) -> LinalgError {
LinalgError::Shape(error)
}
/// Strides of the array is not supported
#[error(transparent)]
Shape(#[from] ShapeError),
}