facet_format/deserializer/
error.rs1extern crate alloc;
2
3use alloc::string::String;
4use core::fmt;
5use facet_path::Path;
6use facet_reflect::ReflectError;
7
8#[derive(Debug)]
10pub enum DeserializeError<E> {
11 Parser(E),
13 Reflect {
15 error: ReflectError,
17 span: Option<facet_reflect::Span>,
19 path: Option<Path>,
21 },
22 TypeMismatch {
24 expected: &'static str,
26 got: String,
28 span: Option<facet_reflect::Span>,
30 path: Option<Path>,
32 },
33 Unsupported(String),
35 UnknownField {
37 field: String,
39 span: Option<facet_reflect::Span>,
41 path: Option<Path>,
43 },
44 CannotBorrow {
46 message: String,
48 },
49 MissingField {
51 field: &'static str,
53 type_name: &'static str,
55 span: Option<facet_reflect::Span>,
57 path: Option<Path>,
59 },
60 #[cfg(feature = "validate")]
62 Validation {
63 field: &'static str,
65 message: String,
67 span: Option<facet_reflect::Span>,
69 path: Option<Path>,
71 },
72 UnexpectedEof {
74 expected: &'static str,
76 },
77}
78
79impl<E: fmt::Display> fmt::Display for DeserializeError<E> {
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81 match self {
82 DeserializeError::Parser(err) => write!(f, "{err}"),
83 DeserializeError::Reflect { error, .. } => write!(f, "{error}"),
84 DeserializeError::TypeMismatch { expected, got, .. } => {
85 write!(f, "type mismatch: expected {expected}, got {got}")
86 }
87 DeserializeError::Unsupported(msg) => write!(f, "unsupported: {msg}"),
88 DeserializeError::UnknownField { field, .. } => write!(f, "unknown field: {field}"),
89 DeserializeError::CannotBorrow { message } => write!(f, "{message}"),
90 DeserializeError::MissingField {
91 field, type_name, ..
92 } => {
93 write!(f, "missing field `{field}` in type `{type_name}`")
94 }
95 #[cfg(feature = "validate")]
96 DeserializeError::Validation { field, message, .. } => {
97 write!(f, "validation failed for field `{field}`: {message}")
98 }
99 DeserializeError::UnexpectedEof { expected } => {
100 write!(f, "unexpected end of input, expected {expected}")
101 }
102 }
103 }
104}
105
106impl<E: fmt::Debug + fmt::Display> std::error::Error for DeserializeError<E> {}
107
108impl<E> DeserializeError<E> {
109 #[inline]
111 pub const fn reflect(error: ReflectError) -> Self {
112 DeserializeError::Reflect {
113 error,
114 span: None,
115 path: None,
116 }
117 }
118
119 #[inline]
121 pub const fn reflect_with_span(error: ReflectError, span: facet_reflect::Span) -> Self {
122 DeserializeError::Reflect {
123 error,
124 span: Some(span),
125 path: None,
126 }
127 }
128
129 #[inline]
131 pub const fn reflect_with_context(
132 error: ReflectError,
133 span: Option<facet_reflect::Span>,
134 path: Path,
135 ) -> Self {
136 DeserializeError::Reflect {
137 error,
138 span,
139 path: Some(path),
140 }
141 }
142
143 pub const fn path(&self) -> Option<&Path> {
145 match self {
146 DeserializeError::Reflect { path, .. } => path.as_ref(),
147 DeserializeError::TypeMismatch { path, .. } => path.as_ref(),
148 DeserializeError::UnknownField { path, .. } => path.as_ref(),
149 DeserializeError::MissingField { path, .. } => path.as_ref(),
150 _ => None,
151 }
152 }
153
154 pub fn with_path(self, new_path: Path) -> Self {
156 match self {
157 DeserializeError::Reflect { error, span, .. } => DeserializeError::Reflect {
158 error,
159 span,
160 path: Some(new_path),
161 },
162 DeserializeError::TypeMismatch {
163 expected,
164 got,
165 span,
166 ..
167 } => DeserializeError::TypeMismatch {
168 expected,
169 got,
170 span,
171 path: Some(new_path),
172 },
173 DeserializeError::UnknownField { field, span, .. } => DeserializeError::UnknownField {
174 field,
175 span,
176 path: Some(new_path),
177 },
178 DeserializeError::MissingField {
179 field,
180 type_name,
181 span,
182 ..
183 } => DeserializeError::MissingField {
184 field,
185 type_name,
186 span,
187 path: Some(new_path),
188 },
189 other => other,
191 }
192 }
193}