facet_reflect/
error.rs

1use facet_core::{Characteristic, EnumDef, Field, FieldError, Shape};
2
3/// Errors that can occur when reflecting on types.
4#[derive(Debug)]
5#[non_exhaustive]
6pub enum ReflectError {
7    /// Tried to `build` or `build_in_place` a struct/enum without initializing all fields.
8    PartiallyInitialized {
9        /// The field that was not initialized.
10        field: Field,
11    },
12
13    /// Tried to set an enum to a variant that does not exist
14    NoSuchVariant {
15        /// The enum definition containing all known variants.
16        enum_def: EnumDef,
17    },
18
19    /// Tried to get the wrong shape out of a value — e.g. we were manipulating
20    /// a `String`, but `.get()` was called with a `u64` or something.
21    WrongShape {
22        /// The expected shape of the value.
23        expected: &'static Shape,
24        /// The actual shape of the value.
25        actual: &'static Shape,
26    },
27
28    /// Attempted to perform an operation that expected a struct or something
29    WasNotA {
30        /// The name of the expected type.
31        expected: &'static str,
32
33        /// The type we got instead
34        actual: &'static Shape,
35    },
36
37    /// A field was not initialized during build
38    UninitializedField {
39        /// The shape containing the field
40        shape: &'static Shape,
41        /// The name of the field that wasn't initialized
42        field_name: &'static str,
43    },
44
45    /// A field in an enum variant was not initialized during build
46    UninitializedEnumField {
47        /// The enum shape
48        shape: &'static Shape,
49        /// The name of the field that wasn't initialized
50        field_name: &'static str,
51        /// The name of the variant containing the field
52        variant_name: &'static str,
53    },
54
55    /// An enum had no variant selected during build
56    NoVariantSelected {
57        /// The enum shape
58        shape: &'static Shape,
59    },
60
61    /// A scalar value was not initialized during build
62    UninitializedValue {
63        /// The scalar shape
64        shape: &'static Shape,
65    },
66
67    /// An invariant of the reflection system was violated.
68    InvariantViolation {
69        /// The invariant that was violated.
70        invariant: &'static str,
71    },
72
73    /// Attempted to set a value to its default, but the value doesn't implement `Default`.
74    MissingCharacteristic {
75        /// The shape of the value that doesn't implement `Default`.
76        shape: &'static Shape,
77        /// The characteristic that is missing.
78        characteristic: Characteristic,
79    },
80
81    /// An operation failed for a given shape
82    OperationFailed {
83        /// The shape of the value for which the operation failed.
84        shape: &'static Shape,
85        /// The name of the operation that failed.
86        operation: &'static str,
87    },
88
89    /// An error occurred when attempting to access or modify a field.
90    FieldError {
91        /// The shape of the value containing the field.
92        shape: &'static Shape,
93        /// The specific error that occurred with the field.
94        field_error: FieldError,
95    },
96
97    /// An unknown error occurred.
98    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 {}