Skip to main content

cjc_runtime/
error.rs

1use std::fmt;
2
3// ---------------------------------------------------------------------------
4// Error types
5// ---------------------------------------------------------------------------
6
7/// Errors produced by the CJC runtime.
8#[derive(Debug, Clone)]
9pub enum RuntimeError {
10    IndexOutOfBounds {
11        index: usize,
12        length: usize,
13    },
14    ShapeMismatch {
15        expected: usize,
16        got: usize,
17    },
18    DimensionMismatch {
19        expected: usize,
20        got: usize,
21    },
22    InvalidOperation(String),
23}
24
25impl fmt::Display for RuntimeError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            RuntimeError::IndexOutOfBounds { index, length } => {
29                write!(f, "index {index} out of bounds for length {length}")
30            }
31            RuntimeError::ShapeMismatch { expected, got } => {
32                write!(f, "shape mismatch: expected {expected} elements, got {got}")
33            }
34            RuntimeError::DimensionMismatch { expected, got } => {
35                write!(
36                    f,
37                    "dimension mismatch: expected {expected} dimensions, got {got}"
38                )
39            }
40            RuntimeError::InvalidOperation(msg) => {
41                write!(f, "invalid operation: {msg}")
42            }
43        }
44    }
45}
46
47impl std::error::Error for RuntimeError {}
48