1use facet_core::{Characteristic, EnumDef, Field, FieldError, Shape};
2
3#[derive(Debug)]
5#[non_exhaustive]
6pub enum ReflectError {
7 PartiallyInitialized {
9 field: Field,
11 },
12
13 NoSuchVariant {
15 enum_def: EnumDef,
17 },
18
19 WrongShape {
22 expected: &'static Shape,
24 actual: &'static Shape,
26 },
27
28 WasNotA {
30 expected: &'static str,
32
33 actual: &'static Shape,
35 },
36
37 UninitializedField {
39 shape: &'static Shape,
41 field_name: &'static str,
43 },
44
45 UninitializedEnumField {
47 shape: &'static Shape,
49 field_name: &'static str,
51 variant_name: &'static str,
53 },
54
55 NoVariantSelected {
57 shape: &'static Shape,
59 },
60
61 UninitializedValue {
63 shape: &'static Shape,
65 },
66
67 InvariantViolation {
69 invariant: &'static str,
71 },
72
73 MissingCharacteristic {
75 shape: &'static Shape,
77 characteristic: Characteristic,
79 },
80
81 OperationFailed {
83 shape: &'static Shape,
85 operation: &'static str,
87 },
88
89 FieldError {
91 shape: &'static Shape,
93 field_error: FieldError,
95 },
96
97 Unknown,
99}
100
101impl core::fmt::Display for ReflectError {
102 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
103 match self {
104 ReflectError::PartiallyInitialized { field } => {
105 write!(
106 f,
107 "Value partially initialized: field {} was not set",
108 field.name
109 )
110 }
111 ReflectError::NoSuchVariant { enum_def } => {
112 write!(f, "No such variant in enum. Known variants: ")?;
113 for v in enum_def.variants {
114 write!(f, ", {}", v.name)?;
115 }
116 write!(f, ", that's it.")
117 }
118 ReflectError::WrongShape { expected, actual } => {
119 write!(f, "Wrong shape: expected {}, but got {}", expected, actual)
120 }
121 ReflectError::WasNotA { expected, actual } => {
122 write!(f, "Wrong shape: expected {}, but got {}", expected, actual)
123 }
124 ReflectError::UninitializedField { shape, field_name } => {
125 write!(f, "Field '{}::{}' was not initialized", shape, field_name)
126 }
127 ReflectError::UninitializedEnumField {
128 shape,
129 field_name,
130 variant_name,
131 } => {
132 write!(
133 f,
134 "Field '{}::{}' in variant '{}' was not initialized",
135 shape, field_name, variant_name
136 )
137 }
138 ReflectError::NoVariantSelected { shape } => {
139 write!(f, "Enum '{}' had no variant selected", shape)
140 }
141 ReflectError::UninitializedValue { shape } => {
142 write!(f, "Value '{}' was not initialized", shape)
143 }
144 ReflectError::InvariantViolation { invariant } => {
145 write!(f, "Invariant violation: {}", invariant)
146 }
147 ReflectError::MissingCharacteristic {
148 shape,
149 characteristic,
150 } => write!(
151 f,
152 "{shape} does not implement characteristic {characteristic:?}",
153 ),
154 ReflectError::OperationFailed { shape, operation } => {
155 write!(f, "Operation '{}' failed for shape {}", operation, shape)
156 }
157 ReflectError::FieldError { shape, field_error } => {
158 write!(f, "Field error for shape {}: {}", shape, field_error)
159 }
160 ReflectError::Unknown => write!(f, "Unknown error"),
161 }
162 }
163}
164
165impl core::error::Error for ReflectError {}