quantrs2_symengine_pure/
error.rs

1//! Error types for the symbolic mathematics library.
2//!
3//! This module provides error types that follow the "No unwrap policy"
4//! ensuring all fallible operations return proper Result types.
5
6use std::fmt;
7use thiserror::Error;
8
9/// Result type alias for symbolic operations
10pub type SymEngineResult<T> = Result<T, SymEngineError>;
11
12/// Errors that can occur during symbolic computation
13#[derive(Error, Debug, Clone)]
14pub enum SymEngineError {
15    /// Failed to parse an expression from a string
16    #[error("Parse error: {0}")]
17    ParseError(String),
18
19    /// Invalid operation on expressions
20    #[error("Invalid operation: {0}")]
21    InvalidOperation(String),
22
23    /// Division by zero in symbolic computation
24    #[error("Division by zero")]
25    DivisionByZero,
26
27    /// Undefined result (e.g., 0/0, inf - inf)
28    #[error("Undefined result: {0}")]
29    Undefined(String),
30
31    /// Type mismatch in expression
32    #[error("Type mismatch: expected {expected}, got {actual}")]
33    TypeMismatch { expected: String, actual: String },
34
35    /// Invalid symbol name
36    #[error("Invalid symbol name: {0}")]
37    InvalidSymbol(String),
38
39    /// Matrix dimension mismatch
40    #[error("Matrix dimension mismatch: {0}")]
41    DimensionMismatch(String),
42
43    /// Evaluation error (e.g., undefined variable)
44    #[error("Evaluation error: {0}")]
45    EvaluationError(String),
46
47    /// Differentiation error
48    #[error("Differentiation error: {0}")]
49    DifferentiationError(String),
50
51    /// Simplification failed
52    #[error("Simplification error: {0}")]
53    SimplificationError(String),
54
55    /// Serialization/deserialization error
56    #[error("Serialization error: {0}")]
57    SerializationError(String),
58
59    /// Internal error (should not happen in normal operation)
60    #[error("Internal error: {0}")]
61    InternalError(String),
62
63    /// Feature not yet implemented
64    #[error("Not implemented: {0}")]
65    NotImplemented(String),
66
67    /// Quantum-specific error
68    #[error("Quantum error: {0}")]
69    QuantumError(String),
70}
71
72impl SymEngineError {
73    /// Create a parse error with the given message
74    #[must_use]
75    pub fn parse<S: Into<String>>(msg: S) -> Self {
76        Self::ParseError(msg.into())
77    }
78
79    /// Create an invalid operation error
80    #[must_use]
81    pub fn invalid_op<S: Into<String>>(msg: S) -> Self {
82        Self::InvalidOperation(msg.into())
83    }
84
85    /// Create an evaluation error
86    #[must_use]
87    pub fn eval<S: Into<String>>(msg: S) -> Self {
88        Self::EvaluationError(msg.into())
89    }
90
91    /// Create a differentiation error
92    #[must_use]
93    pub fn diff<S: Into<String>>(msg: S) -> Self {
94        Self::DifferentiationError(msg.into())
95    }
96
97    /// Create a not implemented error
98    #[must_use]
99    pub fn not_impl<S: Into<String>>(msg: S) -> Self {
100        Self::NotImplemented(msg.into())
101    }
102
103    /// Create a quantum error
104    #[must_use]
105    pub fn quantum<S: Into<String>>(msg: S) -> Self {
106        Self::QuantumError(msg.into())
107    }
108
109    /// Create a type mismatch error
110    #[must_use]
111    pub fn type_mismatch<S1: Into<String>, S2: Into<String>>(expected: S1, actual: S2) -> Self {
112        Self::TypeMismatch {
113            expected: expected.into(),
114            actual: actual.into(),
115        }
116    }
117
118    /// Create a dimension mismatch error
119    #[must_use]
120    pub fn dimension<S: Into<String>>(msg: S) -> Self {
121        Self::DimensionMismatch(msg.into())
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128
129    #[test]
130    fn test_error_messages() {
131        let err = SymEngineError::parse("invalid syntax");
132        assert!(err.to_string().contains("Parse error"));
133
134        let err = SymEngineError::DivisionByZero;
135        assert!(err.to_string().contains("Division by zero"));
136
137        let err = SymEngineError::type_mismatch("Symbol", "Number");
138        assert!(err.to_string().contains("Symbol"));
139        assert!(err.to_string().contains("Number"));
140    }
141}