#[cfg(feature = "std")]
use thiserror::Error;
#[cfg(not(feature = "std"))]
use core::fmt;
#[cfg(feature = "std")]
#[derive(Error, Debug, Clone, PartialEq)]
pub enum CoreError {
#[error("Invalid dimension: expected {expected}, got {actual}")]
InvalidDimension { expected: usize, actual: usize },
#[error("Division by zero")]
DivisionByZero,
#[error("Numerical instability detected")]
NumericalInstability,
#[error("Invalid basis vector index: {0} (max: {1})")]
InvalidBasisIndex(usize, usize),
#[error("Matrix is singular and cannot be inverted")]
SingularMatrix,
#[error("Invalid metric signature: positive {positive} + negative {negative} + zero {zero} != dimension {dimension}")]
InvalidSignature {
positive: usize,
negative: usize,
zero: usize,
dimension: usize,
},
#[error("GF(2) dimension mismatch: expected {expected}, got {got}")]
GF2DimensionMismatch { expected: usize, got: usize },
#[error("GF(2) matrix is not square: {rows}x{cols}")]
GF2NotSquare { rows: usize, cols: usize },
#[error("GF(2) index {index} out of bounds for dimension {dim}")]
GF2IndexOutOfBounds { index: usize, dim: usize },
}
#[cfg(not(feature = "std"))]
#[derive(Debug, Clone, PartialEq)]
pub enum CoreError {
InvalidDimension { expected: usize, actual: usize },
DivisionByZero,
NumericalInstability,
InvalidBasisIndex(usize, usize),
SingularMatrix,
InvalidSignature {
positive: usize,
negative: usize,
zero: usize,
dimension: usize,
},
GF2DimensionMismatch { expected: usize, got: usize },
GF2NotSquare { rows: usize, cols: usize },
GF2IndexOutOfBounds { index: usize, dim: usize },
}
#[cfg(not(feature = "std"))]
impl fmt::Display for CoreError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CoreError::InvalidDimension { expected, actual } => {
write!(
f,
"Invalid dimension: expected {}, got {}",
expected, actual
)
}
CoreError::DivisionByZero => {
write!(f, "Division by zero")
}
CoreError::NumericalInstability => {
write!(f, "Numerical instability detected")
}
CoreError::InvalidBasisIndex(idx, max) => {
write!(f, "Invalid basis vector index: {} (max: {})", idx, max)
}
CoreError::SingularMatrix => {
write!(f, "Matrix is singular and cannot be inverted")
}
CoreError::InvalidSignature {
positive,
negative,
zero,
dimension,
} => {
write!(
f,
"Invalid metric signature: positive {} + negative {} + zero {} != dimension {}",
positive, negative, zero, dimension
)
}
CoreError::GF2DimensionMismatch { expected, got } => {
write!(
f,
"GF(2) dimension mismatch: expected {}, got {}",
expected, got
)
}
CoreError::GF2NotSquare { rows, cols } => {
write!(f, "GF(2) matrix is not square: {}x{}", rows, cols)
}
CoreError::GF2IndexOutOfBounds { index, dim } => {
write!(
f,
"GF(2) index {} out of bounds for dimension {}",
index, dim
)
}
}
}
}
pub type CoreResult<T> = Result<T, CoreError>;