1#[cfg(feature = "std")]
4use thiserror::Error;
5
6#[cfg(not(feature = "std"))]
7use core::fmt;
8
9#[cfg(feature = "std")]
11#[derive(Error, Debug, Clone, PartialEq)]
12pub enum CoreError {
13 #[error("Invalid dimension: expected {expected}, got {actual}")]
15 InvalidDimension { expected: usize, actual: usize },
16
17 #[error("Division by zero")]
19 DivisionByZero,
20
21 #[error("Numerical instability detected")]
23 NumericalInstability,
24
25 #[error("Invalid basis vector index: {0} (max: {1})")]
27 InvalidBasisIndex(usize, usize),
28
29 #[error("Matrix is singular and cannot be inverted")]
31 SingularMatrix,
32
33 #[error("Invalid metric signature: positive {positive} + negative {negative} + zero {zero} != dimension {dimension}")]
35 InvalidSignature {
36 positive: usize,
37 negative: usize,
38 zero: usize,
39 dimension: usize,
40 },
41
42 #[error("GF(2) dimension mismatch: expected {expected}, got {got}")]
44 GF2DimensionMismatch { expected: usize, got: usize },
45
46 #[error("GF(2) matrix is not square: {rows}x{cols}")]
48 GF2NotSquare { rows: usize, cols: usize },
49
50 #[error("GF(2) index {index} out of bounds for dimension {dim}")]
52 GF2IndexOutOfBounds { index: usize, dim: usize },
53}
54
55#[cfg(not(feature = "std"))]
57#[derive(Debug, Clone, PartialEq)]
58pub enum CoreError {
59 InvalidDimension { expected: usize, actual: usize },
61
62 DivisionByZero,
64
65 NumericalInstability,
67
68 InvalidBasisIndex(usize, usize),
70
71 SingularMatrix,
73
74 InvalidSignature {
76 positive: usize,
77 negative: usize,
78 zero: usize,
79 dimension: usize,
80 },
81
82 GF2DimensionMismatch { expected: usize, got: usize },
84
85 GF2NotSquare { rows: usize, cols: usize },
87
88 GF2IndexOutOfBounds { index: usize, dim: usize },
90}
91
92#[cfg(not(feature = "std"))]
93impl fmt::Display for CoreError {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 match self {
96 CoreError::InvalidDimension { expected, actual } => {
97 write!(
98 f,
99 "Invalid dimension: expected {}, got {}",
100 expected, actual
101 )
102 }
103 CoreError::DivisionByZero => {
104 write!(f, "Division by zero")
105 }
106 CoreError::NumericalInstability => {
107 write!(f, "Numerical instability detected")
108 }
109 CoreError::InvalidBasisIndex(idx, max) => {
110 write!(f, "Invalid basis vector index: {} (max: {})", idx, max)
111 }
112 CoreError::SingularMatrix => {
113 write!(f, "Matrix is singular and cannot be inverted")
114 }
115 CoreError::InvalidSignature {
116 positive,
117 negative,
118 zero,
119 dimension,
120 } => {
121 write!(
122 f,
123 "Invalid metric signature: positive {} + negative {} + zero {} != dimension {}",
124 positive, negative, zero, dimension
125 )
126 }
127 CoreError::GF2DimensionMismatch { expected, got } => {
128 write!(
129 f,
130 "GF(2) dimension mismatch: expected {}, got {}",
131 expected, got
132 )
133 }
134 CoreError::GF2NotSquare { rows, cols } => {
135 write!(f, "GF(2) matrix is not square: {}x{}", rows, cols)
136 }
137 CoreError::GF2IndexOutOfBounds { index, dim } => {
138 write!(
139 f,
140 "GF(2) index {} out of bounds for dimension {}",
141 index, dim
142 )
143 }
144 }
145 }
146}
147
148pub type CoreResult<T> = Result<T, CoreError>;