1use core::fmt;
2
3#[cfg_attr(
13 feature = "serde-support",
14 derive(serde::Serialize, serde::Deserialize)
15)]
16#[derive(Debug, Clone, PartialEq, Eq)]
17#[non_exhaustive]
18pub enum CoreError {
19 DimensionMismatch {
21 expected: Vec<usize>,
22 got: Vec<usize>,
23 },
24
25 InvalidShape {
27 shape: Vec<usize>,
28 reason: &'static str,
29 },
30
31 AxisOutOfBounds { axis: usize, ndim: usize },
33
34 IndexOutOfBounds {
36 index: Vec<usize>,
37 shape: Vec<usize>,
38 },
39
40 SingularMatrix,
42
43 InvalidArgument { reason: &'static str },
45
46 BroadcastError {
48 shape_a: Vec<usize>,
49 shape_b: Vec<usize>,
50 },
51}
52
53impl fmt::Display for CoreError {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 match self {
56 Self::DimensionMismatch { expected, got } => {
57 write!(f, "dimension mismatch: expected {expected:?}, got {got:?}")
58 }
59 Self::InvalidShape { shape, reason } => {
60 write!(f, "invalid shape {shape:?}: {reason}")
61 }
62 Self::AxisOutOfBounds { axis, ndim } => {
63 write!(
64 f,
65 "axis {axis} out of bounds for tensor with {ndim} dimensions"
66 )
67 }
68 Self::IndexOutOfBounds { index, shape } => {
69 write!(f, "index {index:?} out of bounds for shape {shape:?}")
70 }
71 Self::SingularMatrix => write!(f, "singular matrix"),
72 Self::InvalidArgument { reason } => write!(f, "invalid argument: {reason}"),
73 Self::BroadcastError { shape_a, shape_b } => {
74 write!(f, "cannot broadcast shapes {shape_a:?} and {shape_b:?}")
75 }
76 }
77 }
78}
79
80impl std::error::Error for CoreError {}
81
82pub type Result<T> = std::result::Result<T, CoreError>;