quantrs2_symengine_pure/
error.rs1use std::fmt;
7use thiserror::Error;
8
9pub type SymEngineResult<T> = Result<T, SymEngineError>;
11
12#[derive(Error, Debug, Clone)]
14pub enum SymEngineError {
15 #[error("Parse error: {0}")]
17 ParseError(String),
18
19 #[error("Invalid operation: {0}")]
21 InvalidOperation(String),
22
23 #[error("Division by zero")]
25 DivisionByZero,
26
27 #[error("Undefined result: {0}")]
29 Undefined(String),
30
31 #[error("Type mismatch: expected {expected}, got {actual}")]
33 TypeMismatch { expected: String, actual: String },
34
35 #[error("Invalid symbol name: {0}")]
37 InvalidSymbol(String),
38
39 #[error("Matrix dimension mismatch: {0}")]
41 DimensionMismatch(String),
42
43 #[error("Evaluation error: {0}")]
45 EvaluationError(String),
46
47 #[error("Differentiation error: {0}")]
49 DifferentiationError(String),
50
51 #[error("Simplification error: {0}")]
53 SimplificationError(String),
54
55 #[error("Serialization error: {0}")]
57 SerializationError(String),
58
59 #[error("Internal error: {0}")]
61 InternalError(String),
62
63 #[error("Not implemented: {0}")]
65 NotImplemented(String),
66
67 #[error("Quantum error: {0}")]
69 QuantumError(String),
70}
71
72impl SymEngineError {
73 #[must_use]
75 pub fn parse<S: Into<String>>(msg: S) -> Self {
76 Self::ParseError(msg.into())
77 }
78
79 #[must_use]
81 pub fn invalid_op<S: Into<String>>(msg: S) -> Self {
82 Self::InvalidOperation(msg.into())
83 }
84
85 #[must_use]
87 pub fn eval<S: Into<String>>(msg: S) -> Self {
88 Self::EvaluationError(msg.into())
89 }
90
91 #[must_use]
93 pub fn diff<S: Into<String>>(msg: S) -> Self {
94 Self::DifferentiationError(msg.into())
95 }
96
97 #[must_use]
99 pub fn not_impl<S: Into<String>>(msg: S) -> Self {
100 Self::NotImplemented(msg.into())
101 }
102
103 #[must_use]
105 pub fn quantum<S: Into<String>>(msg: S) -> Self {
106 Self::QuantumError(msg.into())
107 }
108
109 #[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 #[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}