Skip to main content

horon_engine/
error.rs

1//! Error handling for the horon-engine library
2//!
3//! This module defines a comprehensive error handling system for the Hyperbolic Tree Tensor
4//! library. It establishes HTT-specific error types that eliminate dependencies on
5//! GSD's IntegrationError, enabling HTT to function as a fully standalone library.
6//! The module provides bidirectional conversions between HTT errors and GSD integration
7//! errors when the "gsd" feature is enabled.
8
9use std::fmt;
10use std::error::Error;
11use std::io;
12
13/// Main error type for HTT operations
14#[derive(Debug)]
15pub enum HTTError {
16    /// Configuration error
17    Config(String),
18    
19    /// Storage operation error
20    Storage(String),
21    
22    /// Tensor network operation error
23    Tensor(String),
24    
25    /// Hyperbolic geometry error
26    Geometry(String),
27    
28    /// Dimension mismatch
29    DimensionMismatch {
30        /// The dimension the store was configured with.
31        expected: usize,
32        /// The dimension the caller supplied.
33        actual: usize,
34    },
35    
36    /// Component registry error
37    Registry(String),
38    
39    /// I/O error (for file operations)
40    IO(io::Error),
41    
42    /// Serialization error
43    Serialization(String),
44    
45    /// Extension error
46    Extension(String),
47    
48    /// Initialization error
49    Initialization(String),
50}
51
52impl fmt::Display for HTTError {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match self {
55            HTTError::Config(msg) => write!(f, "Configuration error: {}", msg),
56            HTTError::Storage(msg) => write!(f, "Storage error: {}", msg),
57            HTTError::Tensor(msg) => write!(f, "Tensor operation error: {}", msg),
58            HTTError::Geometry(msg) => write!(f, "Hyperbolic geometry error: {}", msg),
59            HTTError::DimensionMismatch { expected, actual } => {
60                write!(f, "Dimension mismatch: expected {}, got {}", expected, actual)
61            },
62            HTTError::Registry(msg) => write!(f, "Registry error: {}", msg),
63            HTTError::IO(err) => write!(f, "I/O error: {}", err),
64            HTTError::Serialization(msg) => write!(f, "Serialization error: {}", msg),
65            HTTError::Extension(msg) => write!(f, "Extension error: {}", msg),
66            HTTError::Initialization(msg) => write!(f, "Initialization error: {}", msg),
67        }
68    }
69}
70
71impl Error for HTTError {
72    fn source(&self) -> Option<&(dyn Error + 'static)> {
73        match self {
74            HTTError::IO(err) => Some(err),
75            _ => None,
76        }
77    }
78}
79
80// Implement From trait for common error types
81
82impl From<io::Error> for HTTError {
83    fn from(err: io::Error) -> Self {
84        HTTError::IO(err)
85    }
86}
87
88impl From<serde_json::Error> for HTTError {
89    fn from(err: serde_json::Error) -> Self {
90        HTTError::Serialization(err.to_string())
91    }
92}
93
94impl From<super::registry::RegistryError> for HTTError {
95    fn from(err: super::registry::RegistryError) -> Self {
96        HTTError::Registry(err.to_string())
97    }
98}
99
100/// Result type alias for HTT operations.
101pub type HTTResult<T> = Result<T, HTTError>;
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106    
107    #[test]
108    fn test_error_display() {
109        let errors = vec![
110            HTTError::Config("Invalid configuration".to_string()),
111            HTTError::Storage("Storage failure".to_string()),
112            HTTError::Tensor("Tensor operation failed".to_string()),
113            HTTError::Geometry("Invalid hyperbolic coordinates".to_string()),
114            HTTError::DimensionMismatch { expected: 3, actual: 2 },
115            HTTError::Registry("Component not found".to_string()),
116            HTTError::IO(io::Error::new(io::ErrorKind::NotFound, "File not found")),
117            HTTError::Serialization("Invalid JSON".to_string()),
118            HTTError::Extension("Extension failed to load".to_string()),
119            HTTError::Initialization("Failed to initialize HTT".to_string()),
120        ];
121        
122        for error in errors {
123            // Just check that display doesn't panic
124            let _display = format!("{}", error);
125            assert!(!_display.is_empty());
126        }
127    }
128    
129    #[test]
130    fn test_io_error_conversion() {
131        let io_error = io::Error::new(io::ErrorKind::PermissionDenied, "Permission denied");
132        let htt_error: HTTError = io_error.into();
133        
134        match htt_error {
135            HTTError::IO(_) => (), // Expected
136            _ => panic!("Expected IO error variant"),
137        }
138    }
139}