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