use std::fmt;
use std::error::Error;
use std::io;
#[derive(Debug)]
pub enum HTTError {
Config(String),
Storage(String),
Tensor(String),
Geometry(String),
DimensionMismatch {
expected: usize,
actual: usize,
},
Registry(String),
IO(io::Error),
Serialization(String),
Extension(String),
Initialization(String),
}
impl fmt::Display for HTTError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
HTTError::Config(msg) => write!(f, "Configuration error: {}", msg),
HTTError::Storage(msg) => write!(f, "Storage error: {}", msg),
HTTError::Tensor(msg) => write!(f, "Tensor operation error: {}", msg),
HTTError::Geometry(msg) => write!(f, "Hyperbolic geometry error: {}", msg),
HTTError::DimensionMismatch { expected, actual } => {
write!(f, "Dimension mismatch: expected {}, got {}", expected, actual)
},
HTTError::Registry(msg) => write!(f, "Registry error: {}", msg),
HTTError::IO(err) => write!(f, "I/O error: {}", err),
HTTError::Serialization(msg) => write!(f, "Serialization error: {}", msg),
HTTError::Extension(msg) => write!(f, "Extension error: {}", msg),
HTTError::Initialization(msg) => write!(f, "Initialization error: {}", msg),
}
}
}
impl Error for HTTError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
HTTError::IO(err) => Some(err),
_ => None,
}
}
}
impl From<io::Error> for HTTError {
fn from(err: io::Error) -> Self {
HTTError::IO(err)
}
}
impl From<serde_json::Error> for HTTError {
fn from(err: serde_json::Error) -> Self {
HTTError::Serialization(err.to_string())
}
}
impl From<super::registry::RegistryError> for HTTError {
fn from(err: super::registry::RegistryError) -> Self {
HTTError::Registry(err.to_string())
}
}
pub type HTTResult<T> = Result<T, HTTError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let errors = vec![
HTTError::Config("Invalid configuration".to_string()),
HTTError::Storage("Storage failure".to_string()),
HTTError::Tensor("Tensor operation failed".to_string()),
HTTError::Geometry("Invalid hyperbolic coordinates".to_string()),
HTTError::DimensionMismatch { expected: 3, actual: 2 },
HTTError::Registry("Component not found".to_string()),
HTTError::IO(io::Error::new(io::ErrorKind::NotFound, "File not found")),
HTTError::Serialization("Invalid JSON".to_string()),
HTTError::Extension("Extension failed to load".to_string()),
HTTError::Initialization("Failed to initialize HTT".to_string()),
];
for error in errors {
let _display = format!("{}", error);
assert!(!_display.is_empty());
}
}
#[test]
fn test_io_error_conversion() {
let io_error = io::Error::new(io::ErrorKind::PermissionDenied, "Permission denied");
let htt_error: HTTError = io_error.into();
match htt_error {
HTTError::IO(_) => (), _ => panic!("Expected IO error variant"),
}
}
}