1use core::fmt;
2use core::marker::PhantomData;
3
4use crate::expecting::{self, Expecting};
5use crate::{Allocator, Context};
6
7use super::{Decoder, MapDecoder, SequenceDecoder, SizeHint, UnsizedVisitor, VariantDecoder};
8
9pub trait Visitor<'de, C>: Sized
15where
16 C: Context<Error = Self::Error, Allocator = Self::Allocator>,
17{
18 type Ok;
20 type Error;
22 type Allocator: Allocator;
24 type String: UnsizedVisitor<
26 'de,
27 C,
28 str,
29 Ok = Self::Ok,
30 Error = Self::Error,
31 Allocator = Self::Allocator,
32 >;
33 type Bytes: UnsizedVisitor<
35 'de,
36 C,
37 [u8],
38 Ok = Self::Ok,
39 Error = Self::Error,
40 Allocator = Self::Allocator,
41 >;
42
43 #[doc(hidden)]
47 type __UseMusliVisitorAttributeMacro;
48
49 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
52
53 #[inline]
55 fn visit_empty(self, cx: C) -> Result<Self::Ok, Self::Error> {
56 Err(cx.message(expecting::unsupported_type(
57 &expecting::Empty,
58 ExpectingWrapper::new(&self),
59 )))
60 }
61
62 #[inline]
64 fn visit_bool(self, cx: C, _: bool) -> Result<Self::Ok, Self::Error> {
65 Err(cx.message(expecting::unsupported_type(
66 &expecting::Bool,
67 ExpectingWrapper::new(&self),
68 )))
69 }
70
71 #[inline]
73 fn visit_char(self, cx: C, _: char) -> Result<Self::Ok, Self::Error> {
74 Err(cx.message(expecting::unsupported_type(
75 &expecting::Char,
76 ExpectingWrapper::new(&self),
77 )))
78 }
79
80 #[inline]
82 fn visit_u8(self, cx: C, _: u8) -> Result<Self::Ok, Self::Error> {
83 Err(cx.message(expecting::unsupported_type(
84 &expecting::Unsigned8,
85 ExpectingWrapper::new(&self),
86 )))
87 }
88
89 #[inline]
91 fn visit_u16(self, cx: C, _: u16) -> Result<Self::Ok, Self::Error> {
92 Err(cx.message(expecting::unsupported_type(
93 &expecting::Unsigned16,
94 ExpectingWrapper::new(&self),
95 )))
96 }
97
98 #[inline]
100 fn visit_u32(self, cx: C, _: u32) -> Result<Self::Ok, Self::Error> {
101 Err(cx.message(expecting::unsupported_type(
102 &expecting::Unsigned32,
103 ExpectingWrapper::new(&self),
104 )))
105 }
106
107 #[inline]
109 fn visit_u64(self, cx: C, _: u64) -> Result<Self::Ok, Self::Error> {
110 Err(cx.message(expecting::unsupported_type(
111 &expecting::Unsigned64,
112 ExpectingWrapper::new(&self),
113 )))
114 }
115
116 #[inline]
118 fn visit_u128(self, cx: C, _: u128) -> Result<Self::Ok, Self::Error> {
119 Err(cx.message(expecting::unsupported_type(
120 &expecting::Unsigned128,
121 ExpectingWrapper::new(&self),
122 )))
123 }
124
125 #[inline]
127 fn visit_i8(self, cx: C, _: i8) -> Result<Self::Ok, Self::Error> {
128 Err(cx.message(expecting::unsupported_type(
129 &expecting::Signed8,
130 ExpectingWrapper::new(&self),
131 )))
132 }
133
134 #[inline]
136 fn visit_i16(self, cx: C, _: i16) -> Result<Self::Ok, Self::Error> {
137 Err(cx.message(expecting::unsupported_type(
138 &expecting::Signed16,
139 ExpectingWrapper::new(&self),
140 )))
141 }
142
143 #[inline]
145 fn visit_i32(self, cx: C, _: i32) -> Result<Self::Ok, Self::Error> {
146 Err(cx.message(expecting::unsupported_type(
147 &expecting::Signed32,
148 ExpectingWrapper::new(&self),
149 )))
150 }
151
152 #[inline]
154 fn visit_i64(self, cx: C, _: i64) -> Result<Self::Ok, Self::Error> {
155 Err(cx.message(expecting::unsupported_type(
156 &expecting::Signed64,
157 ExpectingWrapper::new(&self),
158 )))
159 }
160
161 #[inline]
163 fn visit_i128(self, cx: C, _: i128) -> Result<Self::Ok, Self::Error> {
164 Err(cx.message(expecting::unsupported_type(
165 &expecting::Signed128,
166 ExpectingWrapper::new(&self),
167 )))
168 }
169
170 #[inline]
172 fn visit_usize(self, cx: C, _: usize) -> Result<Self::Ok, Self::Error> {
173 Err(cx.message(expecting::unsupported_type(
174 &expecting::Usize,
175 ExpectingWrapper::new(&self),
176 )))
177 }
178
179 #[inline]
181 fn visit_isize(self, cx: C, _: isize) -> Result<Self::Ok, Self::Error> {
182 Err(cx.message(expecting::unsupported_type(
183 &expecting::Isize,
184 ExpectingWrapper::new(&self),
185 )))
186 }
187
188 #[inline]
190 fn visit_f32(self, cx: C, _: f32) -> Result<Self::Ok, Self::Error> {
191 Err(cx.message(expecting::unsupported_type(
192 &expecting::Float32,
193 ExpectingWrapper::new(&self),
194 )))
195 }
196
197 #[inline]
199 fn visit_f64(self, cx: C, _: f64) -> Result<Self::Ok, Self::Error> {
200 Err(cx.message(expecting::unsupported_type(
201 &expecting::Float64,
202 ExpectingWrapper::new(&self),
203 )))
204 }
205
206 #[inline]
208 fn visit_none(self, cx: C) -> Result<Self::Ok, Self::Error> {
209 Err(cx.message(expecting::unsupported_type(
210 &expecting::Option,
211 ExpectingWrapper::new(&self),
212 )))
213 }
214
215 #[inline]
217 fn visit_some<D>(self, decoder: D) -> Result<Self::Ok, Self::Error>
218 where
219 D: Decoder<'de, Cx = C, Error = C::Error, Allocator = C::Allocator>,
220 {
221 Err(decoder.cx().message(expecting::unsupported_type(
222 &expecting::Option,
223 ExpectingWrapper::new(&self),
224 )))
225 }
226
227 #[inline]
229 fn visit_sequence<D>(self, decoder: &mut D) -> Result<Self::Ok, Self::Error>
230 where
231 D: ?Sized + SequenceDecoder<'de, Cx = C, Error = Self::Error, Allocator = Self::Allocator>,
232 {
233 Err(decoder.cx().message(expecting::unsupported_type(
234 &expecting::SequenceWith(decoder.size_hint()),
235 ExpectingWrapper::new(&self),
236 )))
237 }
238
239 #[inline]
241 fn visit_map<D>(self, decoder: &mut D) -> Result<Self::Ok, Self::Error>
242 where
243 D: ?Sized + MapDecoder<'de, Cx = C, Error = Self::Error, Allocator = Self::Allocator>,
244 {
245 Err(decoder.cx().message(expecting::unsupported_type(
246 &expecting::MapWith(decoder.size_hint()),
247 ExpectingWrapper::new(&self),
248 )))
249 }
250
251 #[inline]
253 fn visit_string(self, cx: C, hint: SizeHint) -> Result<Self::String, Self::Error> {
254 Err(cx.message(expecting::unsupported_type(
255 &expecting::StringWith(hint),
256 ExpectingWrapper::new(&self),
257 )))
258 }
259
260 #[inline]
262 fn visit_bytes(self, cx: C, hint: SizeHint) -> Result<Self::Bytes, Self::Error> {
263 Err(cx.message(expecting::unsupported_type(
264 &expecting::BytesWith(hint),
265 ExpectingWrapper::new(&self),
266 )))
267 }
268
269 #[inline]
271 fn visit_variant<D>(self, decoder: &mut D) -> Result<Self::Ok, Self::Error>
272 where
273 D: ?Sized + VariantDecoder<'de, Cx = C, Error = C::Error, Allocator = C::Allocator>,
274 {
275 Err(decoder.cx().message(expecting::unsupported_type(
276 &expecting::Variant,
277 ExpectingWrapper::new(&self),
278 )))
279 }
280
281 #[inline]
283 fn visit_unknown<D>(self, decoder: D) -> Result<Self::Ok, D::Error>
284 where
285 D: Decoder<'de, Cx = C, Error = C::Error, Allocator = C::Allocator>,
286 {
287 Err(decoder.cx().message(expecting::unsupported_type(
288 &expecting::Any,
289 ExpectingWrapper::new(&self),
290 )))
291 }
292}
293
294#[repr(transparent)]
295struct ExpectingWrapper<T, C> {
296 inner: T,
297 _marker: PhantomData<C>,
298}
299
300impl<T, C> ExpectingWrapper<T, C> {
301 fn new(inner: &T) -> &Self {
302 unsafe { &*(inner as *const T as *const Self) }
304 }
305}
306
307impl<'de, T, C> Expecting for ExpectingWrapper<T, C>
308where
309 C: Context,
310 T: Visitor<'de, C, Error = C::Error, Allocator = C::Allocator>,
311{
312 #[inline]
313 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
314 self.inner.expecting(f)
315 }
316}