|
1 | 1 | //! Define Errors |
2 | 2 |
|
3 | 3 | use ndarray::{Ixs, ShapeError}; |
| 4 | +use std::error; |
| 5 | +use std::fmt; |
4 | 6 |
|
5 | 7 | pub type Result<T> = ::std::result::Result<T, LinalgError>; |
6 | 8 |
|
7 | 9 | /// Master Error type of this crate |
8 | | -#[derive(Fail, Debug)] |
| 10 | +#[derive(Debug)] |
9 | 11 | pub enum LinalgError { |
10 | 12 | /// Matrix is not square |
11 | | - #[fail(display = "Not square: rows({}) != cols({})", rows, cols)] |
12 | 13 | NotSquare { rows: i32, cols: i32 }, |
13 | | - |
14 | 14 | /// LAPACK subroutine returns non-zero code |
15 | | - #[fail(display = "LAPACK: return_code = {}", return_code)] |
16 | | - LapackFailure { return_code: i32 }, |
17 | | - |
| 15 | + Lapack { return_code: i32 }, |
18 | 16 | /// Strides of the array is not supported |
19 | | - #[fail(display = "invalid stride: s0={}, s1={}", s0, s1)] |
20 | 17 | InvalidStride { s0: Ixs, s1: Ixs }, |
21 | | - |
22 | 18 | /// Memory is not aligned continously |
23 | | - #[fail(display = "Memory is not contiguous")] |
24 | | - MemoryNotCont {}, |
25 | | - |
| 19 | + MemoryNotCont, |
26 | 20 | /// Strides of the array is not supported |
27 | | - #[fail(display = "Shape Error: {}", error)] |
28 | | - ShapeFailure { error: ShapeError }, |
| 21 | + Shape(ShapeError), |
| 22 | +} |
| 23 | + |
| 24 | +impl fmt::Display for LinalgError { |
| 25 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 26 | + match self { |
| 27 | + LinalgError::NotSquare { rows, cols } => write!(f, "Not square: rows({}) != cols({})", rows, cols), |
| 28 | + LinalgError::Lapack { return_code } => write!(f, "LAPACK: return_code = {}", return_code), |
| 29 | + LinalgError::InvalidStride { s0, s1 } => write!(f, "invalid stride: s0={}, s1={}", s0, s1), |
| 30 | + LinalgError::MemoryNotCont => write!(f, "Memory is not contiguous"), |
| 31 | + LinalgError::Shape(err) => write!(f, "Shape Error: {}", err), |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl error::Error for LinalgError { |
| 37 | + fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
| 38 | + match self { |
| 39 | + LinalgError::Shape(err) => Some(err), |
| 40 | + _ => None, |
| 41 | + } |
| 42 | + } |
29 | 43 | } |
30 | 44 |
|
31 | 45 | impl From<ShapeError> for LinalgError { |
32 | 46 | fn from(error: ShapeError) -> LinalgError { |
33 | | - LinalgError::ShapeFailure { error } |
| 47 | + LinalgError::Shape(error) |
34 | 48 | } |
35 | 49 | } |
0 commit comments