cuda_rust_wasm/
error.rs

1//! Error types for CUDA-Rust transpiler
2
3use thiserror::Error;
4
5/// Main error type for CUDA-Rust operations
6#[derive(Error, Debug)]
7pub enum CudaRustError {
8    /// Parser encountered an error
9    #[error("Parser error: {0}")]
10    ParseError(String),
11    
12    /// Translation/transpilation error
13    #[error("Translation error: {0}")]
14    TranslationError(String),
15    
16    /// Runtime execution error
17    #[error("Runtime error: {0}")]
18    RuntimeError(String),
19    
20    /// Memory allocation or management error
21    #[error("Memory error: {0}")]
22    MemoryError(String),
23    
24    /// Backend-specific error
25    #[error("Backend error: {0}")]
26    Backend(String),
27    
28    /// Kernel compilation error
29    #[error("Kernel compilation error: {0}")]
30    KernelError(String),
31    
32    /// Device not found or not supported
33    #[error("Device error: {0}")]
34    DeviceError(String),
35    
36    /// Invalid argument provided
37    #[error("Invalid argument: {0}")]
38    InvalidArgument(String),
39    
40    /// Feature not yet implemented
41    #[error("Not implemented: {0}")]
42    NotImplemented(String),
43    
44    /// WebGPU-specific error
45    #[cfg(feature = "webgpu-only")]
46    #[error("WebGPU error: {0}")]
47    WebGPUError(String),
48    
49    /// IO error
50    #[error("IO error: {0}")]
51    IoError(#[from] std::io::Error),
52}
53
54/// Convenient Result type alias
55pub type Result<T> = std::result::Result<T, CudaRustError>;
56
57/// Helper macro for creating parse errors
58#[macro_export]
59macro_rules! parse_error {
60    ($msg:expr) => {
61        $crate::error::CudaRustError::ParseError($msg.to_string())
62    };
63    ($fmt:expr, $($arg:tt)*) => {
64        $crate::error::CudaRustError::ParseError(format!($fmt, $($arg)*))
65    };
66}
67
68/// Helper macro for creating translation errors
69#[macro_export]
70macro_rules! translation_error {
71    ($msg:expr) => {
72        $crate::error::CudaRustError::TranslationError($msg.to_string())
73    };
74    ($fmt:expr, $($arg:tt)*) => {
75        $crate::error::CudaRustError::TranslationError(format!($fmt, $($arg)*))
76    };
77}
78
79/// Helper macro for creating runtime errors
80#[macro_export]
81macro_rules! runtime_error {
82    ($msg:expr) => {
83        $crate::error::CudaRustError::RuntimeError($msg.to_string())
84    };
85    ($fmt:expr, $($arg:tt)*) => {
86        $crate::error::CudaRustError::RuntimeError(format!($fmt, $($arg)*))
87    };
88}
89
90/// Macro for creating memory errors
91#[macro_export]
92macro_rules! memory_error {
93    ($msg:expr) => {
94        $crate::error::CudaRustError::MemoryError($msg.to_string())
95    };
96    ($fmt:expr, $($arg:tt)*) => {
97        $crate::error::CudaRustError::MemoryError(format!($fmt, $($arg)*))
98    };
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn test_error_display() {
107        let err = CudaRustError::ParseError("Invalid syntax".to_string());
108        assert_eq!(err.to_string(), "Parser error: Invalid syntax");
109    }
110    
111    #[test]
112    fn test_error_macros() {
113        let err = parse_error!("test error");
114        assert!(matches!(err, CudaRustError::ParseError(_)));
115        
116        let err = parse_error!("error: {}", 42);
117        assert_eq!(err.to_string(), "Parser error: error: 42");
118    }
119}