quantrs2_symengine_pure/
error.rs1use std::fmt;
7use thiserror::Error;
8
9pub type SymEngineResult<T> = Result<T, SymEngineError>;
11
12#[derive(Error, Debug, Clone)]
14#[non_exhaustive]
15pub enum SymEngineError {
16 #[error("Parse error: {0}")]
18 ParseError(String),
19
20 #[error("Invalid operation: {0}")]
22 InvalidOperation(String),
23
24 #[error("Division by zero")]
26 DivisionByZero,
27
28 #[error("Undefined result: {0}")]
30 Undefined(String),
31
32 #[error("Type mismatch: expected {expected}, got {actual}")]
34 TypeMismatch { expected: String, actual: String },
35
36 #[error("Invalid symbol name: {0}")]
38 InvalidSymbol(String),
39
40 #[error("Matrix dimension mismatch: {0}")]
42 DimensionMismatch(String),
43
44 #[error("Evaluation error: {0}")]
46 EvaluationError(String),
47
48 #[error("Differentiation error: {0}")]
50 DifferentiationError(String),
51
52 #[error("Simplification error: {0}")]
54 SimplificationError(String),
55
56 #[error("Serialization error: {0}")]
58 SerializationError(String),
59
60 #[error("Internal error: {0}")]
62 InternalError(String),
63
64 #[error("Not implemented: {0}")]
66 NotImplemented(String),
67
68 #[error("Quantum error: {0}")]
70 QuantumError(String),
71}
72
73impl SymEngineError {
74 #[must_use]
76 pub fn parse<S: Into<String>>(msg: S) -> Self {
77 Self::ParseError(msg.into())
78 }
79
80 #[must_use]
82 pub fn invalid_op<S: Into<String>>(msg: S) -> Self {
83 Self::InvalidOperation(msg.into())
84 }
85
86 #[must_use]
88 pub fn eval<S: Into<String>>(msg: S) -> Self {
89 Self::EvaluationError(msg.into())
90 }
91
92 #[must_use]
94 pub fn diff<S: Into<String>>(msg: S) -> Self {
95 Self::DifferentiationError(msg.into())
96 }
97
98 #[must_use]
100 pub fn not_impl<S: Into<String>>(msg: S) -> Self {
101 Self::NotImplemented(msg.into())
102 }
103
104 #[must_use]
106 pub fn quantum<S: Into<String>>(msg: S) -> Self {
107 Self::QuantumError(msg.into())
108 }
109
110 #[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 #[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}