strange_loop/
error.rs

1//! Error types for the strange-loop crate
2
3use thiserror::Error;
4
5/// Result type for strange loop operations
6pub type Result<T> = std::result::Result<T, LoopError>;
7
8/// Errors that can occur during strange loop execution
9#[derive(Error, Debug, Clone, PartialEq)]
10#[allow(missing_docs)]
11pub enum LoopError {
12    /// Loop failed to converge within the specified iteration limit
13    #[error("Strange loop failed to converge after {iterations} iterations")]
14    ConvergenceFailure { iterations: usize },
15
16    /// Loop exceeded the maximum allowed execution time
17    #[error("Strange loop timed out after {duration_ns}ns")]
18    TimeoutError { duration_ns: u128 },
19
20    /// Lipschitz constant violation detected
21    #[error("Lipschitz constant violation: measured L={measured:.6} > limit={limit:.6}")]
22    LipschitzViolation { measured: f64, limit: f64 },
23
24    /// Invalid policy parameters
25    #[error("Invalid policy parameters: {message}")]
26    InvalidPolicy { message: String },
27
28    /// Quantum state error
29    #[error("Quantum state error: {message}")]
30    QuantumError { message: String },
31
32    /// Consciousness calculation error
33    #[error("Consciousness calculation failed: {message}")]
34    ConsciousnessError { message: String },
35
36    /// Mathematical operation error
37    #[error("Mathematical error: {message}")]
38    MathError { message: String },
39
40    /// Memory allocation error
41    #[error("Memory allocation failed: {message}")]
42    MemoryError { message: String },
43
44    /// Concurrency error
45    #[error("Concurrency error: {message}")]
46    ConcurrencyError { message: String },
47
48    /// WASM-specific error
49    #[cfg(feature = "wasm")]
50    #[error("WASM error: {message}")]
51    WasmError { message: String },
52
53    /// SIMD operation error
54    #[cfg(feature = "simd")]
55    #[error("SIMD operation failed: {message}")]
56    SimdError { message: String },
57}
58
59impl LoopError {
60    /// Create a new convergence failure error
61    pub fn convergence_failure(iterations: usize) -> Self {
62        Self::ConvergenceFailure { iterations }
63    }
64
65    /// Create a new timeout error
66    pub fn timeout(duration_ns: u128) -> Self {
67        Self::TimeoutError { duration_ns }
68    }
69
70    /// Create a new Lipschitz violation error
71    pub fn lipschitz_violation(measured: f64, limit: f64) -> Self {
72        Self::LipschitzViolation { measured, limit }
73    }
74
75    /// Create a new invalid policy error
76    pub fn invalid_policy(message: impl Into<String>) -> Self {
77        Self::InvalidPolicy { message: message.into() }
78    }
79
80    /// Create a new quantum error
81    pub fn quantum_error(message: impl Into<String>) -> Self {
82        Self::QuantumError { message: message.into() }
83    }
84
85    /// Create a new consciousness error
86    pub fn consciousness_error(message: impl Into<String>) -> Self {
87        Self::ConsciousnessError { message: message.into() }
88    }
89
90    /// Create a new math error
91    pub fn math_error(message: impl Into<String>) -> Self {
92        Self::MathError { message: message.into() }
93    }
94
95    /// Create a new memory error
96    pub fn memory_error(message: impl Into<String>) -> Self {
97        Self::MemoryError { message: message.into() }
98    }
99
100    /// Create a new concurrency error
101    pub fn concurrency_error(message: impl Into<String>) -> Self {
102        Self::ConcurrencyError { message: message.into() }
103    }
104
105    /// Check if this is a recoverable error
106    pub fn is_recoverable(&self) -> bool {
107        matches!(
108            self,
109            Self::ConvergenceFailure { .. } |
110            Self::TimeoutError { .. } |
111            Self::LipschitzViolation { .. }
112        )
113    }
114
115    /// Check if this is a fatal error
116    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}