scirs2_neural/
error.rs

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