simd_kernels/errors.rs
1// Copyright Peter Bower 2025. All Rights Reserved.
2// Licensed under Mozilla Public License (MPL) 2.0.
3
4//! # **Error Types** - *Kernel Operation Error Handling*
5//!
6//! Error types for kernel operations with structured error reporting.
7//! Provides context for debugging and error recovery in computational pipelines.
8//!
9//! ## Error Categories
10//! - **Type Errors**: Mismatched or unsupported data types
11//! - **Dimension Errors**: Array length and capacity mismatches
12//! - **Operator Errors**: Invalid operations for given operands
13//! - **Boundary Errors**: Out-of-bounds access and divide-by-zero conditions
14//! - **Planning Errors**: Configuration and setup failures
15//!
16//! All errors include contextual message space for debugging.
17
18use core::fmt;
19use std::error::Error;
20
21/// Comprehensive error type for all kernel operations.
22///
23/// Each variant includes a contextual message string providing specific details
24/// about the error condition, enabling precise debugging and error reporting.
25#[derive(Debug, Clone)]
26pub enum KernelError {
27 /// Data type mismatch between operands or unsupported type combinations.
28 TypeMismatch(String),
29
30 /// Array length mismatch between operands.
31 LengthMismatch(String),
32
33 /// Invalid operator for the given operands or context.
34 OperatorMismatch(String),
35
36 /// Unsupported data type for the requested operation.
37 UnsupportedType(String),
38
39 /// Column or field not found in structured data.
40 ColumnNotFound(String),
41
42 /// Invalid arguments provided to kernel function.
43 InvalidArguments(String),
44
45 /// Planning or configuration error.
46 Plan(String),
47
48 /// Array index or memory access out of bounds.
49 OutOfBounds(String),
50
51 /// Division by zero or similar mathematical errors.
52 DivideByZero(String),
53}
54
55impl fmt::Display for KernelError {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 match self {
58 KernelError::TypeMismatch(msg) => write!(f, "Type mismatch: {}", msg),
59 KernelError::LengthMismatch(msg) => write!(f, "Length mismatch: {}", msg),
60 KernelError::OperatorMismatch(msg) => write!(f, "Operator mismatch: {}", msg),
61 KernelError::UnsupportedType(msg) => write!(f, "Unsupported type: {}", msg),
62 KernelError::ColumnNotFound(msg) => write!(f, "Column not found: {}", msg),
63 KernelError::InvalidArguments(msg) => write!(f, "Invalid arguments: {}", msg),
64 KernelError::Plan(msg) => write!(f, "Planning error: {}", msg),
65 KernelError::OutOfBounds(msg) => write!(f, "Out of bounds: {}", msg),
66 KernelError::DivideByZero(msg) => write!(f, "Divide by Zero error: {}", msg),
67 }
68 }
69}
70
71impl Error for KernelError {}
72
73/// Creates a formatted error message for length mismatches between left-hand side (LHS) and right-hand side (RHS) arrays.
74///
75/// # Arguments
76/// * `fname` - Function name where the mismatch occurred
77/// * `lhs` - Length of the left-hand side array
78/// * `rhs` - Length of the right-hand side array
79///
80/// # Returns
81/// A formatted error message string
82pub fn log_length_mismatch(fname: String, lhs: usize, rhs: usize) -> String {
83 return format!("{} => Length mismatch: LHS {} RHS {}", fname, lhs, rhs);
84}