Skip to main content

oxibonsai_kernels/
error.rs

1//! Error types for kernel operations.
2
3use thiserror::Error;
4
5/// Result type alias for kernel operations.
6pub type KernelResult<T> = Result<T, KernelError>;
7
8/// Errors that can occur during 1-bit kernel operations.
9#[derive(Error, Debug)]
10pub enum KernelError {
11    /// Matrix/vector dimension mismatch.
12    #[error("dimension mismatch: expected {expected}, got {got}")]
13    DimensionMismatch { expected: usize, got: usize },
14
15    /// Output buffer is too small.
16    #[error("output buffer too small: need {needed} elements, have {available}")]
17    BufferTooSmall { needed: usize, available: usize },
18
19    /// Number of elements is not a multiple of the block size.
20    #[error("{count} elements is not divisible by block size {block_size}")]
21    NotBlockAligned { count: usize, block_size: usize },
22
23    /// Underlying core error.
24    #[error("core error: {0}")]
25    Core(#[from] oxibonsai_core::error::BonsaiError),
26
27    /// Operation is not supported by this kernel tier.
28    #[error("unsupported operation: {0}")]
29    UnsupportedOperation(String),
30
31    /// A GPU backend error propagated to the kernel layer.
32    #[error("GPU error: {0}")]
33    GpuError(String),
34}
35
36impl KernelError {
37    /// Return a short, stable error code string for monitoring and alerting.
38    pub fn error_code(&self) -> &str {
39        match self {
40            Self::DimensionMismatch { .. } => "DIMENSION_MISMATCH",
41            Self::BufferTooSmall { .. } => "BUFFER_TOO_SMALL",
42            Self::NotBlockAligned { .. } => "NOT_BLOCK_ALIGNED",
43            Self::Core(_) => "CORE_ERROR",
44            Self::UnsupportedOperation(_) => "UNSUPPORTED_OPERATION",
45            Self::GpuError(_) => "GPU_ERROR",
46        }
47    }
48}