1#[derive(PartialEq, Eq, Debug, Copy, Clone)]
4pub enum StructureError {
5 Unsorted(&'static str),
6 SizeMismatch(&'static str),
7 OutOfRange(&'static str),
8}
9
10#[derive(PartialEq, Eq, Debug, Copy, Clone)]
11#[non_exhaustive]
12pub enum StructureErrorKind {
13 Unsorted,
14 SizeMismatch,
15 OutOfRange,
16}
17
18impl StructureError {
19 pub fn kind(&self) -> StructureErrorKind {
20 match self {
21 StructureError::Unsorted(_) => StructureErrorKind::Unsorted,
22 StructureError::SizeMismatch(_) => StructureErrorKind::SizeMismatch,
23 StructureError::OutOfRange(_) => StructureErrorKind::OutOfRange,
24 }
25 }
26
27 fn kind_str(&self) -> &str {
28 match self {
29 StructureError::Unsorted(_) => "unsorted",
30 StructureError::SizeMismatch(_) => "size mismatch",
31 StructureError::OutOfRange(_) => "out of range",
32 }
33 }
34
35 fn msg(&self) -> &str {
36 match self {
37 StructureError::Unsorted(s)
38 | StructureError::SizeMismatch(s)
39 | StructureError::OutOfRange(s) => s,
40 }
41 }
42}
43
44impl std::fmt::Display for StructureError {
45 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
46 write!(f, "Structure Error ({}): {}", self.kind_str(), self.msg())
47 }
48}
49
50impl std::error::Error for StructureError {}
51
52#[derive(PartialEq, Eq, Debug, Copy, Clone)]
53pub struct ShapeMismatchInfo {
54 pub expected: (usize, usize),
55 pub received: (usize, usize),
56}
57
58#[derive(PartialEq, Eq, Debug, Copy, Clone)]
59pub struct SingularMatrixInfo {
60 pub index: usize,
61 pub reason: &'static str,
62}
63
64#[derive(PartialEq, Eq, Debug, Clone)]
65#[non_exhaustive]
66pub enum LinalgError {
67 ShapeMismatch(ShapeMismatchInfo),
68 NonSquareMatrix,
69 SingularMatrix(SingularMatrixInfo),
70 ThirdPartyError(isize, &'static str),
71}
72
73impl std::fmt::Display for LinalgError {
74 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
75 match self {
76 LinalgError::ShapeMismatch(shapes) => {
77 write!(
78 f,
79 "Shape mismatch: expected ({}, {}), got ({}, {})",
80 shapes.expected.0,
81 shapes.expected.1,
82 shapes.received.0,
83 shapes.received.1,
84 )
85 }
86 LinalgError::NonSquareMatrix => write!(f, "Non square matrix"),
87 LinalgError::SingularMatrix(info) => {
88 write!(
89 f,
90 "Singular matrix at index {} ({})",
91 info.index, info.reason,
92 )
93 }
94 LinalgError::ThirdPartyError(code, msg) => {
95 write!(f, "Third party error: {msg} (code {code})",)
96 }
97 }
98 }
99}
100
101impl std::error::Error for LinalgError {}
102
103#[derive(PartialEq, Eq, Debug, Clone)]
107#[non_exhaustive]
108pub enum SprsError {
109 Structure(StructureError),
110 Linalg(LinalgError),
111}
112
113impl From<StructureError> for SprsError {
114 fn from(e: StructureError) -> Self {
115 Self::Structure(e)
116 }
117}
118
119impl From<LinalgError> for SprsError {
120 fn from(e: LinalgError) -> Self {
121 Self::Linalg(e)
122 }
123}
124
125impl std::fmt::Display for SprsError {
126 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
127 match self {
128 Self::Structure(e) => write!(f, "Structure error: {e}"),
129 Self::Linalg(e) => write!(f, "Linalg error: {e}"),
130 }
131 }
132}
133
134impl std::error::Error for SprsError {}