Skip to main content

spacetimedb_sats/
de.rs

1// Some parts copyright Serde developers under the MIT / Apache-2.0 licenses at your option.
2// See `serde` version `v1.0.169` for the parts where MIT / Apache-2.0 applies.
3
4mod impls;
5#[cfg(any(test, feature = "serde"))]
6pub mod serde;
7
8#[doc(hidden)]
9pub use impls::{visit_named_product, visit_seq_product, WithBound};
10
11use crate::buffer::BufReader;
12use crate::{bsatn, i256, u256};
13use core::fmt;
14use core::marker::PhantomData;
15use smallvec::SmallVec;
16use std::borrow::Borrow;
17
18/// A data format that can deserialize any data structure supported by SATS.
19///
20/// The `Deserializer` trait in SATS performs the same function as `serde::Deserializer` in [`serde`].
21/// See the documentation of `serde::Deserializer` for more information of the data model.
22///
23/// Implementations of `Deserialize` map themselves into this data model
24/// by passing to the `Deserializer` a visitor that can receive the necessary types.
25/// The kind of visitor depends on the `deserialize_*` method.
26/// Unlike in Serde, there isn't a single monolithic `Visitor` trait,
27/// but rather, this functionality is split up into more targeted traits such as `SumVisitor<'de>`.
28///
29/// The lifetime `'de` allows us to deserialize lifetime-generic types in a zero-copy fashion.
30///
31/// [`serde`]: https://crates.io/crates/serde
32pub trait Deserializer<'de>: Sized {
33    /// The error type that can be returned if some error occurs during deserialization.
34    type Error: Error;
35
36    /// Deserializes a product value from the input.
37    fn deserialize_product<V: ProductVisitor<'de>>(self, visitor: V) -> Result<V::Output, Self::Error>;
38
39    /// Validates a product value from the input.
40    fn validate_product<V: ProductVisitor<'de>>(self, visitor: V) -> Result<(), Self::Error> {
41        self.deserialize_product(visitor).map(|_| ())
42    }
43
44    /// Deserializes a sum value from the input.
45    ///
46    /// The entire process of deserializing a sum, starting from `deserialize(args...)`, is roughly:
47    ///
48    /// - [`deserialize`][Deserialize::deserialize] calls this method,
49    ///   [`deserialize_sum(sum_visitor)`](Deserializer::deserialize_sum),
50    ///   providing us with a [`sum_visitor`](SumVisitor).
51    ///
52    /// - This method calls [`sum_visitor.visit_sum(sum_access)`](SumVisitor::visit_sum),
53    ///   where [`sum_access`](SumAccess) deals with extracting the tag and the variant data,
54    ///   with the latter provided as [`VariantAccess`]).
55    ///   The `SumVisitor` will then assemble these into the representation of a sum value
56    ///   that the [`Deserialize`] implementation wants.
57    ///
58    /// - [`visit_sum`](SumVisitor::visit_sum) then calls
59    ///   [`sum_access.variant(variant_visitor)`](SumAccess::variant),
60    ///   and uses the provided `variant_visitor` to translate extracted variant names / tags
61    ///   into something that is meaningful for `visit_sum`, e.g., an index.
62    ///
63    ///   The call to `variant` will also return [`variant_access`](VariantAccess)
64    ///   that can deserialize the contents of the variant.
65    ///
66    /// - Finally, after `variant` returns,
67    ///   `visit_sum` deserializes the variant data using
68    ///   [`variant_access.deserialize_seed(seed)`](VariantAccess::deserialize_seed)
69    ///   or [`variant_access.deserialize()`](VariantAccess::deserialize).
70    ///   This part may require some conditional logic depending on the identified variant.
71    ///
72    ///
73    /// The data format will also return an object ([`VariantAccess`])
74    /// that can deserialize the contents of the variant.
75    fn deserialize_sum<V: SumVisitor<'de>>(self, visitor: V) -> Result<V::Output, Self::Error>;
76
77    /// Validates a sum value from the input.
78    ///
79    /// The entire process of validating a sum, starting from `validate(args...)`, is roughly:
80    ///
81    /// - [`validate`][Deserialize::validate] calls this method,
82    ///   [`validate_sum(sum_visitor)`](Deserializer::validate_sum),
83    ///   providing us with a [`sum_visitor`](SumVisitor).
84    ///
85    /// - This method calls [`sum_visitor.validate_sum(sum_access)`](SumVisitor::validate_sum),
86    ///   where [`sum_access`](SumAccess) deals with extracting the tag and the variant data,
87    ///   with the latter provided as [`VariantAccess`]).
88    ///   The `SumVisitor` will then assemble these into the representation of a sum value
89    ///   that the [`Deserialize`] implementation wants.
90    ///
91    /// - [`validate_sum`](SumVisitor::validate_sum) then calls
92    ///   [`sum_access.variant(variant_visitor)`](SumAccess::variant),
93    ///   and uses the provided `variant_visitor` to translate extracted variant names / tags
94    ///   into something that is meaningful for `validate_sum`, e.g., an index.
95    ///
96    ///   The call to `variant` will also return [`variant_access`](VariantAccess)
97    ///   that can validate the contents of the variant.
98    ///
99    /// - Finally, after `variant` returns,
100    ///   `validate_sum` validates the variant data using
101    ///   [`variant_access.validate_seed(seed)`](VariantAccess::validate_seed)
102    ///   or [`variant_access.validate()`](VariantAccess::validate).
103    ///   This part may require some conditional logic depending on the identified variant.
104    ///
105    /// The data format will also return an object ([`VariantAccess`])
106    /// that can validate the contents of the variant.
107    fn validate_sum<V: SumVisitor<'de>>(self, visitor: V) -> Result<(), Self::Error> {
108        self.deserialize_sum(visitor).map(|_| ())
109    }
110
111    /// Deserializes a `bool` value from the input.
112    fn deserialize_bool(self) -> Result<bool, Self::Error>;
113
114    /// Deserializes a `u8` value from the input.
115    fn deserialize_u8(self) -> Result<u8, Self::Error>;
116
117    /// Deserializes a `u16` value from the input.
118    fn deserialize_u16(self) -> Result<u16, Self::Error>;
119
120    /// Deserializes a `u32` value from the input.
121    fn deserialize_u32(self) -> Result<u32, Self::Error>;
122
123    /// Deserializes a `u64` value from the input.
124    fn deserialize_u64(self) -> Result<u64, Self::Error>;
125
126    /// Deserializes a `u128` value from the input.
127    fn deserialize_u128(self) -> Result<u128, Self::Error>;
128
129    /// Deserializes a `u256` value from the input.
130    fn deserialize_u256(self) -> Result<u256, Self::Error>;
131
132    /// Deserializes an `i8` value from the input.
133    fn deserialize_i8(self) -> Result<i8, Self::Error>;
134
135    /// Deserializes an `i16` value from the input.
136    fn deserialize_i16(self) -> Result<i16, Self::Error>;
137
138    /// Deserializes an `i32` value from the input.
139    fn deserialize_i32(self) -> Result<i32, Self::Error>;
140
141    /// Deserializes an `i64` value from the input.
142    fn deserialize_i64(self) -> Result<i64, Self::Error>;
143
144    /// Deserializes an `i128` value from the input.
145    fn deserialize_i128(self) -> Result<i128, Self::Error>;
146
147    /// Deserializes an `i256` value from the input.
148    fn deserialize_i256(self) -> Result<i256, Self::Error>;
149
150    /// Deserializes an `f32` value from the input.
151    fn deserialize_f32(self) -> Result<f32, Self::Error>;
152
153    /// Deserializes an `f64` value from the input.
154    fn deserialize_f64(self) -> Result<f64, Self::Error>;
155
156    /// Deserializes a string-like object the input.
157    fn deserialize_str<V: SliceVisitor<'de, str>>(self, visitor: V) -> Result<V::Output, Self::Error>;
158
159    /// Deserializes an `&str` string value.
160    fn deserialize_str_slice(self) -> Result<&'de str, Self::Error> {
161        self.deserialize_str(BorrowedSliceVisitor)
162    }
163
164    /// Deserializes a byte slice-like value.
165    fn deserialize_bytes<V: SliceVisitor<'de, [u8]>>(self, visitor: V) -> Result<V::Output, Self::Error>;
166
167    /// Deserializes an array value.
168    ///
169    /// This is typically the same as [`deserialize_array_seed`](Deserializer::deserialize_array_seed)
170    /// with an uninteresting `seed` value.
171    fn deserialize_array<V: ArrayVisitor<'de, T>, T: Deserialize<'de>>(
172        self,
173        visitor: V,
174    ) -> Result<V::Output, Self::Error> {
175        self.deserialize_array_seed(visitor, PhantomData)
176    }
177
178    /// Deserializes an array value.
179    ///
180    /// The deserialization is provided with a `seed` value.
181    fn deserialize_array_seed<V: ArrayVisitor<'de, T::Output>, T: DeserializeSeed<'de> + Clone>(
182        self,
183        visitor: V,
184        seed: T,
185    ) -> Result<V::Output, Self::Error>;
186
187    /// Validates an array value.
188    ///
189    /// The validation is provided with a `seed` value.
190    fn validate_array_seed<V: ArrayVisitor<'de, T::Output>, T: DeserializeSeed<'de> + Clone>(
191        self,
192        visitor: V,
193        seed: T,
194    ) -> Result<(), Self::Error> {
195        self.deserialize_array_seed(visitor, seed).map(|_| ())
196    }
197}
198
199/// The `Error` trait allows [`Deserialize`] implementations to create descriptive error messages
200/// belonging to the [`Deserializer`] against which they are currently running.
201///
202/// Every [`Deserializer`] declares an [`Error`] type
203/// that encompasses both general-purpose deserialization errors
204/// as well as errors specific to the particular deserialization format.
205///
206/// Most deserializers should only need to provide the [`Error::custom`] method
207/// and inherit the default behavior for the other methods.
208pub trait Error: Sized {
209    /// Raised when there is general error when deserializing a type.
210    fn custom(msg: impl fmt::Display) -> Self;
211
212    /// Deserializing named products are not supported for this visitor.
213    fn named_products_not_supported() -> Self {
214        Self::custom("named products not supported")
215    }
216
217    /// The product length was not as promised.
218    fn invalid_product_length<'de, T: ProductVisitor<'de>>(len: usize, expected: &T) -> Self {
219        Self::custom(format_args!(
220            "invalid length {}, expected {}",
221            len,
222            fmt_invalid_len(expected)
223        ))
224    }
225
226    /// There was a missing field at `index`.
227    fn missing_field<'de, T: ProductVisitor<'de>>(index: usize, field_name: Option<&str>, prod: &T) -> Self {
228        Self::custom(error_on_field("missing ", index, field_name, prod))
229    }
230
231    /// A field with `index` was specified more than once.
232    fn duplicate_field<'de, T: ProductVisitor<'de>>(index: usize, field_name: Option<&str>, prod: &T) -> Self {
233        Self::custom(error_on_field("duplicate ", index, field_name, prod))
234    }
235
236    /// A field with name `field_name` does not exist.
237    fn unknown_field_name<'de, T: FieldNameVisitor<'de>>(field_name: &str, expected: &T) -> Self {
238        let el_ty = match expected.kind() {
239            ProductKind::Normal => "field",
240            ProductKind::ReducerArgs => "reducer argument",
241        };
242        match one_of_names(|| expected.field_names()) {
243            Some(one_of) => Self::custom(format_args!("unknown {el_ty} `{field_name}`, expected {one_of}")),
244            _ => Self::custom(format_args!("unknown {el_ty} `{field_name}`, there are no {el_ty}s")),
245        }
246    }
247
248    /// The `tag` does not specify a variant of the sum type.
249    fn unknown_variant_tag<'de, T: SumVisitor<'de>>(tag: u8, expected: &T) -> Self {
250        Self::custom(format_args!(
251            "unknown tag {tag:#x} for sum type {}",
252            expected.sum_name().unwrap_or("<unknown>"),
253        ))
254    }
255
256    /// The `name` is not that of a variant of the sum type.
257    fn unknown_variant_name<'de, T: VariantVisitor<'de>>(name: &str, expected: &T) -> Self {
258        match one_of_names(|| expected.variant_names().map(Some)) {
259            Some(one_of) => Self::custom(format_args!("unknown variant `{name}`, expected {one_of}",)),
260            _ => Self::custom(format_args!("unknown variant `{name}`, there are no variants")),
261        }
262    }
263
264    /// An allocation of `size` elements failed during deserialization.
265    fn allocation_failed(size: usize) -> Self {
266        Self::custom(format_args!("allocation of {size} elements failed"))
267    }
268}
269
270/// Turns a closure `impl Fn(&mut Formatter) -> Result` into a `Display`able object.
271pub struct FDisplay<F>(F);
272
273impl<F: Fn(&mut fmt::Formatter) -> fmt::Result> fmt::Display for FDisplay<F> {
274    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
275        (self.0)(f)
276    }
277}
278
279/// Turns a closure `F: Fn(&mut Formatter) -> Result` into a `Display`able object.
280pub fn fmt_fn<F: Fn(&mut fmt::Formatter) -> fmt::Result>(f: F) -> FDisplay<F> {
281    FDisplay(f)
282}
283
284/// Returns an error message for a `problem` with field at `index` and an optional `name`.
285fn error_on_field<'a, 'de>(
286    problem: &'static str,
287    index: usize,
288    name: Option<&'a str>,
289    prod: &impl ProductVisitor<'de>,
290) -> impl fmt::Display + 'a {
291    let field_kind = match prod.product_kind() {
292        ProductKind::Normal => "field",
293        ProductKind::ReducerArgs => "reducer argument",
294    };
295    fmt_fn(move |f| {
296        // e.g. "missing field `foo`"
297        f.write_str(problem)?;
298        f.write_str(field_kind)?;
299        if let Some(name) = name {
300            write!(f, " `{name}`")
301        } else {
302            write!(f, " (index {index})")
303        }
304    })
305}
306
307/// Returns an error message for invalid product type lengths.
308fn fmt_invalid_len<'de>(
309    expected: &impl ProductVisitor<'de>,
310) -> FDisplay<impl '_ + Fn(&mut fmt::Formatter) -> fmt::Result> {
311    fmt_fn(|f| {
312        let ty = match expected.product_kind() {
313            ProductKind::Normal => "product type",
314            ProductKind::ReducerArgs => "reducer args for",
315        };
316        let name = expected.product_name().unwrap_or("<product>");
317        let len = expected.product_len();
318
319        write!(f, "{ty} {name} with {len} elements")
320    })
321}
322
323/// A visitor walking through a [`Deserializer`] for products.
324pub trait ProductVisitor<'de>: Sized {
325    /// The resulting product.
326    type Output;
327
328    /// Returns the name of the product, if any.
329    fn product_name(&self) -> Option<&str>;
330
331    /// Returns the length of the product.
332    fn product_len(&self) -> usize;
333
334    /// Returns the kind of the product.
335    fn product_kind(&self) -> ProductKind {
336        ProductKind::Normal
337    }
338
339    /// The input contains an unnamed product.
340    fn visit_seq_product<A: SeqProductAccess<'de>>(self, prod: A) -> Result<Self::Output, A::Error>;
341
342    /// The input contains a named product.
343    fn visit_named_product<A: NamedProductAccess<'de>>(self, prod: A) -> Result<Self::Output, A::Error>;
344
345    /// The input contains an unnamed product.
346    fn validate_seq_product<A: SeqProductAccess<'de>>(self, prod: A) -> Result<(), A::Error> {
347        self.visit_seq_product(prod).map(|_| ())
348    }
349
350    /// The input contains a named product.
351    fn validate_named_product<A: NamedProductAccess<'de>>(self, prod: A) -> Result<(), A::Error> {
352        self.visit_named_product(prod).map(|_| ())
353    }
354}
355
356/// What kind of product is this?
357#[derive(Clone, Copy)]
358pub enum ProductKind {
359    // A normal product.
360    Normal,
361    /// A product in the context of reducer arguments.
362    ReducerArgs,
363}
364
365/// Provides a [`ProductVisitor`] with access to each element of the unnamed product in the input.
366///
367/// This is a trait that a [`Deserializer`] passes to a [`ProductVisitor`] implementation.
368pub trait SeqProductAccess<'de> {
369    /// The error type that can be returned if some error occurs during deserialization.
370    type Error: Error;
371
372    /// Deserializes an `T` from the input.
373    ///
374    /// Returns `Ok(Some(value))` for the next element in the product,
375    /// or `Ok(None)` if there are no more remaining items.
376    ///
377    /// This method exists as a convenience for [`Deserialize`] implementations.
378    /// [`SeqProductAccess`] implementations should not override the default behavior.
379    fn next_element<T: Deserialize<'de>>(&mut self) -> Result<Option<T>, Self::Error> {
380        self.next_element_seed(PhantomData)
381    }
382
383    /// Statefully validates `T::Output` from the input provided a `seed` value.
384    ///
385    /// Returns `Ok(Some(()))` for the next element in the unnamed product,
386    /// or `Ok(None)` if there are no more remaining items.
387    fn validate_next_element<T: Deserialize<'de>>(&mut self) -> Result<Option<()>, Self::Error> {
388        self.validate_next_element_seed(PhantomData::<T>)
389    }
390
391    /// Statefully deserializes `T::Output` from the input provided a `seed` value.
392    ///
393    /// Returns `Ok(Some(value))` for the next element in the unnamed product,
394    /// or `Ok(None)` if there are no more remaining items.
395    ///
396    /// [`Deserialize`] implementations should typically use
397    /// [`next_element`](SeqProductAccess::next_element) instead.
398    fn next_element_seed<T: DeserializeSeed<'de>>(&mut self, seed: T) -> Result<Option<T::Output>, Self::Error>;
399
400    /// Statefully validates `T::Output` from the input provided a `seed` value.
401    ///
402    /// Returns `Ok(Some(()))` for the next element in the unnamed product,
403    /// or `Ok(None)` if there are no more remaining items.
404    fn validate_next_element_seed<T: DeserializeSeed<'de>>(&mut self, seed: T) -> Result<Option<()>, Self::Error> {
405        self.next_element_seed(seed).map(|opt| opt.map(|_| ()))
406    }
407}
408
409/// Provides a [`ProductVisitor`] with access to each element of the named product in the input.
410///
411/// This is a trait that a [`Deserializer`] passes to a [`ProductVisitor`] implementation.
412pub trait NamedProductAccess<'de> {
413    /// The error type that can be returned if some error occurs during deserialization.
414    type Error: Error;
415
416    /// Deserializes field name of type `V::Output`
417    /// from the input using a visitor provided by the deserializer.
418    fn get_field_ident<V: FieldNameVisitor<'de>>(&mut self, visitor: V) -> Result<Option<V::Output>, Self::Error>;
419
420    /// Deserializes field value of type `T` from the input.
421    ///
422    /// This method exists as a convenience for [`Deserialize`] implementations.
423    /// [`NamedProductAccess`] implementations should not override the default behavior.
424    fn get_field_value<T: Deserialize<'de>>(&mut self) -> Result<T, Self::Error> {
425        self.get_field_value_seed(PhantomData)
426    }
427
428    /// Deserializes field value of type `T` from the input.
429    ///
430    /// This method exists as a convenience for [`Deserialize`] implementations.
431    /// [`NamedProductAccess`] implementations should not override the default behavior.
432    fn validate_field_value<T: Deserialize<'de>>(&mut self) -> Result<(), Self::Error> {
433        self.validate_field_value_seed(PhantomData::<T>)
434    }
435
436    /// Statefully deserializes the field value `T::Output` from the input provided a `seed` value.
437    ///
438    /// [`Deserialize`] implementations should typically use
439    /// [`next_element`](NamedProductAccess::get_field_value) instead.
440    fn get_field_value_seed<T: DeserializeSeed<'de>>(&mut self, seed: T) -> Result<T::Output, Self::Error>;
441
442    /// Statefully validates the field value `T::Output` from the input provided a `seed` value.
443    fn validate_field_value_seed<T: DeserializeSeed<'de>>(&mut self, seed: T) -> Result<(), Self::Error> {
444        self.get_field_value_seed(seed).map(|_| ())
445    }
446}
447
448/// Visitor used to deserialize the name of a field.
449pub trait FieldNameVisitor<'de> {
450    /// The resulting field name.
451    type Output;
452
453    /// The sort of product deserialized.
454    fn kind(&self) -> ProductKind {
455        ProductKind::Normal
456    }
457
458    /// Provides a list of valid field names.
459    ///
460    /// Where `None` is yielded, this indicates a nameless field.
461    fn field_names(&self) -> impl '_ + Iterator<Item = Option<&str>>;
462
463    /// Deserializes the name of a field using `name`.
464    fn visit<E: Error>(self, name: &str) -> Result<Self::Output, E>;
465
466    /// Deserializes the name of a field using `index`.
467    ///
468    /// Should only be called when `index` is already known to exist
469    /// and is expected to panic otherwise.
470    fn visit_seq(self, index: usize) -> Self::Output;
471}
472
473/// A visitor walking through a [`Deserializer`] for sums.
474///
475/// This side is provided by a [`Deserialize`] implementation
476/// when calling [`Deserializer::deserialize_sum`].
477pub trait SumVisitor<'de> {
478    /// The resulting sum.
479    type Output;
480
481    /// Returns the name of the sum, if any.
482    fn sum_name(&self) -> Option<&str>;
483
484    /// Returns whether an option is expected.
485    ///
486    /// The provided implementation does not.
487    fn is_option(&self) -> bool {
488        false
489    }
490
491    /// Drives the deserialization of a sum value.
492    ///
493    /// This method will ask the data format ([`A: SumAccess`][SumAccess])
494    /// which variant of the sum to select in terms of a variant name / tag.
495    /// `A` will use a [`VariantVisitor`], that `SumVisitor` has provided,
496    /// to translate into something that is meaningful for `visit_sum`, e.g., an index.
497    ///
498    /// The data format will also return an object ([`VariantAccess`])
499    /// that can deserialize the contents of the variant.
500    fn visit_sum<A: SumAccess<'de>>(self, data: A) -> Result<Self::Output, A::Error>;
501
502    /// Drives the validation of a sum value.
503    ///
504    /// This method will ask the data format ([`A: SumAccess`][SumAccess])
505    /// which variant of the sum to select in terms of a variant name / tag.
506    /// `A` will use a [`VariantVisitor`], that `SumVisitor` has provided,
507    /// to translate into something that is meaningful for `visit_sum`, e.g., an index.
508    ///
509    /// The data format will also return an object ([`VariantAccess`])
510    /// that can validate the contents of the variant.
511    fn validate_sum<A: SumAccess<'de>>(self, data: A) -> Result<(), A::Error>;
512}
513
514/// Provides a [`SumVisitor`] access to the data of a sum in the input.
515///
516/// An `A: SumAccess` object is created by the [`D: Deserializer`]
517/// which passes `A` to a [`V: SumVisitor`] that `D` in turn was passed.
518/// `A` is then used by `V` to split tag and value input apart.
519pub trait SumAccess<'de> {
520    /// The error type that can be returned if some error occurs during deserialization.
521    type Error: Error;
522
523    /// The visitor used to deserialize the content of the sum variant.
524    type Variant: VariantAccess<'de, Error = Self::Error>;
525
526    /// Called to identify which variant to deserialize.
527    /// Returns a tuple with the result of identification (`V::Output`)
528    /// and the input to variant data deserialization.
529    ///
530    /// The `visitor` is provided by the [`Deserializer`].
531    /// This method is typically called from [`SumVisitor::visit_sum`]
532    /// which will provide the [`V: VariantVisitor`](VariantVisitor).
533    fn variant<V: VariantVisitor<'de>>(self, visitor: V) -> Result<(V::Output, Self::Variant), Self::Error>;
534}
535
536/// A visitor passed from [`SumVisitor`] to [`SumAccess::variant`]
537/// which the latter uses to decide what variant to deserialize.
538pub trait VariantVisitor<'de> {
539    /// The result of identifying a variant, e.g., some index type.
540    type Output;
541
542    /// Provides a list of variant names.
543    fn variant_names(&self) -> impl '_ + Iterator<Item = &str>;
544
545    /// Identify the variant based on `tag`.
546    fn visit_tag<E: Error>(self, tag: u8) -> Result<Self::Output, E>;
547
548    /// Identify the variant based on `name`.
549    fn visit_name<E: Error>(self, name: &str) -> Result<Self::Output, E>;
550}
551
552/// A visitor passed from [`SumAccess`] to [`SumVisitor::visit_sum`]
553/// which the latter uses to deserialize the data of a selected variant.
554pub trait VariantAccess<'de>: Sized {
555    type Error: Error;
556
557    /// Called when deserializing the contents of a sum variant.
558    ///
559    /// This method exists as a convenience for [`Deserialize`] implementations.
560    fn deserialize<T: Deserialize<'de>>(self) -> Result<T, Self::Error> {
561        self.deserialize_seed(PhantomData)
562    }
563
564    /// Called when deserializing the contents of a sum variant, and provided with a `seed` value.
565    fn deserialize_seed<T: DeserializeSeed<'de>>(self, seed: T) -> Result<T::Output, Self::Error>;
566
567    /// Called when validating the contents of a sum variant.
568    ///
569    /// This method exists as a convenience for [`Deserialize`] implementations.
570    fn validate<T: Deserialize<'de>>(self) -> Result<(), Self::Error> {
571        self.validate_seed(PhantomData::<T>)
572    }
573
574    /// Called when validating the contents of a sum variant, and provided with a `seed` value.
575    fn validate_seed<T: DeserializeSeed<'de>>(self, seed: T) -> Result<(), Self::Error> {
576        self.deserialize_seed(seed).map(|_| ())
577    }
578}
579
580/// A `SliceVisitor` is provided a slice `T` of some elements by a [`Deserializer`]
581/// and is tasked with translating this slice to the `Output` type.
582pub trait SliceVisitor<'de, T: ToOwned + ?Sized>: Sized {
583    /// The output produced by this visitor.
584    type Output;
585
586    /// The input contains a slice.
587    ///
588    /// The lifetime of the slice is ephemeral
589    /// and it may be destroyed after this method returns.
590    fn visit<E: Error>(self, slice: &T) -> Result<Self::Output, E>;
591
592    /// The input contains a slice and ownership of the slice is being given to the [`SliceVisitor`].
593    fn visit_owned<E: Error>(self, buf: T::Owned) -> Result<Self::Output, E> {
594        self.visit(buf.borrow())
595    }
596
597    /// The input contains a slice that lives at least as long (`'de`) as the [`Deserializer`].
598    fn visit_borrowed<E: Error>(self, borrowed_slice: &'de T) -> Result<Self::Output, E> {
599        self.visit(borrowed_slice)
600    }
601}
602
603/// A visitor walking through a [`Deserializer`] for arrays.
604pub trait ArrayVisitor<'de, T>: Sized {
605    /// The output produced by this visitor.
606    type Output;
607
608    /// The input contains an array, deserialize it.
609    fn visit<A: ArrayAccess<'de, Element = T>>(self, vec: A) -> Result<Self::Output, A::Error>;
610
611    /// The input contains an array, but just validate it, don't deserialize.
612    fn validate<A: ArrayAccess<'de, Element = T>>(self, vec: A) -> Result<(), A::Error> {
613        let _ = self.visit(vec)?;
614        Ok(())
615    }
616}
617
618/// Provides an [`ArrayVisitor`] with access to each element of the array in the input.
619///
620/// This is a trait that a [`Deserializer`] passes to an [`ArrayVisitor`] implementation.
621pub trait ArrayAccess<'de> {
622    /// The element / base type of the array.
623    type Element;
624
625    /// The error type that can be returned if some error occurs during deserialization.
626    type Error: Error;
627
628    /// This returns `Ok(Some(value))` for the next element in the array,
629    /// or `Ok(None)` if there are no more remaining elements.
630    fn next_element(&mut self) -> Result<Option<Self::Element>, Self::Error>;
631
632    /// This returns `Ok(Some(()))` for the next element in the array,
633    /// or `Ok(None)` if there are no more remaining elements.
634    fn validate_next_element(&mut self) -> Result<Option<()>, Self::Error> {
635        let opt = self.next_element()?;
636        Ok(opt.map(|_| ()))
637    }
638
639    /// Returns the number of elements remaining in the array, if known.
640    fn size_hint(&self) -> Option<usize> {
641        None
642    }
643}
644
645/// `DeserializeSeed` is the stateful form of the [`Deserialize`] trait.
646pub trait DeserializeSeed<'de>: Sized {
647    /// The type produced by using this seed.
648    type Output;
649
650    /// Equivalent to the more common [`Deserialize::deserialize`] associated function,
651    /// except with some initial piece of data (the seed `self`) passed in.
652    fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Output, D::Error>;
653
654    /// Validate that the input is of the correct form for this seed.
655    ///
656    /// The default implementation simply deserializes the input and discards the result,
657    /// but implementations can override this to perform more efficient validation
658    /// without fully deserializing the input.
659    fn validate<D: Deserializer<'de>>(self, deserializer: D) -> Result<(), D::Error> {
660        let _ = self.deserialize(deserializer)?;
661        Ok(())
662    }
663}
664
665use crate::de::impls::BorrowedSliceVisitor;
666pub use spacetimedb_bindings_macro::Deserialize;
667
668/// A data structure that can be deserialized from any data format supported by the SpacetimeDB Algebraic Type System.
669///
670/// In most cases, implementations of `Deserialize` may be `#[derive(Deserialize)]`d.
671///
672/// The `Deserialize` trait in SATS performs the same function as `serde::Deserialize` in [`serde`].
673/// See the documentation of `serde::Deserialize` for more information of the data model.
674///
675/// The lifetime `'de` allows us to deserialize lifetime-generic types in a zero-copy fashion.
676///
677/// Do not manually implement this trait unless you know what you are doing.
678/// Implementations must be consistent with `Serialize for T`, `SpacetimeType for T` and `Serialize, Deserialize for AlgebraicValue`.
679/// Implementations that are inconsistent across these traits may result in data loss.
680///
681/// [`serde`]: https://crates.io/crates/serde
682pub trait Deserialize<'de>: Sized {
683    /// Deserialize this value from the given `deserializer`.
684    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>;
685
686    #[doc(hidden)]
687    /// Deserialize this value from the given the BSATN `deserializer`.
688    fn deserialize_from_bsatn<R: BufReader<'de>>(
689        deserializer: bsatn::Deserializer<'de, R>,
690    ) -> Result<Self, bsatn::DecodeError> {
691        Self::deserialize(deserializer)
692    }
693
694    /// used in the Deserialize for Vec<T> impl to allow specializing deserializing Vec<T> as bytes
695    #[doc(hidden)]
696    #[inline(always)]
697    fn __deserialize_vec<D: Deserializer<'de>>(deserializer: D) -> Result<Vec<Self>, D::Error> {
698        deserializer.deserialize_array(BasicVecVisitor)
699    }
700
701    #[doc(hidden)]
702    #[inline(always)]
703    fn __deserialize_array<D: Deserializer<'de>, const N: usize>(deserializer: D) -> Result<[Self; N], D::Error> {
704        deserializer.deserialize_array(BasicArrayVisitor)
705    }
706
707    #[doc(hidden)]
708    #[inline(always)]
709    /// Validate that the input is of the correct form for this type.
710    ///
711    /// The default implementation simply deserializes the input and discards the result,
712    /// but implementations can override this to perform more efficient validation
713    /// without fully deserializing the input.
714    fn validate<D: Deserializer<'de>>(deserializer: D) -> Result<(), D::Error> {
715        let _ = Self::deserialize(deserializer)?;
716        Ok(())
717    }
718}
719
720/// A data structure that can be deserialized in SATS
721/// without borrowing any data from the deserializer.
722pub trait DeserializeOwned: for<'de> Deserialize<'de> {}
723impl<T: for<'de> Deserialize<'de>> DeserializeOwned for T {}
724
725impl<'de, T: Deserialize<'de>> DeserializeSeed<'de> for PhantomData<T> {
726    type Output = T;
727
728    fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Output, D::Error> {
729        T::deserialize(deserializer)
730    }
731}
732
733/// A vector with two operations: `with_capacity` and `push`.
734pub trait GrowingVec<T> {
735    /// Create the collection with the given capacity.
736    fn try_with_capacity<E: Error>(cap: usize) -> Result<Self, E>
737    where
738        Self: Sized;
739
740    /// Push to the vector the `elem`.
741    fn push(&mut self, elem: T);
742}
743
744impl<T> GrowingVec<T> for Vec<T> {
745    fn try_with_capacity<E: Error>(cap: usize) -> Result<Self, E>
746    where
747        Self: Sized,
748    {
749        let mut vec = Vec::new();
750        vec.try_reserve_exact(cap).map_err(|_| E::allocation_failed(cap))?;
751        Ok(vec)
752    }
753    fn push(&mut self, elem: T) {
754        self.push(elem)
755    }
756}
757
758impl<T, const N: usize> GrowingVec<T> for SmallVec<[T; N]> {
759    fn try_with_capacity<E: Error>(cap: usize) -> Result<Self, E>
760    where
761        Self: Sized,
762    {
763        let mut vec = Self::new();
764        vec.try_reserve_exact(cap).map_err(|_| E::allocation_failed(cap))?;
765        Ok(vec)
766    }
767    fn push(&mut self, elem: T) {
768        self.push(elem)
769    }
770}
771
772/// A basic implementation of `ArrayVisitor::visit` using the provided size hint.
773pub fn array_visit<'de, A: ArrayAccess<'de>, V: GrowingVec<A::Element>>(mut access: A) -> Result<V, A::Error> {
774    let mut v = V::try_with_capacity(access.size_hint().unwrap_or(0))?;
775    while let Some(x) = access.next_element()? {
776        v.push(x)
777    }
778    Ok(v)
779}
780
781/// A basic implementation of `ArrayVisitor::validate`.
782pub fn array_validate<'de, A: ArrayAccess<'de>>(mut access: A) -> Result<(), A::Error> {
783    while access.validate_next_element()?.is_some() {}
784    Ok(())
785}
786
787/// An implementation of [`ArrayVisitor<'de, T>`] where the output is a `Vec<T>`.
788pub struct BasicVecVisitor;
789
790impl<'de, T> ArrayVisitor<'de, T> for BasicVecVisitor {
791    type Output = Vec<T>;
792
793    fn visit<A: ArrayAccess<'de, Element = T>>(self, vec: A) -> Result<Self::Output, A::Error> {
794        array_visit(vec)
795    }
796
797    fn validate<A: ArrayAccess<'de, Element = T>>(self, vec: A) -> Result<(), A::Error> {
798        array_validate(vec)
799    }
800}
801
802/// An implementation of [`ArrayVisitor<'de, T>`] where the output is a `SmallVec<[T; N]>`.
803pub struct BasicSmallVecVisitor<const N: usize>;
804
805impl<'de, T, const N: usize> ArrayVisitor<'de, T> for BasicSmallVecVisitor<N> {
806    type Output = SmallVec<[T; N]>;
807
808    fn visit<A: ArrayAccess<'de, Element = T>>(self, vec: A) -> Result<Self::Output, A::Error> {
809        array_visit(vec)
810    }
811
812    fn validate<A: ArrayAccess<'de, Element = T>>(self, vec: A) -> Result<(), A::Error> {
813        array_validate(vec)
814    }
815}
816
817/// An implementation of [`ArrayVisitor<'de, T>`] where the output is a `[T; N]`.
818struct BasicArrayVisitor<const N: usize>;
819
820impl<'de, T, const N: usize> ArrayVisitor<'de, T> for BasicArrayVisitor<N> {
821    type Output = [T; N];
822
823    fn visit<A: ArrayAccess<'de, Element = T>>(self, mut vec: A) -> Result<Self::Output, A::Error> {
824        let mut v = arrayvec::ArrayVec::<T, N>::new();
825        while let Some(el) = vec.next_element()? {
826            v.try_push(el)
827                .map_err(|_| Error::custom("too many elements for array"))?
828        }
829        v.into_inner().map_err(|_| Error::custom("too few elements for array"))
830    }
831
832    fn validate<A: ArrayAccess<'de, Element = T>>(self, mut vec: A) -> Result<(), A::Error> {
833        // Validate each element and count.
834        let mut count = 0;
835        while vec.next_element()?.is_some() {
836            count += 1;
837        }
838        // Don't do this in the loop,
839        // as we bias towards there not being any errors.
840        if count > N {
841            return Err(Error::custom("too many elements for array"));
842        }
843        if count < N {
844            return Err(Error::custom("too few elements for array"));
845        }
846        Ok(())
847    }
848}
849
850/// Provided a list of names,
851/// returns a human readable list of all the names,
852/// or `None` in the case of an empty list of names.
853fn one_of_names<'a, I: Iterator<Item = Option<&'a str>>>(names: impl Fn() -> I) -> Option<impl fmt::Display> {
854    // Count how many names there are.
855    let count = names().count();
856
857    // There was at least one name; render those names.
858    (count != 0).then(move || {
859        fmt_fn(move |f| {
860            let mut anon_name = 0;
861            // An example of what happens for names "foo", "bar", and "baz":
862            //
863            // count = 1 -> "`foo`"
864            //       = 2 -> "`foo` or `bar`"
865            //       > 2 -> "one of `foo`, `bar`, or `baz`"
866            for (index, mut name) in names().enumerate() {
867                let mut name_buf: String = String::new();
868                let name = name.get_or_insert_with(|| {
869                    name_buf = format!("{anon_name}");
870                    anon_name += 1;
871                    &name_buf
872                });
873                match (count, index) {
874                    (1, _) => write!(f, "`{name}`"),
875                    (2, 1) => write!(f, "`{name}`"),
876                    (2, 2) => write!(f, "`or `{name}`"),
877                    (_, 1) => write!(f, "one of `{name}`"),
878                    (c, i) if i < c => write!(f, ", `{name}`"),
879                    (_, _) => write!(f, ", `, or {name}`"),
880                }?;
881            }
882
883            Ok(())
884        })
885    })
886}
887
888/// Deserializes `none` variant of an optional value.
889pub struct NoneAccess<E>(PhantomData<E>);
890
891impl<E: Error> NoneAccess<E> {
892    /// Returns a new [`NoneAccess`].
893    pub fn new() -> Self {
894        Self(PhantomData)
895    }
896}
897
898impl<E: Error> Default for NoneAccess<E> {
899    fn default() -> Self {
900        Self::new()
901    }
902}
903
904impl<'de, E: Error> SumAccess<'de> for NoneAccess<E> {
905    type Error = E;
906    type Variant = Self;
907
908    fn variant<V: VariantVisitor<'de>>(self, visitor: V) -> Result<(V::Output, Self::Variant), Self::Error> {
909        visitor.visit_name("none").map(|var| (var, self))
910    }
911}
912impl<'de, E: Error> VariantAccess<'de> for NoneAccess<E> {
913    type Error = E;
914    fn deserialize_seed<T: DeserializeSeed<'de>>(self, seed: T) -> Result<T::Output, Self::Error> {
915        seed.deserialize(UnitAccess::new())
916    }
917}
918
919/// Deserializes `some` variant of an optional value.
920pub struct SomeAccess<D>(D);
921
922impl<D> SomeAccess<D> {
923    /// Returns a new [`SomeAccess`] with a given deserializer for the `some` variant.
924    pub fn new(de: D) -> Self {
925        Self(de)
926    }
927}
928
929impl<'de, D: Deserializer<'de>> SumAccess<'de> for SomeAccess<D> {
930    type Error = D::Error;
931    type Variant = Self;
932
933    fn variant<V: VariantVisitor<'de>>(self, visitor: V) -> Result<(V::Output, Self::Variant), Self::Error> {
934        visitor.visit_name("some").map(|var| (var, self))
935    }
936}
937
938impl<'de, D: Deserializer<'de>> VariantAccess<'de> for SomeAccess<D> {
939    type Error = D::Error;
940    fn deserialize_seed<T: DeserializeSeed<'de>>(self, seed: T) -> Result<T::Output, Self::Error> {
941        seed.deserialize(self.0)
942    }
943    fn validate_seed<T: DeserializeSeed<'de>>(self, seed: T) -> Result<(), Self::Error> {
944        seed.validate(self.0)
945    }
946}
947
948/// A `Deserializer` that represents a unit value.
949// used in the implementation of `VariantAccess for NoneAccess`
950pub struct UnitAccess<E>(PhantomData<E>);
951
952impl<E: Error> UnitAccess<E> {
953    /// Returns a new [`UnitAccess`].
954    pub fn new() -> Self {
955        Self(PhantomData)
956    }
957}
958
959impl<E: Error> Default for UnitAccess<E> {
960    fn default() -> Self {
961        Self::new()
962    }
963}
964
965impl<'de, E: Error> SeqProductAccess<'de> for UnitAccess<E> {
966    type Error = E;
967
968    fn next_element_seed<T: DeserializeSeed<'de>>(&mut self, _seed: T) -> Result<Option<T::Output>, Self::Error> {
969        Ok(None)
970    }
971}
972
973impl<'de, E: Error> NamedProductAccess<'de> for UnitAccess<E> {
974    type Error = E;
975
976    fn get_field_ident<V: FieldNameVisitor<'de>>(&mut self, _visitor: V) -> Result<Option<V::Output>, Self::Error> {
977        Ok(None)
978    }
979
980    fn get_field_value_seed<T: DeserializeSeed<'de>>(&mut self, _seed: T) -> Result<T::Output, Self::Error> {
981        unreachable!()
982    }
983}
984
985impl<'de, E: Error> Deserializer<'de> for UnitAccess<E> {
986    type Error = E;
987
988    fn deserialize_product<V: ProductVisitor<'de>>(self, visitor: V) -> Result<V::Output, Self::Error> {
989        visitor.visit_seq_product(self)
990    }
991
992    fn deserialize_sum<V: SumVisitor<'de>>(self, _visitor: V) -> Result<V::Output, Self::Error> {
993        Err(E::custom("invalid type"))
994    }
995
996    fn deserialize_bool(self) -> Result<bool, Self::Error> {
997        Err(E::custom("invalid type"))
998    }
999
1000    fn deserialize_u8(self) -> Result<u8, Self::Error> {
1001        Err(E::custom("invalid type"))
1002    }
1003
1004    fn deserialize_u16(self) -> Result<u16, Self::Error> {
1005        Err(E::custom("invalid type"))
1006    }
1007
1008    fn deserialize_u32(self) -> Result<u32, Self::Error> {
1009        Err(E::custom("invalid type"))
1010    }
1011
1012    fn deserialize_u64(self) -> Result<u64, Self::Error> {
1013        Err(E::custom("invalid type"))
1014    }
1015
1016    fn deserialize_u128(self) -> Result<u128, Self::Error> {
1017        Err(E::custom("invalid type"))
1018    }
1019
1020    fn deserialize_u256(self) -> Result<u256, Self::Error> {
1021        Err(E::custom("invalid type"))
1022    }
1023
1024    fn deserialize_i8(self) -> Result<i8, Self::Error> {
1025        Err(E::custom("invalid type"))
1026    }
1027
1028    fn deserialize_i16(self) -> Result<i16, Self::Error> {
1029        Err(E::custom("invalid type"))
1030    }
1031
1032    fn deserialize_i32(self) -> Result<i32, Self::Error> {
1033        Err(E::custom("invalid type"))
1034    }
1035
1036    fn deserialize_i64(self) -> Result<i64, Self::Error> {
1037        Err(E::custom("invalid type"))
1038    }
1039
1040    fn deserialize_i128(self) -> Result<i128, Self::Error> {
1041        Err(E::custom("invalid type"))
1042    }
1043
1044    fn deserialize_i256(self) -> Result<i256, Self::Error> {
1045        Err(E::custom("invalid type"))
1046    }
1047
1048    fn deserialize_f32(self) -> Result<f32, Self::Error> {
1049        Err(E::custom("invalid type"))
1050    }
1051
1052    fn deserialize_f64(self) -> Result<f64, Self::Error> {
1053        Err(E::custom("invalid type"))
1054    }
1055
1056    fn deserialize_str<V: SliceVisitor<'de, str>>(self, _visitor: V) -> Result<V::Output, Self::Error> {
1057        Err(E::custom("invalid type"))
1058    }
1059
1060    fn deserialize_bytes<V: SliceVisitor<'de, [u8]>>(self, _visitor: V) -> Result<V::Output, Self::Error> {
1061        Err(E::custom("invalid type"))
1062    }
1063
1064    fn deserialize_array_seed<V: ArrayVisitor<'de, T::Output>, T: DeserializeSeed<'de> + Clone>(
1065        self,
1066        _visitor: V,
1067        _seed: T,
1068    ) -> Result<V::Output, Self::Error> {
1069        Err(E::custom("invalid type"))
1070    }
1071}