musli_core/de/
visitor.rs

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
9/// Visitor capable of decoding any type into a value [`Visitor::Ok`].
10///
11/// Each callback on this visitor indicates the type that should be decoded from
12/// the passed in decoder. A typical implementation would simply call the
13/// corresponding decoder function for the type being visited.
14pub trait Visitor<'de, C>: Sized
15where
16    C: Context<Error = Self::Error, Allocator = Self::Allocator>,
17{
18    /// The value produced by the visitor.
19    type Ok;
20    /// The error produced by the visitor.
21    type Error;
22    /// The allocator associated with the visitor.
23    type Allocator: Allocator;
24    /// String decoder to use.
25    type String: UnsizedVisitor<
26        'de,
27        C,
28        str,
29        Ok = Self::Ok,
30        Error = Self::Error,
31        Allocator = Self::Allocator,
32    >;
33    /// Bytes decoder to use.
34    type Bytes: UnsizedVisitor<
35        'de,
36        C,
37        [u8],
38        Ok = Self::Ok,
39        Error = Self::Error,
40        Allocator = Self::Allocator,
41    >;
42
43    /// This is a type argument used to hint to any future implementor that they
44    /// should be using the [`#[musli::de::visitor]`][musli::de::visitor]
45    /// attribute macro when implementing [`Visitor`].
46    #[doc(hidden)]
47    type __UseMusliVisitorAttributeMacro;
48
49    /// Format the human-readable message that should occur if the decoder was
50    /// expecting to decode some specific kind of value.
51    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
52
53    /// Indicates that the visited type is empty.
54    #[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    /// Indicates that the visited type is a `bool`.
63    #[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    /// Indicates that the visited type is a `char`.
72    #[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    /// Indicates that the visited type is a `u8`.
81    #[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    /// Indicates that the visited type is a `u16`.
90    #[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    /// Indicates that the visited type is a `u32`.
99    #[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    /// Indicates that the visited type is a `u64`.
108    #[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    /// Indicates that the visited type is a `u128`.
117    #[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    /// Indicates that the visited type is a `i8`.
126    #[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    /// Indicates that the visited type is a `i16`.
135    #[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    /// Indicates that the visited type is a `i32`.
144    #[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    /// Indicates that the visited type is a `i64`.
153    #[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    /// Indicates that the visited type is a `i128`.
162    #[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    /// Indicates that the visited type is a `usize`.
171    #[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    /// Indicates that the visited type is a `isize`.
180    #[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    /// Indicates that the visited type is a `f32`.
189    #[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    /// Indicates that the visited type is a `f64`.
198    #[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    /// Indicates that the visited type is an optional value that is absent.
207    #[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    /// Indicates that the visited type is an optional value that is present.
216    #[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    /// Indicates that the visited type is a sequence.
228    #[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    /// Indicates that the visited type is a map.
240    #[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    /// Indicates that the visited type is `string`.
252    #[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    /// Indicates that the visited type is `bytes`.
261    #[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    /// Indicates that the visited type is a variant.
270    #[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    /// Indicates that the encoding does not support dynamic types.
282    #[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        // SAFETY: `ExpectingWrapper` is repr(transparent) over `T`.
303        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}