1use facet_core::{Characteristic, EnumType, FieldError, Shape, TryFromError};
2
3#[allow(missing_docs)]
5#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
6#[non_exhaustive]
7pub enum TrackerKind {
8 Uninit,
9 Init,
10 Array,
11 Struct,
12 SmartPointer,
13 SmartPointerSlice,
14 Enum,
15 List,
16 Map,
17 Option,
18}
19
20#[derive(Clone)]
22pub enum ReflectError {
23 NoSuchVariant {
25 enum_type: EnumType,
27 },
28
29 WrongShape {
32 expected: &'static Shape,
34 actual: &'static Shape,
36 },
37
38 WasNotA {
40 expected: &'static str,
42
43 actual: &'static Shape,
45 },
46
47 UninitializedField {
49 shape: &'static Shape,
51 field_name: &'static str,
53 },
54
55 UninitializedEnumField {
57 shape: &'static Shape,
59 field_name: &'static str,
61 variant_name: &'static str,
63 },
64
65 UninitializedValue {
67 shape: &'static Shape,
69 },
70
71 InvariantViolation {
73 invariant: &'static str,
75 },
76
77 MissingCharacteristic {
79 shape: &'static Shape,
81 characteristic: Characteristic,
83 },
84
85 OperationFailed {
87 shape: &'static Shape,
89 operation: &'static str,
91 },
92
93 FieldError {
95 shape: &'static Shape,
97 field_error: FieldError,
99 },
100
101 MissingPushPointee {
104 shape: &'static Shape,
106 },
107
108 Unknown,
110
111 TryFromError {
113 src_shape: &'static Shape,
115
116 dst_shape: &'static Shape,
118
119 inner: TryFromError,
121 },
122
123 DefaultAttrButNoDefaultImpl {
125 shape: &'static Shape,
127 },
128
129 Unsized {
131 shape: &'static Shape,
133 operation: &'static str,
135 },
136
137 ArrayNotFullyInitialized {
139 shape: &'static Shape,
141 pushed_count: usize,
143 expected_size: usize,
145 },
146
147 ArrayIndexOutOfBounds {
149 shape: &'static Shape,
151 index: usize,
153 size: usize,
155 },
156
157 InvalidOperation {
159 operation: &'static str,
161 reason: &'static str,
163 },
164
165 UnexpectedTracker {
167 message: &'static str,
170
171 current_tracker: TrackerKind,
173 },
174
175 NoActiveFrame,
177
178 HeistCancelledDifferentShapes {
181 src_shape: &'static Shape,
183 dst_shape: &'static Shape,
185 },
186}
187
188impl core::fmt::Display for ReflectError {
189 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
190 match self {
191 ReflectError::NoSuchVariant { enum_type } => {
192 write!(f, "No such variant in enum. Known variants: ")?;
193 for v in enum_type.variants {
194 write!(f, ", {}", v.name)?;
195 }
196 write!(f, ", that's it.")
197 }
198 ReflectError::WrongShape { expected, actual } => {
199 write!(f, "Wrong shape: expected {expected}, but got {actual}")
200 }
201 ReflectError::WasNotA { expected, actual } => {
202 write!(f, "Wrong shape: expected {expected}, but got {actual}")
203 }
204 ReflectError::UninitializedField { shape, field_name } => {
205 write!(f, "Field '{shape}::{field_name}' was not initialized")
206 }
207 ReflectError::UninitializedEnumField {
208 shape,
209 field_name,
210 variant_name,
211 } => {
212 write!(
213 f,
214 "Field '{shape}::{field_name}' in variant '{variant_name}' was not initialized"
215 )
216 }
217 ReflectError::UninitializedValue { shape } => {
218 write!(f, "Value '{shape}' was not initialized")
219 }
220 ReflectError::InvariantViolation { invariant } => {
221 write!(f, "Invariant violation: {invariant}")
222 }
223 ReflectError::MissingCharacteristic {
224 shape,
225 characteristic,
226 } => write!(
227 f,
228 "{shape} does not implement characteristic {characteristic:?}",
229 ),
230 ReflectError::OperationFailed { shape, operation } => {
231 write!(f, "Operation failed on shape {shape}: {operation}")
232 }
233 ReflectError::FieldError { shape, field_error } => {
234 write!(f, "Field error for shape {shape}: {field_error}")
235 }
236 ReflectError::MissingPushPointee { shape } => {
237 write!(
238 f,
239 "Tried to access a field on smart pointer '{shape}', but you need to call .begin_smart_ptr() first to work with the value it points to (and pop it with .pop() later)"
240 )
241 }
242 ReflectError::Unknown => write!(f, "Unknown error"),
243 ReflectError::TryFromError {
244 src_shape,
245 dst_shape,
246 inner,
247 } => {
248 write!(
249 f,
250 "While trying to put {src_shape} into a {dst_shape}: {inner}"
251 )
252 }
253 ReflectError::DefaultAttrButNoDefaultImpl { shape } => write!(
254 f,
255 "Shape '{shape}' has a `default` attribute but no default implementation"
256 ),
257 ReflectError::Unsized { shape, operation } => write!(
258 f,
259 "Shape '{shape}' is unsized, can't perform operation {operation}"
260 ),
261 ReflectError::ArrayNotFullyInitialized {
262 shape,
263 pushed_count,
264 expected_size,
265 } => {
266 write!(
267 f,
268 "Array '{shape}' not fully initialized: expected {expected_size} elements, but got {pushed_count}"
269 )
270 }
271 ReflectError::ArrayIndexOutOfBounds { shape, index, size } => {
272 write!(
273 f,
274 "Array index {index} out of bounds for '{shape}' (array length is {size})"
275 )
276 }
277 ReflectError::InvalidOperation { operation, reason } => {
278 write!(f, "Invalid operation '{operation}': {reason}")
279 }
280 ReflectError::UnexpectedTracker {
281 message,
282 current_tracker,
283 } => {
284 write!(f, "{message}: current tracker is {current_tracker:?}")
285 }
286 ReflectError::NoActiveFrame => {
287 write!(f, "No active frame in Partial")
288 }
289 ReflectError::HeistCancelledDifferentShapes {
290 src_shape,
291 dst_shape,
292 } => {
293 write!(
294 f,
295 "Tried to steal_nth_field from {src_shape} into {dst_shape}"
296 )
297 }
298 }
299 }
300}
301
302impl core::fmt::Debug for ReflectError {
303 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
304 write!(f, "ReflectError({self})")
306 }
307}
308
309impl core::error::Error for ReflectError {}