1use std::fmt;
10use std::error::Error;
11use std::io;
12
13#[derive(Debug)]
15pub enum HTTError {
16 Config(String),
18
19 Storage(String),
21
22 Tensor(String),
24
25 Geometry(String),
27
28 DimensionMismatch {
30 expected: usize,
32 actual: usize,
34 },
35
36 Registry(String),
38
39 IO(io::Error),
41
42 Serialization(String),
44
45 Extension(String),
47
48 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
80impl 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
100pub 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 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(_) => (), _ => panic!("Expected IO error variant"),
137 }
138 }
139}