|
1 | 1 | //! Define Errors |
2 | 2 |
|
3 | 3 | use ndarray::{Ixs, ShapeError}; |
4 | | -use std::error; |
5 | | -use std::fmt; |
6 | 4 |
|
7 | 5 | pub type Result<T> = ::std::result::Result<T, LinalgError>; |
8 | 6 |
|
9 | 7 | /// Master Error type of this crate |
10 | | -#[derive(Debug, EnumError)] |
| 8 | +#[derive(Fail, Debug)] |
11 | 9 | pub enum LinalgError { |
12 | | - NotSquare(NotSquareError), |
13 | | - Lapack(LapackError), |
14 | | - Stride(StrideError), |
15 | | - MemoryCont(MemoryContError), |
16 | | - Shape(ShapeError), |
17 | | -} |
18 | | - |
19 | | -/// Error from LAPACK |
20 | | -#[derive(Debug, new)] |
21 | | -pub struct LapackError { |
22 | | - pub return_code: i32, |
23 | | -} |
24 | | - |
25 | | -impl fmt::Display for LapackError { |
26 | | - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
27 | | - write!(f, "LAPACK: return_code = {}", self.return_code) |
28 | | - } |
29 | | -} |
30 | | - |
31 | | -impl error::Error for LapackError { |
32 | | - fn description(&self) -> &str { |
33 | | - "LAPACK subroutine returns non-zero code" |
34 | | - } |
35 | | -} |
| 10 | + /// Matrix is not square |
| 11 | + #[fail(display = "Not square: rows({}) != cols({})", rows, cols)] |
| 12 | + NotSquare { rows: i32, cols: i32 }, |
36 | 13 |
|
37 | | -impl From<i32> for LapackError { |
38 | | - fn from(code: i32) -> LapackError { |
39 | | - LapackError { return_code: code } |
40 | | - } |
41 | | -} |
| 14 | + /// LAPACK subroutine returns non-zero code |
| 15 | + #[fail(display = "LAPACK: return_code = {}", return_code)] |
| 16 | + LapackFailure { return_code: i32 }, |
42 | 17 |
|
43 | | -/// Error that matrix is not square |
44 | | -#[derive(Debug, new)] |
45 | | -pub struct NotSquareError { |
46 | | - pub rows: i32, |
47 | | - pub cols: i32, |
48 | | -} |
| 18 | + /// Strides of the array is not supported |
| 19 | + #[fail(display = "invalid stride: s0={}, s1={}", s0, s1)] |
| 20 | + InvalidStride { s0: Ixs, s1: Ixs }, |
49 | 21 |
|
50 | | -impl fmt::Display for NotSquareError { |
51 | | - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
52 | | - write!(f, "Not square: rows({}) != cols({})", self.rows, self.cols) |
53 | | - } |
54 | | -} |
| 22 | + /// Memory is not aligned continously |
| 23 | + #[fail(display = "Memory is not contiguous")] |
| 24 | + MemoryNotCont {}, |
55 | 25 |
|
56 | | -impl error::Error for NotSquareError { |
57 | | - fn description(&self) -> &str { |
58 | | - "Matrix is not square" |
59 | | - } |
60 | | -} |
61 | | - |
62 | | -/// Error that strides of the array is not supported |
63 | | -#[derive(Debug, new)] |
64 | | -pub struct StrideError { |
65 | | - pub s0: Ixs, |
66 | | - pub s1: Ixs, |
67 | | -} |
68 | | - |
69 | | -impl fmt::Display for StrideError { |
70 | | - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
71 | | - write!(f, "invalid stride: s0={}, s1={}", self.s0, self.s1) |
72 | | - } |
73 | | -} |
74 | | - |
75 | | -impl error::Error for StrideError { |
76 | | - fn description(&self) -> &str { |
77 | | - "invalid stride" |
78 | | - } |
79 | | -} |
80 | | - |
81 | | -/// Error that the memory is not aligned continously |
82 | | -#[derive(Debug, new)] |
83 | | -pub struct MemoryContError {} |
84 | | - |
85 | | -impl fmt::Display for MemoryContError { |
86 | | - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
87 | | - write!(f, "Memory is not contiguous") |
88 | | - } |
| 26 | + /// Strides of the array is not supported |
| 27 | + #[fail(display = "Shape Error: {}", error)] |
| 28 | + ShapeFailure { error: ShapeError }, |
89 | 29 | } |
90 | 30 |
|
91 | | -impl error::Error for MemoryContError { |
92 | | - fn description(&self) -> &str { |
93 | | - "Memory is not contiguous" |
| 31 | +impl From<ShapeError> for LinalgError { |
| 32 | + fn from(error: ShapeError) -> LinalgError { |
| 33 | + LinalgError::ShapeFailure { error } |
94 | 34 | } |
95 | 35 | } |
0 commit comments