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 SmartPointerStr,
14 SmartPointerSlice,
15 Enum,
16 List,
17 Map,
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
180impl core::fmt::Display for ReflectError {
181 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
182 match self {
183 ReflectError::NoSuchVariant { enum_type } => {
184 write!(f, "No such variant in enum. Known variants: ")?;
185 for v in enum_type.variants {
186 write!(f, ", {}", v.name)?;
187 }
188 write!(f, ", that's it.")
189 }
190 ReflectError::WrongShape { expected, actual } => {
191 write!(f, "Wrong shape: expected {expected}, but got {actual}")
192 }
193 ReflectError::WasNotA { expected, actual } => {
194 write!(f, "Wrong shape: expected {expected}, but got {actual}")
195 }
196 ReflectError::UninitializedField { shape, field_name } => {
197 write!(f, "Field '{shape}::{field_name}' was not initialized")
198 }
199 ReflectError::UninitializedEnumField {
200 shape,
201 field_name,
202 variant_name,
203 } => {
204 write!(
205 f,
206 "Field '{shape}::{field_name}' in variant '{variant_name}' was not initialized"
207 )
208 }
209 ReflectError::UninitializedValue { shape } => {
210 write!(f, "Value '{shape}' was not initialized")
211 }
212 ReflectError::InvariantViolation { invariant } => {
213 write!(f, "Invariant violation: {invariant}")
214 }
215 ReflectError::MissingCharacteristic {
216 shape,
217 characteristic,
218 } => write!(
219 f,
220 "{shape} does not implement characteristic {characteristic:?}",
221 ),
222 ReflectError::OperationFailed { shape, operation } => {
223 write!(f, "Operation failed on shape {shape}: {operation}")
224 }
225 ReflectError::FieldError { shape, field_error } => {
226 write!(f, "Field error for shape {shape}: {field_error}")
227 }
228 ReflectError::MissingPushPointee { shape } => {
229 write!(
230 f,
231 "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)"
232 )
233 }
234 ReflectError::Unknown => write!(f, "Unknown error"),
235 ReflectError::TryFromError {
236 src_shape,
237 dst_shape,
238 inner,
239 } => {
240 write!(
241 f,
242 "While trying to put {src_shape} into a {dst_shape}: {inner}"
243 )
244 }
245 ReflectError::DefaultAttrButNoDefaultImpl { shape } => write!(
246 f,
247 "Shape '{shape}' has a `default` attribute but no default implementation"
248 ),
249 ReflectError::Unsized { shape, operation } => write!(
250 f,
251 "Shape '{shape}' is unsized, can't perform operation {operation}"
252 ),
253 ReflectError::ArrayNotFullyInitialized {
254 shape,
255 pushed_count,
256 expected_size,
257 } => {
258 write!(
259 f,
260 "Array '{shape}' not fully initialized: expected {expected_size} elements, but got {pushed_count}"
261 )
262 }
263 ReflectError::ArrayIndexOutOfBounds { shape, index, size } => {
264 write!(
265 f,
266 "Array index {index} out of bounds for '{shape}' (array length is {size})"
267 )
268 }
269 ReflectError::InvalidOperation { operation, reason } => {
270 write!(f, "Invalid operation '{operation}': {reason}")
271 }
272 ReflectError::UnexpectedTracker {
273 message,
274 current_tracker,
275 } => {
276 write!(f, "{message}: current tracker is {current_tracker:?}")
277 }
278 ReflectError::NoActiveFrame => {
279 write!(f, "No active frame in Partial")
280 }
281 }
282 }
283}
284
285impl core::fmt::Debug for ReflectError {
286 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
287 write!(f, "ReflectError({self})")
289 }
290}
291
292impl core::error::Error for ReflectError {}