1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
extern crate alloc;
use alloc::string::String;
use core::fmt;
use facet_path::Path;
use facet_reflect::ReflectError;
/// Error produced by the format deserializer.
#[derive(Debug)]
pub enum DeserializeError<E> {
/// Error emitted by the format-specific parser.
Parser(E),
/// Reflection error from Partial operations.
Reflect {
/// The underlying reflection error.
error: ReflectError,
/// Source span where the error occurred (if available).
span: Option<facet_reflect::Span>,
/// Path through the type structure where the error occurred.
path: Option<Path>,
},
/// Type mismatch during deserialization.
TypeMismatch {
/// The expected type or token.
expected: &'static str,
/// The actual type or token that was encountered.
got: String,
/// Source span where the mismatch occurred (if available).
span: Option<facet_reflect::Span>,
/// Path through the type structure where the error occurred.
path: Option<Path>,
},
/// Unsupported type or operation.
Unsupported(String),
/// Unknown field encountered when deny_unknown_fields is set.
UnknownField {
/// The unknown field name.
field: String,
/// Source span where the unknown field was found (if available).
span: Option<facet_reflect::Span>,
/// Path through the type structure where the error occurred.
path: Option<Path>,
},
/// Cannot borrow string from input (e.g., escaped string into &str).
CannotBorrow {
/// Description of why borrowing failed.
message: String,
},
/// Required field missing from input.
MissingField {
/// The field that is missing.
field: &'static str,
/// The type that contains the field.
type_name: &'static str,
/// Source span where the struct was being parsed (if available).
span: Option<facet_reflect::Span>,
/// Path through the type structure where the error occurred.
path: Option<Path>,
},
/// Field validation failed.
#[cfg(feature = "validate")]
Validation {
/// The field that failed validation.
field: &'static str,
/// The validation error message.
message: String,
/// Source span where the invalid value was found.
span: Option<facet_reflect::Span>,
/// Path through the type structure where the error occurred.
path: Option<Path>,
},
/// Unexpected end of input.
UnexpectedEof {
/// What was expected before EOF.
expected: &'static str,
},
}
impl<E: fmt::Display> fmt::Display for DeserializeError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DeserializeError::Parser(err) => write!(f, "{err}"),
DeserializeError::Reflect { error, .. } => write!(f, "{error}"),
DeserializeError::TypeMismatch { expected, got, .. } => {
write!(f, "type mismatch: expected {expected}, got {got}")
}
DeserializeError::Unsupported(msg) => write!(f, "unsupported: {msg}"),
DeserializeError::UnknownField { field, .. } => write!(f, "unknown field: {field}"),
DeserializeError::CannotBorrow { message } => write!(f, "{message}"),
DeserializeError::MissingField {
field, type_name, ..
} => {
write!(f, "missing field `{field}` in type `{type_name}`")
}
#[cfg(feature = "validate")]
DeserializeError::Validation { field, message, .. } => {
write!(f, "validation failed for field `{field}`: {message}")
}
DeserializeError::UnexpectedEof { expected } => {
write!(f, "unexpected end of input, expected {expected}")
}
}
}
}
impl<E: fmt::Debug + fmt::Display> std::error::Error for DeserializeError<E> {}
impl<E> DeserializeError<E> {
/// Create a Reflect error without span or path information.
#[inline]
pub const fn reflect(error: ReflectError) -> Self {
DeserializeError::Reflect {
error,
span: None,
path: None,
}
}
/// Create a Reflect error with span information.
#[inline]
pub const fn reflect_with_span(error: ReflectError, span: facet_reflect::Span) -> Self {
DeserializeError::Reflect {
error,
span: Some(span),
path: None,
}
}
/// Create a Reflect error with span and path information.
#[inline]
pub const fn reflect_with_context(
error: ReflectError,
span: Option<facet_reflect::Span>,
path: Path,
) -> Self {
DeserializeError::Reflect {
error,
span,
path: Some(path),
}
}
/// Get the path where the error occurred, if available.
pub const fn path(&self) -> Option<&Path> {
match self {
DeserializeError::Reflect { path, .. } => path.as_ref(),
DeserializeError::TypeMismatch { path, .. } => path.as_ref(),
DeserializeError::UnknownField { path, .. } => path.as_ref(),
DeserializeError::MissingField { path, .. } => path.as_ref(),
_ => None,
}
}
/// Add path information to an error (consumes and returns the modified error).
pub fn with_path(self, new_path: Path) -> Self {
match self {
DeserializeError::Reflect { error, span, .. } => DeserializeError::Reflect {
error,
span,
path: Some(new_path),
},
DeserializeError::TypeMismatch {
expected,
got,
span,
..
} => DeserializeError::TypeMismatch {
expected,
got,
span,
path: Some(new_path),
},
DeserializeError::UnknownField { field, span, .. } => DeserializeError::UnknownField {
field,
span,
path: Some(new_path),
},
DeserializeError::MissingField {
field,
type_name,
span,
..
} => DeserializeError::MissingField {
field,
type_name,
span,
path: Some(new_path),
},
// Other variants don't have path fields
other => other,
}
}
}