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 Set,
18 Option,
19}
20
21#[derive(Clone)]
23pub enum ReflectError {
24 NoSuchVariant {
26 enum_type: EnumType,
28 },
29
30 WrongShape {
33 expected: &'static Shape,
35 actual: &'static Shape,
37 },
38
39 WasNotA {
41 expected: &'static str,
43
44 actual: &'static Shape,
46 },
47
48 UninitializedField {
50 shape: &'static Shape,
52 field_name: &'static str,
54 },
55
56 UninitializedEnumField {
58 shape: &'static Shape,
60 field_name: &'static str,
62 variant_name: &'static str,
64 },
65
66 UninitializedValue {
68 shape: &'static Shape,
70 },
71
72 InvariantViolation {
74 invariant: &'static str,
76 },
77
78 MissingCharacteristic {
80 shape: &'static Shape,
82 characteristic: Characteristic,
84 },
85
86 OperationFailed {
88 shape: &'static Shape,
90 operation: &'static str,
92 },
93
94 FieldError {
96 shape: &'static Shape,
98 field_error: FieldError,
100 },
101
102 MissingPushPointee {
105 shape: &'static Shape,
107 },
108
109 Unknown,
111
112 TryFromError {
114 src_shape: &'static Shape,
116
117 dst_shape: &'static Shape,
119
120 inner: TryFromError,
122 },
123
124 DefaultAttrButNoDefaultImpl {
126 shape: &'static Shape,
128 },
129
130 Unsized {
132 shape: &'static Shape,
134 operation: &'static str,
136 },
137
138 ArrayNotFullyInitialized {
140 shape: &'static Shape,
142 pushed_count: usize,
144 expected_size: usize,
146 },
147
148 ArrayIndexOutOfBounds {
150 shape: &'static Shape,
152 index: usize,
154 size: usize,
156 },
157
158 InvalidOperation {
160 operation: &'static str,
162 reason: &'static str,
164 },
165
166 UnexpectedTracker {
168 message: &'static str,
171
172 current_tracker: TrackerKind,
174 },
175
176 NoActiveFrame,
178
179 HeistCancelledDifferentShapes {
182 src_shape: &'static Shape,
184 dst_shape: &'static Shape,
186 },
187
188 #[cfg(feature = "alloc")]
189 CustomDeserializationError {
191 message: alloc::string::String,
193 src_shape: &'static Shape,
195 dst_shape: &'static Shape,
197 },
198
199 #[cfg(feature = "alloc")]
200 CustomSerializationError {
202 message: alloc::string::String,
204 src_shape: &'static Shape,
206 dst_shape: &'static Shape,
208 },
209}
210
211impl core::fmt::Display for ReflectError {
212 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
213 match self {
214 ReflectError::NoSuchVariant { enum_type } => {
215 write!(f, "No such variant in enum. Known variants: ")?;
216 for v in enum_type.variants {
217 write!(f, ", {}", v.name)?;
218 }
219 write!(f, ", that's it.")
220 }
221 ReflectError::WrongShape { expected, actual } => {
222 write!(f, "Wrong shape: expected {expected}, but got {actual}")
223 }
224 ReflectError::WasNotA { expected, actual } => {
225 write!(f, "Wrong shape: expected {expected}, but got {actual}")
226 }
227 ReflectError::UninitializedField { shape, field_name } => {
228 write!(f, "Field '{shape}::{field_name}' was not initialized")
229 }
230 ReflectError::UninitializedEnumField {
231 shape,
232 field_name,
233 variant_name,
234 } => {
235 write!(
236 f,
237 "Field '{shape}::{field_name}' in variant '{variant_name}' was not initialized"
238 )
239 }
240 ReflectError::UninitializedValue { shape } => {
241 write!(f, "Value '{shape}' was not initialized")
242 }
243 ReflectError::InvariantViolation { invariant } => {
244 write!(f, "Invariant violation: {invariant}")
245 }
246 ReflectError::MissingCharacteristic {
247 shape,
248 characteristic,
249 } => write!(
250 f,
251 "{shape} does not implement characteristic {characteristic:?}",
252 ),
253 ReflectError::OperationFailed { shape, operation } => {
254 write!(f, "Operation failed on shape {shape}: {operation}")
255 }
256 ReflectError::FieldError { shape, field_error } => {
257 write!(f, "Field error for shape {shape}: {field_error}")
258 }
259 ReflectError::MissingPushPointee { shape } => {
260 write!(
261 f,
262 "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)"
263 )
264 }
265 ReflectError::Unknown => write!(f, "Unknown error"),
266 ReflectError::TryFromError {
267 src_shape,
268 dst_shape,
269 inner,
270 } => {
271 write!(
272 f,
273 "While trying to put {src_shape} into a {dst_shape}: {inner}"
274 )
275 }
276 ReflectError::DefaultAttrButNoDefaultImpl { shape } => write!(
277 f,
278 "Shape '{shape}' has a `default` attribute but no default implementation"
279 ),
280 ReflectError::Unsized { shape, operation } => write!(
281 f,
282 "Shape '{shape}' is unsized, can't perform operation {operation}"
283 ),
284 ReflectError::ArrayNotFullyInitialized {
285 shape,
286 pushed_count,
287 expected_size,
288 } => {
289 write!(
290 f,
291 "Array '{shape}' not fully initialized: expected {expected_size} elements, but got {pushed_count}"
292 )
293 }
294 ReflectError::ArrayIndexOutOfBounds { shape, index, size } => {
295 write!(
296 f,
297 "Array index {index} out of bounds for '{shape}' (array length is {size})"
298 )
299 }
300 ReflectError::InvalidOperation { operation, reason } => {
301 write!(f, "Invalid operation '{operation}': {reason}")
302 }
303 ReflectError::UnexpectedTracker {
304 message,
305 current_tracker,
306 } => {
307 write!(f, "{message}: current tracker is {current_tracker:?}")
308 }
309 ReflectError::NoActiveFrame => {
310 write!(f, "No active frame in Partial")
311 }
312 ReflectError::HeistCancelledDifferentShapes {
313 src_shape,
314 dst_shape,
315 } => {
316 write!(
317 f,
318 "Tried to steal_nth_field from {src_shape} into {dst_shape}"
319 )
320 }
321 #[cfg(feature = "alloc")]
322 ReflectError::CustomDeserializationError {
323 message,
324 src_shape,
325 dst_shape,
326 } => {
327 write!(
328 f,
329 "Custom deserialization of shape '{src_shape}' into '{dst_shape}' failed: {message}"
330 )
331 }
332 #[cfg(feature = "alloc")]
333 ReflectError::CustomSerializationError {
334 message,
335 src_shape,
336 dst_shape,
337 } => {
338 write!(
339 f,
340 "Custom serialization of shape '{src_shape}' into '{dst_shape}' failed: {message}"
341 )
342 }
343 }
344 }
345}
346
347impl core::fmt::Debug for ReflectError {
348 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
349 write!(f, "ReflectError({self})")
351 }
352}
353
354impl core::error::Error for ReflectError {}