1use facet_core::{Characteristic, EnumType, FieldError, Shape, TryFromError};
2use owo_colors::OwoColorize;
3
4#[derive(PartialEq, Clone)]
6#[non_exhaustive]
7pub enum ReflectError<'shape> {
8 NoSuchVariant {
10 enum_type: EnumType<'shape>,
12 },
13
14 WrongShape {
17 expected: &'shape Shape<'shape>,
19 actual: &'shape Shape<'shape>,
21 },
22
23 WasNotA {
25 expected: &'shape str,
27
28 actual: &'shape Shape<'shape>,
30 },
31
32 UninitializedField {
34 shape: &'shape Shape<'shape>,
36 field_name: &'shape str,
38 },
39
40 UninitializedEnumField {
42 shape: &'shape Shape<'shape>,
44 field_name: &'shape str,
46 variant_name: &'shape str,
48 },
49
50 UninitializedValue {
52 shape: &'shape Shape<'shape>,
54 },
55
56 InvariantViolation {
58 invariant: &'shape str,
60 },
61
62 MissingCharacteristic {
64 shape: &'shape Shape<'shape>,
66 characteristic: Characteristic,
68 },
69
70 OperationFailed {
72 shape: &'shape Shape<'shape>,
74 operation: &'shape str,
76 },
77
78 FieldError {
80 shape: &'shape Shape<'shape>,
82 field_error: FieldError,
84 },
85
86 MissingPushPointee {
89 shape: &'shape Shape<'shape>,
91 },
92
93 Unknown,
95
96 TryFromError {
98 src_shape: &'shape Shape<'shape>,
100
101 dst_shape: &'shape Shape<'shape>,
103
104 inner: TryFromError<'shape>,
106 },
107
108 DefaultAttrButNoDefaultImpl {
110 shape: &'shape Shape<'shape>,
112 },
113
114 Unsized {
116 shape: &'shape Shape<'shape>,
118 },
119
120 ArrayNotFullyInitialized {
122 shape: &'shape Shape<'shape>,
124 pushed_count: usize,
126 expected_size: usize,
128 },
129
130 ArrayIndexOutOfBounds {
132 shape: &'shape Shape<'shape>,
134 index: usize,
136 size: usize,
138 },
139
140 InvalidOperation {
142 operation: &'static str,
144 reason: &'static str,
146 },
147
148 NoActiveFrame,
150}
151
152impl core::fmt::Display for ReflectError<'_> {
153 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
154 match self {
155 ReflectError::NoSuchVariant { enum_type } => {
156 write!(f, "No such variant in enum. Known variants: ")?;
157 for v in enum_type.variants {
158 write!(f, ", {}", v.name.cyan())?;
159 }
160 write!(f, ", that's it.")
161 }
162 ReflectError::WrongShape { expected, actual } => {
163 write!(
164 f,
165 "Wrong shape: expected {}, but got {}",
166 expected.green(),
167 actual.red()
168 )
169 }
170 ReflectError::WasNotA { expected, actual } => {
171 write!(
172 f,
173 "Wrong shape: expected {}, but got {}",
174 expected.green(),
175 actual.red()
176 )
177 }
178 ReflectError::UninitializedField { shape, field_name } => {
179 write!(f, "Field '{}::{}' was not initialized", shape, field_name)
180 }
181 ReflectError::UninitializedEnumField {
182 shape,
183 field_name,
184 variant_name,
185 } => {
186 write!(
187 f,
188 "Field '{}::{}' in variant '{}' was not initialized",
189 shape.blue(),
190 field_name.yellow(),
191 variant_name.red()
192 )
193 }
194 ReflectError::UninitializedValue { shape } => {
195 write!(f, "Value '{}' was not initialized", shape.blue())
196 }
197 ReflectError::InvariantViolation { invariant } => {
198 write!(f, "Invariant violation: {}", invariant.red())
199 }
200 ReflectError::MissingCharacteristic {
201 shape,
202 characteristic,
203 } => write!(
204 f,
205 "{shape} does not implement characteristic {characteristic:?}",
206 ),
207 ReflectError::OperationFailed { shape, operation } => {
208 write!(
209 f,
210 "Operation failed on shape {}: {}",
211 shape.blue(),
212 operation
213 )
214 }
215 ReflectError::FieldError { shape, field_error } => {
216 write!(f, "Field error for shape {}: {}", shape.red(), field_error)
217 }
218 ReflectError::MissingPushPointee { shape } => {
219 write!(
220 f,
221 "Tried to access a field on smart pointer '{}', but you need to call {} first to work with the value it points to (and pop it with {} later)",
222 shape.blue(),
223 ".begin_smart_ptr()".yellow(),
224 ".pop()".yellow()
225 )
226 }
227 ReflectError::Unknown => write!(f, "Unknown error"),
228 ReflectError::TryFromError {
229 src_shape,
230 dst_shape,
231 inner,
232 } => {
233 write!(
234 f,
235 "While trying to put {} into a {}: {}",
236 src_shape.green(),
237 dst_shape.blue(),
238 inner.red()
239 )
240 }
241 ReflectError::DefaultAttrButNoDefaultImpl { shape } => write!(
242 f,
243 "Shape '{}' has a `default` attribute but no default implementation",
244 shape.red()
245 ),
246 ReflectError::Unsized { shape } => write!(f, "Shape '{}' is unsized", shape.red()),
247 ReflectError::ArrayNotFullyInitialized {
248 shape,
249 pushed_count,
250 expected_size,
251 } => {
252 write!(
253 f,
254 "Array '{}' not fully initialized: expected {} elements, but got {}",
255 shape.blue(),
256 expected_size,
257 pushed_count
258 )
259 }
260 ReflectError::ArrayIndexOutOfBounds { shape, index, size } => {
261 write!(
262 f,
263 "Array index {} out of bounds for '{}' (array length is {})",
264 index,
265 shape.blue(),
266 size
267 )
268 }
269 ReflectError::InvalidOperation { operation, reason } => {
270 write!(f, "Invalid operation '{}': {}", operation.yellow(), reason)
271 }
272 ReflectError::NoActiveFrame => {
273 write!(f, "No active frame in Partial")
274 }
275 }
276 }
277}
278
279impl core::fmt::Debug for ReflectError<'_> {
280 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
281 write!(f, "ReflectError({})", self)
283 }
284}
285
286impl core::error::Error for ReflectError<'_> {}