Skip to main content

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