facet_reflect/error.rs
1use facet_core::{Characteristic, EnumDef, Field, FieldError, Shape, TryFromError};
2use owo_colors::OwoColorize;
3
4/// Errors that can occur when reflecting on types.
5#[derive(Debug, PartialEq, Clone)]
6#[non_exhaustive]
7pub enum ReflectError {
8 /// Tried to `build` or `build_in_place` a struct/enum without initializing all fields.
9 PartiallyInitialized {
10 /// The field that was not initialized.
11 field: Field,
12 },
13
14 /// Tried to set an enum to a variant that does not exist
15 NoSuchVariant {
16 /// The enum definition containing all known variants.
17 enum_def: EnumDef,
18 },
19
20 /// Tried to get the wrong shape out of a value — e.g. we were manipulating
21 /// a `String`, but `.get()` was called with a `u64` or something.
22 WrongShape {
23 /// The expected shape of the value.
24 expected: &'static Shape,
25 /// The actual shape of the value.
26 actual: &'static Shape,
27 },
28
29 /// Attempted to perform an operation that expected a struct or something
30 WasNotA {
31 /// The name of the expected type.
32 expected: &'static str,
33
34 /// The type we got instead
35 actual: &'static Shape,
36 },
37
38 /// A field was not initialized during build
39 UninitializedField {
40 /// The shape containing the field
41 shape: &'static Shape,
42 /// The name of the field that wasn't initialized
43 field_name: &'static str,
44 },
45
46 /// A field in an enum variant was not initialized during build
47 UninitializedEnumField {
48 /// The enum shape
49 shape: &'static Shape,
50 /// The name of the field that wasn't initialized
51 field_name: &'static str,
52 /// The name of the variant containing the field
53 variant_name: &'static str,
54 },
55
56 /// An enum had no variant selected during build
57 NoVariantSelected {
58 /// The enum shape
59 shape: &'static Shape,
60 },
61
62 /// A scalar value was not initialized during build
63 UninitializedValue {
64 /// The scalar shape
65 shape: &'static Shape,
66 },
67
68 /// An invariant of the reflection system was violated.
69 InvariantViolation {
70 /// The invariant that was violated.
71 invariant: &'static str,
72 },
73
74 /// Attempted to set a value to its default, but the value doesn't implement `Default`.
75 MissingCharacteristic {
76 /// The shape of the value that doesn't implement `Default`.
77 shape: &'static Shape,
78 /// The characteristic that is missing.
79 characteristic: Characteristic,
80 },
81
82 /// An operation failed for a given shape
83 OperationFailed {
84 /// The shape of the value for which the operation failed.
85 shape: &'static Shape,
86 /// The name of the operation that failed.
87 operation: &'static str,
88 },
89
90 /// An error occurred when attempting to access or modify a field.
91 FieldError {
92 /// The shape of the value containing the field.
93 shape: &'static Shape,
94 /// The specific error that occurred with the field.
95 field_error: FieldError,
96 },
97
98 /// An unknown error occurred.
99 Unknown,
100
101 /// An error occured while putting
102 TryFromError {
103 /// The shape of the value being converted from.
104 src_shape: &'static Shape,
105
106 /// The shape of the value being converted to.
107 dst_shape: &'static Shape,
108
109 /// The inner error
110 inner: TryFromError,
111 },
112
113 /// A shape has a `default` attribute, but no implementation of the `Default` trait.
114 DefaultAttrButNoDefaultImpl {
115 /// The shape of the value that has a `default` attribute but no default implementation.
116 shape: &'static Shape,
117 },
118
119 /// The type is unsized
120 Unsized {
121 /// The shape for the type that is unsized
122 shape: &'static Shape,
123 },
124}
125
126impl core::fmt::Display for ReflectError {
127 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
128 match self {
129 ReflectError::PartiallyInitialized { field } => {
130 write!(
131 f,
132 "Value partially initialized: field {} was not set",
133 field.name
134 )
135 }
136 ReflectError::NoSuchVariant { enum_def } => {
137 write!(f, "No such variant in enum. Known variants: ")?;
138 for v in enum_def.variants {
139 write!(f, ", {}", v.name)?;
140 }
141 write!(f, ", that's it.")
142 }
143 ReflectError::WrongShape { expected, actual } => {
144 write!(
145 f,
146 "Wrong shape: expected {}, but got {}",
147 expected.green(),
148 actual.red()
149 )
150 }
151 ReflectError::WasNotA { expected, actual } => {
152 write!(
153 f,
154 "Wrong shape: expected {}, but got {}",
155 expected.green(),
156 actual.red()
157 )
158 }
159 ReflectError::UninitializedField { shape, field_name } => {
160 write!(f, "Field '{}::{}' was not initialized", shape, field_name)
161 }
162 ReflectError::UninitializedEnumField {
163 shape,
164 field_name,
165 variant_name,
166 } => {
167 write!(
168 f,
169 "Field '{}::{}' in variant '{}' was not initialized",
170 shape, field_name, variant_name
171 )
172 }
173 ReflectError::NoVariantSelected { shape } => {
174 write!(f, "Enum '{}' had no variant selected", shape)
175 }
176 ReflectError::UninitializedValue { shape } => {
177 write!(f, "Value '{}' was not initialized", shape)
178 }
179 ReflectError::InvariantViolation { invariant } => {
180 write!(f, "Invariant violation: {}", invariant)
181 }
182 ReflectError::MissingCharacteristic {
183 shape,
184 characteristic,
185 } => write!(
186 f,
187 "{shape} does not implement characteristic {characteristic:?}",
188 ),
189 ReflectError::OperationFailed { shape, operation } => {
190 write!(f, "Operation '{}' failed for shape {}", operation, shape)
191 }
192 ReflectError::FieldError { shape, field_error } => {
193 write!(f, "Field error for shape {}: {}", shape, field_error)
194 }
195 ReflectError::Unknown => write!(f, "Unknown error"),
196 ReflectError::TryFromError {
197 src_shape,
198 dst_shape,
199 inner,
200 } => {
201 write!(
202 f,
203 "While trying to put {} into a {}: {}",
204 src_shape.green(),
205 dst_shape.blue(),
206 inner.red()
207 )
208 }
209 ReflectError::DefaultAttrButNoDefaultImpl { shape } => write!(
210 f,
211 "Shape '{}' has a `default` attribute but no default implementation",
212 shape
213 ),
214 ReflectError::Unsized { shape } => write!(f, "Shape '{}' is unsized", shape),
215 }
216 }
217}
218
219impl core::error::Error for ReflectError {}