use std::fmt;
use thiserror::Error;
pub type SymEngineResult<T> = Result<T, SymEngineError>;
#[derive(Error, Debug, Clone)]
pub enum SymEngineError {
#[error("Parse error: {0}")]
ParseError(String),
#[error("Invalid operation: {0}")]
InvalidOperation(String),
#[error("Division by zero")]
DivisionByZero,
#[error("Undefined result: {0}")]
Undefined(String),
#[error("Type mismatch: expected {expected}, got {actual}")]
TypeMismatch { expected: String, actual: String },
#[error("Invalid symbol name: {0}")]
InvalidSymbol(String),
#[error("Matrix dimension mismatch: {0}")]
DimensionMismatch(String),
#[error("Evaluation error: {0}")]
EvaluationError(String),
#[error("Differentiation error: {0}")]
DifferentiationError(String),
#[error("Simplification error: {0}")]
SimplificationError(String),
#[error("Serialization error: {0}")]
SerializationError(String),
#[error("Internal error: {0}")]
InternalError(String),
#[error("Not implemented: {0}")]
NotImplemented(String),
#[error("Quantum error: {0}")]
QuantumError(String),
}
impl SymEngineError {
#[must_use]
pub fn parse<S: Into<String>>(msg: S) -> Self {
Self::ParseError(msg.into())
}
#[must_use]
pub fn invalid_op<S: Into<String>>(msg: S) -> Self {
Self::InvalidOperation(msg.into())
}
#[must_use]
pub fn eval<S: Into<String>>(msg: S) -> Self {
Self::EvaluationError(msg.into())
}
#[must_use]
pub fn diff<S: Into<String>>(msg: S) -> Self {
Self::DifferentiationError(msg.into())
}
#[must_use]
pub fn not_impl<S: Into<String>>(msg: S) -> Self {
Self::NotImplemented(msg.into())
}
#[must_use]
pub fn quantum<S: Into<String>>(msg: S) -> Self {
Self::QuantumError(msg.into())
}
#[must_use]
pub fn type_mismatch<S1: Into<String>, S2: Into<String>>(expected: S1, actual: S2) -> Self {
Self::TypeMismatch {
expected: expected.into(),
actual: actual.into(),
}
}
#[must_use]
pub fn dimension<S: Into<String>>(msg: S) -> Self {
Self::DimensionMismatch(msg.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_messages() {
let err = SymEngineError::parse("invalid syntax");
assert!(err.to_string().contains("Parse error"));
let err = SymEngineError::DivisionByZero;
assert!(err.to_string().contains("Division by zero"));
let err = SymEngineError::type_mismatch("Symbol", "Number");
assert!(err.to_string().contains("Symbol"));
assert!(err.to_string().contains("Number"));
}
}