1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, LoopError>;
7
8#[derive(Error, Debug, Clone, PartialEq)]
10#[allow(missing_docs)]
11pub enum LoopError {
12 #[error("Strange loop failed to converge after {iterations} iterations")]
14 ConvergenceFailure { iterations: usize },
15
16 #[error("Strange loop timed out after {duration_ns}ns")]
18 TimeoutError { duration_ns: u128 },
19
20 #[error("Lipschitz constant violation: measured L={measured:.6} > limit={limit:.6}")]
22 LipschitzViolation { measured: f64, limit: f64 },
23
24 #[error("Invalid policy parameters: {message}")]
26 InvalidPolicy { message: String },
27
28 #[error("Quantum state error: {message}")]
30 QuantumError { message: String },
31
32 #[error("Consciousness calculation failed: {message}")]
34 ConsciousnessError { message: String },
35
36 #[error("Mathematical error: {message}")]
38 MathError { message: String },
39
40 #[error("Memory allocation failed: {message}")]
42 MemoryError { message: String },
43
44 #[error("Concurrency error: {message}")]
46 ConcurrencyError { message: String },
47
48 #[cfg(feature = "wasm")]
50 #[error("WASM error: {message}")]
51 WasmError { message: String },
52
53 #[cfg(feature = "simd")]
55 #[error("SIMD operation failed: {message}")]
56 SimdError { message: String },
57}
58
59impl LoopError {
60 pub fn convergence_failure(iterations: usize) -> Self {
62 Self::ConvergenceFailure { iterations }
63 }
64
65 pub fn timeout(duration_ns: u128) -> Self {
67 Self::TimeoutError { duration_ns }
68 }
69
70 pub fn lipschitz_violation(measured: f64, limit: f64) -> Self {
72 Self::LipschitzViolation { measured, limit }
73 }
74
75 pub fn invalid_policy(message: impl Into<String>) -> Self {
77 Self::InvalidPolicy { message: message.into() }
78 }
79
80 pub fn quantum_error(message: impl Into<String>) -> Self {
82 Self::QuantumError { message: message.into() }
83 }
84
85 pub fn consciousness_error(message: impl Into<String>) -> Self {
87 Self::ConsciousnessError { message: message.into() }
88 }
89
90 pub fn math_error(message: impl Into<String>) -> Self {
92 Self::MathError { message: message.into() }
93 }
94
95 pub fn memory_error(message: impl Into<String>) -> Self {
97 Self::MemoryError { message: message.into() }
98 }
99
100 pub fn concurrency_error(message: impl Into<String>) -> Self {
102 Self::ConcurrencyError { message: message.into() }
103 }
104
105 pub fn is_recoverable(&self) -> bool {
107 matches!(
108 self,
109 Self::ConvergenceFailure { .. } |
110 Self::TimeoutError { .. } |
111 Self::LipschitzViolation { .. }
112 )
113 }
114
115 pub fn is_fatal(&self) -> bool {
117 !self.is_recoverable()
118 }
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
126 fn test_error_creation() {
127 let error = LoopError::convergence_failure(1000);
128 assert!(matches!(error, LoopError::ConvergenceFailure { iterations: 1000 }));
129 assert!(error.is_recoverable());
130 assert!(!error.is_fatal());
131 }
132
133 #[test]
134 fn test_error_display() {
135 let error = LoopError::timeout(5_000_000);
136 assert_eq!(error.to_string(), "Strange loop timed out after 5000000ns");
137 }
138
139 #[test]
140 fn test_lipschitz_violation() {
141 let error = LoopError::lipschitz_violation(1.2, 1.0);
142 assert!(matches!(
143 error,
144 LoopError::LipschitzViolation { measured, limit } if measured == 1.2 && limit == 1.0
145 ));
146 }
147}