use std::fmt;
#[derive(Debug, Clone)]
pub enum SparseError {
DimensionMismatch {
expected: (usize, usize),
got: (usize, usize),
context: String,
},
NotSquare {
dims: (usize, usize),
},
InvalidInput {
reason: String,
},
StructurallySingular {
column: usize,
},
NumericalSingularity {
pivot_index: usize,
value: f64,
},
AnalysisFailure {
reason: String,
},
IoError {
source: String,
path: String,
},
ParseError {
reason: String,
path: String,
line: Option<usize>,
},
MatrixNotFound {
name: String,
},
SolveBeforeFactor {
context: String,
},
}
impl fmt::Display for SparseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DimensionMismatch {
expected,
got,
context,
} => {
write!(
f,
"Dimension mismatch: expected {:?}, got {:?}. {}",
expected, got, context
)
}
Self::NotSquare { dims } => {
write!(
f,
"Matrix must be square, but has dimensions {}x{}",
dims.0, dims.1
)
}
Self::InvalidInput { reason } => {
write!(f, "Invalid input: {}", reason)
}
Self::StructurallySingular { column } => {
write!(f, "Matrix is structurally singular at column {}", column)
}
Self::NumericalSingularity { pivot_index, value } => {
write!(
f,
"Numerical singularity at pivot index {} (value: {:.2e})",
pivot_index, value
)
}
Self::AnalysisFailure { reason } => {
write!(f, "Symbolic analysis failed: {}", reason)
}
Self::IoError { source, path } => {
write!(f, "IO error for '{}': {}", path, source)
}
Self::ParseError { reason, path, line } => {
if let Some(line) = line {
write!(f, "Parse error in '{}' at line {}: {}", path, line, reason)
} else {
write!(f, "Parse error in '{}': {}", path, reason)
}
}
Self::MatrixNotFound { name } => {
write!(f, "Matrix '{}' not found in registry", name)
}
Self::SolveBeforeFactor { context } => {
write!(f, "Solve called before factor(): {}", context)
}
}
}
}
impl std::error::Error for SparseError {}