1use std::error::Error;
4use std::fmt::{Display, Formatter};
5
6use crate::slice_range::SliceRange;
7
8#[derive(Debug, PartialEq)]
10pub struct DimensionError {
11 pub actual: usize,
13
14 pub expected: usize,
16}
17
18impl Display for DimensionError {
19 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20 write!(
21 f,
22 "tensor has {} dims but expected {}",
23 self.actual, self.expected
24 )
25 }
26}
27
28impl Error for DimensionError {}
29
30#[derive(Debug, PartialEq)]
32pub enum FromDataError {
33 StorageTooShort,
35
36 StorageLengthMismatch,
39
40 MayOverlap,
44}
45
46impl Display for FromDataError {
47 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48 match self {
49 FromDataError::StorageTooShort => write!(f, "data too short"),
50 FromDataError::StorageLengthMismatch => write!(f, "data length mismatch"),
51 FromDataError::MayOverlap => write!(f, "may have internal overlap"),
52 }
53 }
54}
55
56impl Error for FromDataError {}
57
58#[derive(Clone, Debug, PartialEq)]
60pub enum SliceError {
61 TooManyDims {
63 ndim: usize,
65 range_ndim: usize,
67 },
68
69 InvalidAxis { axis: usize },
72
73 InvalidIndex {
76 axis: usize,
78 index: isize,
80 size: usize,
82 },
83
84 InvalidRange {
87 axis: usize,
89
90 range: SliceRange,
92
93 size: usize,
95 },
96
97 InvalidStep {
100 axis: usize,
102
103 step: isize,
105 },
106
107 OutputDimsMismatch { actual: usize, expected: usize },
110}
111
112impl Display for SliceError {
113 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
114 match self {
115 SliceError::TooManyDims { ndim, range_ndim } => {
116 write!(
117 f,
118 "slice range has {} items but tensor has only {} dims",
119 range_ndim, ndim
120 )
121 }
122 SliceError::InvalidAxis { axis } => write!(f, "slice axis {} is invalid", axis),
123 SliceError::InvalidIndex { axis, index, size } => write!(
124 f,
125 "slice index {} is invalid for axis ({}) of size {}",
126 index, axis, size
127 ),
128 SliceError::InvalidRange { axis, range, size } => write!(
129 f,
130 "slice range {:?} is invalid for axis ({}) of size {}",
131 range, axis, size
132 ),
133 SliceError::InvalidStep { axis, step } => {
134 write!(f, "slice step {} is invalid for axis {}", step, axis)
135 }
136 SliceError::OutputDimsMismatch { actual, expected } => {
137 write!(
138 f,
139 "slice output dims {} does not match expected dims {}",
140 actual, expected
141 )
142 }
143 }
144 }
145}
146
147impl Error for SliceError {}
148
149#[derive(Clone, Debug, PartialEq)]
151pub enum ReshapeError {
152 NotContiguous,
155
156 LengthMismatch,
159}
160
161impl std::fmt::Display for ReshapeError {
162 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
163 match self {
164 ReshapeError::NotContiguous => write!(f, "view is not contiguous"),
165 ReshapeError::LengthMismatch => write!(f, "new shape has a different length"),
166 }
167 }
168}
169
170#[derive(Clone, Debug, PartialEq)]
172pub enum ExpandError {
173 ShapeMismatch,
176
177 InsufficientCapacity,
179}
180
181impl std::fmt::Display for ExpandError {
182 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
183 match self {
184 ExpandError::ShapeMismatch => {
185 write!(
186 f,
187 "non-expanding dimensions of source and destination do not match"
188 )
189 }
190 ExpandError::InsufficientCapacity => {
191 write!(f, "insufficient capacity for new dimension size")
192 }
193 }
194 }
195}