1use facet_core::{Characteristic, EnumType, FieldError, Shape, TryFromError};
2use owo_colors::OwoColorize;
3
4#[derive(Debug, 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 NoVariantSelected {
52 shape: &'shape Shape<'shape>,
54 },
55
56 UninitializedValue {
58 shape: &'shape Shape<'shape>,
60 },
61
62 InvariantViolation {
64 invariant: &'shape str,
66 },
67
68 MissingCharacteristic {
70 shape: &'shape Shape<'shape>,
72 characteristic: Characteristic,
74 },
75
76 OperationFailed {
78 shape: &'shape Shape<'shape>,
80 operation: &'shape str,
82 },
83
84 FieldError {
86 shape: &'shape Shape<'shape>,
88 field_error: FieldError,
90 },
91
92 Unknown,
94
95 TryFromError {
97 src_shape: &'shape Shape<'shape>,
99
100 dst_shape: &'shape Shape<'shape>,
102
103 inner: TryFromError<'shape>,
105 },
106
107 DefaultAttrButNoDefaultImpl {
109 shape: &'shape Shape<'shape>,
111 },
112
113 Unsized {
115 shape: &'shape Shape<'shape>,
117 },
118
119 ArrayNotFullyInitialized {
121 shape: &'shape Shape<'shape>,
123 pushed_count: usize,
125 expected_size: usize,
127 },
128
129 ArrayIndexOutOfBounds {
131 shape: &'shape Shape<'shape>,
133 index: usize,
135 size: usize,
137 },
138}
139
140impl core::fmt::Display for ReflectError<'_> {
141 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
142 match self {
143 ReflectError::NoSuchVariant { enum_type } => {
144 write!(f, "No such variant in enum. Known variants: ")?;
145 for v in enum_type.variants {
146 write!(f, ", {}", v.name.cyan())?;
147 }
148 write!(f, ", that's it.")
149 }
150 ReflectError::WrongShape { expected, actual } => {
151 write!(
152 f,
153 "Wrong shape: expected {}, but got {}",
154 expected.green(),
155 actual.red()
156 )
157 }
158 ReflectError::WasNotA { expected, actual } => {
159 write!(
160 f,
161 "Wrong shape: expected {}, but got {}",
162 expected.green(),
163 actual.red()
164 )
165 }
166 ReflectError::UninitializedField { shape, field_name } => {
167 write!(f, "Field '{}::{}' was not initialized", shape, field_name)
168 }
169 ReflectError::UninitializedEnumField {
170 shape,
171 field_name,
172 variant_name,
173 } => {
174 write!(
175 f,
176 "Field '{}::{}' in variant '{}' was not initialized",
177 shape.blue(),
178 field_name.yellow(),
179 variant_name.red()
180 )
181 }
182 ReflectError::NoVariantSelected { shape } => {
183 write!(f, "Enum '{}' had no variant selected", shape.blue())
184 }
185 ReflectError::UninitializedValue { shape } => {
186 write!(f, "Value '{}' was not initialized", shape.blue())
187 }
188 ReflectError::InvariantViolation { invariant } => {
189 write!(f, "Invariant violation: {}", invariant.red())
190 }
191 ReflectError::MissingCharacteristic {
192 shape,
193 characteristic,
194 } => write!(
195 f,
196 "{shape} does not implement characteristic {characteristic:?}",
197 ),
198 ReflectError::OperationFailed { shape, operation } => {
199 write!(
200 f,
201 "Operation failed on shape {}: {}",
202 shape.blue(),
203 operation
204 )
205 }
206 ReflectError::FieldError { shape, field_error } => {
207 write!(f, "Field error for shape {}: {}", shape.red(), field_error)
208 }
209 ReflectError::Unknown => write!(f, "Unknown error"),
210 ReflectError::TryFromError {
211 src_shape,
212 dst_shape,
213 inner,
214 } => {
215 write!(
216 f,
217 "While trying to put {} into a {}: {}",
218 src_shape.green(),
219 dst_shape.blue(),
220 inner.red()
221 )
222 }
223 ReflectError::DefaultAttrButNoDefaultImpl { shape } => write!(
224 f,
225 "Shape '{}' has a `default` attribute but no default implementation",
226 shape.red()
227 ),
228 ReflectError::Unsized { shape } => write!(f, "Shape '{}' is unsized", shape.red()),
229 ReflectError::ArrayNotFullyInitialized {
230 shape,
231 pushed_count,
232 expected_size,
233 } => {
234 write!(
235 f,
236 "Array '{}' not fully initialized: expected {} elements, but got {}",
237 shape.blue(),
238 expected_size,
239 pushed_count
240 )
241 }
242 ReflectError::ArrayIndexOutOfBounds { shape, index, size } => {
243 write!(
244 f,
245 "Array index {} out of bounds for '{}' (array length is {})",
246 index,
247 shape.blue(),
248 size
249 )
250 }
251 }
252 }
253}
254
255impl core::error::Error for ReflectError<'_> {}