scirs2_neural/
error.rs

1//! Error types for the neural network module
2
3use std::error;
4use std::fmt;
5
6// Re-export Error trait for public use
7pub use std::error::Error as StdError;
8
9/// Error type for neural network operations
10#[derive(Debug)]
11pub enum NeuralError {
12    /// Invalid architecture
13    InvalidArchitecture(String),
14    /// Training error
15    TrainingError(String),
16    /// Inference error
17    InferenceError(String),
18    /// Serialization error
19    SerializationError(String),
20    /// Deserialization error
21    DeserializationError(String),
22    /// Validation error
23    ValidationError(String),
24    /// Not implemented error
25    NotImplementedError(String),
26    /// IO error
27    IOError(String),
28    /// Invalid argument error
29    InvalidArgument(String),
30    /// Shape mismatch error  
31    ShapeMismatch(String),
32    /// Computation error
33    ComputationError(String),
34    /// Dimension mismatch error
35    DimensionMismatch(String),
36    /// Distributed training error
37    DistributedError(String),
38    /// Other error
39    Other(String),
40}
41
42impl fmt::Display for NeuralError {
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        match self {
45            NeuralError::InvalidArchitecture(msg) => write!(f, "Invalid architecture: {}", msg),
46            NeuralError::TrainingError(msg) => write!(f, "Training error: {}", msg),
47            NeuralError::InferenceError(msg) => write!(f, "Inference error: {}", msg),
48            NeuralError::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
49            NeuralError::DeserializationError(msg) => write!(f, "Deserialization error: {}", msg),
50            NeuralError::ValidationError(msg) => write!(f, "Validation error: {}", msg),
51            NeuralError::NotImplementedError(msg) => write!(f, "Not implemented: {}", msg),
52            NeuralError::IOError(msg) => write!(f, "IO error: {}", msg),
53            NeuralError::InvalidArgument(msg) => write!(f, "Invalid argument: {}", msg),
54            NeuralError::ShapeMismatch(msg) => write!(f, "Shape mismatch: {}", msg),
55            NeuralError::ComputationError(msg) => write!(f, "Computation error: {}", msg),
56            NeuralError::DimensionMismatch(msg) => write!(f, "Dimension mismatch: {}", msg),
57            NeuralError::DistributedError(msg) => write!(f, "Distributed training error: {}", msg),
58            NeuralError::Other(msg) => write!(f, "Error: {}", msg),
59        }
60    }
61}
62
63impl error::Error for NeuralError {}
64
65/// Error type alias
66pub type Error = NeuralError;
67
68/// Result type for neural network operations
69pub type Result<T> = std::result::Result<T, Error>;
70
71/// Dummy GPU backend type for compilation when GPU features are not available
72#[cfg(not(feature = "gpu"))]
73pub struct DummyGpuBackend;
74
75// Implement conversion from std::io::Error to NeuralError
76impl From<std::io::Error> for NeuralError {
77    fn from(error: std::io::Error) -> Self {
78        NeuralError::IOError(error.to_string())
79    }
80}
81
82// Implement conversion from ndarray::ShapeError to NeuralError
83impl From<ndarray::ShapeError> for NeuralError {
84    fn from(error: ndarray::ShapeError) -> Self {
85        NeuralError::InferenceError(format!("Shape error: {}", error))
86    }
87}