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 // Don’t blindly trust length prefixes when reserving initial capacity
775 // for decoding array elements, as malformed input could generate a huge allocation,
776 // potentially resulting in an OOM kill.
777 const RESERVE_ARRAY_ELEMENTS: usize = 4096;
778 let cap = access.size_hint().unwrap_or(0);
779 let mut v = V::try_with_capacity(cap.min(RESERVE_ARRAY_ELEMENTS))?;
780 while let Some(x) = access.next_element()? {
781 v.push(x)
782 }
783 Ok(v)
784}
785
786/// A basic implementation of `ArrayVisitor::validate`.
787pub fn array_validate<'de, A: ArrayAccess<'de>>(mut access: A) -> Result<(), A::Error> {
788 while access.validate_next_element()?.is_some() {}
789 Ok(())
790}
791
792/// An implementation of [`ArrayVisitor<'de, T>`] where the output is a `Vec<T>`.
793pub struct BasicVecVisitor;
794
795impl<'de, T> ArrayVisitor<'de, T> for BasicVecVisitor {
796 type Output = Vec<T>;
797
798 fn visit<A: ArrayAccess<'de, Element = T>>(self, vec: A) -> Result<Self::Output, A::Error> {
799 array_visit(vec)
800 }
801
802 fn validate<A: ArrayAccess<'de, Element = T>>(self, vec: A) -> Result<(), A::Error> {
803 array_validate(vec)
804 }
805}
806
807/// An implementation of [`ArrayVisitor<'de, T>`] where the output is a `SmallVec<[T; N]>`.
808pub struct BasicSmallVecVisitor<const N: usize>;
809
810impl<'de, T, const N: usize> ArrayVisitor<'de, T> for BasicSmallVecVisitor<N> {
811 type Output = SmallVec<[T; N]>;
812
813 fn visit<A: ArrayAccess<'de, Element = T>>(self, vec: A) -> Result<Self::Output, A::Error> {
814 array_visit(vec)
815 }
816
817 fn validate<A: ArrayAccess<'de, Element = T>>(self, vec: A) -> Result<(), A::Error> {
818 array_validate(vec)
819 }
820}
821
822/// An implementation of [`ArrayVisitor<'de, T>`] where the output is a `[T; N]`.
823struct BasicArrayVisitor<const N: usize>;
824
825impl<'de, T, const N: usize> ArrayVisitor<'de, T> for BasicArrayVisitor<N> {
826 type Output = [T; N];
827
828 fn visit<A: ArrayAccess<'de, Element = T>>(self, mut vec: A) -> Result<Self::Output, A::Error> {
829 let mut v = arrayvec::ArrayVec::<T, N>::new();
830 while let Some(el) = vec.next_element()? {
831 v.try_push(el)
832 .map_err(|_| Error::custom("too many elements for array"))?
833 }
834 v.into_inner().map_err(|_| Error::custom("too few elements for array"))
835 }
836
837 fn validate<A: ArrayAccess<'de, Element = T>>(self, mut vec: A) -> Result<(), A::Error> {
838 // Validate each element and count.
839 let mut count = 0;
840 while vec.next_element()?.is_some() {
841 count += 1;
842 }
843 // Don't do this in the loop,
844 // as we bias towards there not being any errors.
845 if count > N {
846 return Err(Error::custom("too many elements for array"));
847 }
848 if count < N {
849 return Err(Error::custom("too few elements for array"));
850 }
851 Ok(())
852 }
853}
854
855/// Provided a list of names,
856/// returns a human readable list of all the names,
857/// or `None` in the case of an empty list of names.
858fn one_of_names<'a, I: Iterator<Item = Option<&'a str>>>(names: impl Fn() -> I) -> Option<impl fmt::Display> {
859 // Count how many names there are.
860 let count = names().count();
861
862 // There was at least one name; render those names.
863 (count != 0).then(move || {
864 fmt_fn(move |f| {
865 let mut anon_name = 0;
866 // An example of what happens for names "foo", "bar", and "baz":
867 //
868 // count = 1 -> "`foo`"
869 // = 2 -> "`foo` or `bar`"
870 // > 2 -> "one of `foo`, `bar`, or `baz`"
871 for (index, mut name) in names().enumerate() {
872 let mut name_buf: String = String::new();
873 let name = name.get_or_insert_with(|| {
874 name_buf = format!("{anon_name}");
875 anon_name += 1;
876 &name_buf
877 });
878 match (count, index) {
879 (1, _) => write!(f, "`{name}`"),
880 (2, 1) => write!(f, "`{name}`"),
881 (2, 2) => write!(f, "`or `{name}`"),
882 (_, 1) => write!(f, "one of `{name}`"),
883 (c, i) if i < c => write!(f, ", `{name}`"),
884 (_, _) => write!(f, ", `, or {name}`"),
885 }?;
886 }
887
888 Ok(())
889 })
890 })
891}
892
893/// Deserializes `none` variant of an optional value.
894pub struct NoneAccess<E>(PhantomData<E>);
895
896impl<E: Error> NoneAccess<E> {
897 /// Returns a new [`NoneAccess`].
898 pub fn new() -> Self {
899 Self(PhantomData)
900 }
901}
902
903impl<E: Error> Default for NoneAccess<E> {
904 fn default() -> Self {
905 Self::new()
906 }
907}
908
909impl<'de, E: Error> SumAccess<'de> for NoneAccess<E> {
910 type Error = E;
911 type Variant = Self;
912
913 fn variant<V: VariantVisitor<'de>>(self, visitor: V) -> Result<(V::Output, Self::Variant), Self::Error> {
914 visitor.visit_name("none").map(|var| (var, self))
915 }
916}
917impl<'de, E: Error> VariantAccess<'de> for NoneAccess<E> {
918 type Error = E;
919 fn deserialize_seed<T: DeserializeSeed<'de>>(self, seed: T) -> Result<T::Output, Self::Error> {
920 seed.deserialize(UnitAccess::new())
921 }
922}
923
924/// Deserializes `some` variant of an optional value.
925pub struct SomeAccess<D>(D);
926
927impl<D> SomeAccess<D> {
928 /// Returns a new [`SomeAccess`] with a given deserializer for the `some` variant.
929 pub fn new(de: D) -> Self {
930 Self(de)
931 }
932}
933
934impl<'de, D: Deserializer<'de>> SumAccess<'de> for SomeAccess<D> {
935 type Error = D::Error;
936 type Variant = Self;
937
938 fn variant<V: VariantVisitor<'de>>(self, visitor: V) -> Result<(V::Output, Self::Variant), Self::Error> {
939 visitor.visit_name("some").map(|var| (var, self))
940 }
941}
942
943impl<'de, D: Deserializer<'de>> VariantAccess<'de> for SomeAccess<D> {
944 type Error = D::Error;
945 fn deserialize_seed<T: DeserializeSeed<'de>>(self, seed: T) -> Result<T::Output, Self::Error> {
946 seed.deserialize(self.0)
947 }
948 fn validate_seed<T: DeserializeSeed<'de>>(self, seed: T) -> Result<(), Self::Error> {
949 seed.validate(self.0)
950 }
951}
952
953/// A `Deserializer` that represents a unit value.
954// used in the implementation of `VariantAccess for NoneAccess`
955pub struct UnitAccess<E>(PhantomData<E>);
956
957impl<E: Error> UnitAccess<E> {
958 /// Returns a new [`UnitAccess`].
959 pub fn new() -> Self {
960 Self(PhantomData)
961 }
962}
963
964impl<E: Error> Default for UnitAccess<E> {
965 fn default() -> Self {
966 Self::new()
967 }
968}
969
970impl<'de, E: Error> SeqProductAccess<'de> for UnitAccess<E> {
971 type Error = E;
972
973 fn next_element_seed<T: DeserializeSeed<'de>>(&mut self, _seed: T) -> Result<Option<T::Output>, Self::Error> {
974 Ok(None)
975 }
976}
977
978impl<'de, E: Error> NamedProductAccess<'de> for UnitAccess<E> {
979 type Error = E;
980
981 fn get_field_ident<V: FieldNameVisitor<'de>>(&mut self, _visitor: V) -> Result<Option<V::Output>, Self::Error> {
982 Ok(None)
983 }
984
985 fn get_field_value_seed<T: DeserializeSeed<'de>>(&mut self, _seed: T) -> Result<T::Output, Self::Error> {
986 unreachable!()
987 }
988}
989
990impl<'de, E: Error> Deserializer<'de> for UnitAccess<E> {
991 type Error = E;
992
993 fn deserialize_product<V: ProductVisitor<'de>>(self, visitor: V) -> Result<V::Output, Self::Error> {
994 visitor.visit_seq_product(self)
995 }
996
997 fn deserialize_sum<V: SumVisitor<'de>>(self, _visitor: V) -> Result<V::Output, Self::Error> {
998 Err(E::custom("invalid type"))
999 }
1000
1001 fn deserialize_bool(self) -> Result<bool, Self::Error> {
1002 Err(E::custom("invalid type"))
1003 }
1004
1005 fn deserialize_u8(self) -> Result<u8, Self::Error> {
1006 Err(E::custom("invalid type"))
1007 }
1008
1009 fn deserialize_u16(self) -> Result<u16, Self::Error> {
1010 Err(E::custom("invalid type"))
1011 }
1012
1013 fn deserialize_u32(self) -> Result<u32, Self::Error> {
1014 Err(E::custom("invalid type"))
1015 }
1016
1017 fn deserialize_u64(self) -> Result<u64, Self::Error> {
1018 Err(E::custom("invalid type"))
1019 }
1020
1021 fn deserialize_u128(self) -> Result<u128, Self::Error> {
1022 Err(E::custom("invalid type"))
1023 }
1024
1025 fn deserialize_u256(self) -> Result<u256, Self::Error> {
1026 Err(E::custom("invalid type"))
1027 }
1028
1029 fn deserialize_i8(self) -> Result<i8, Self::Error> {
1030 Err(E::custom("invalid type"))
1031 }
1032
1033 fn deserialize_i16(self) -> Result<i16, Self::Error> {
1034 Err(E::custom("invalid type"))
1035 }
1036
1037 fn deserialize_i32(self) -> Result<i32, Self::Error> {
1038 Err(E::custom("invalid type"))
1039 }
1040
1041 fn deserialize_i64(self) -> Result<i64, Self::Error> {
1042 Err(E::custom("invalid type"))
1043 }
1044
1045 fn deserialize_i128(self) -> Result<i128, Self::Error> {
1046 Err(E::custom("invalid type"))
1047 }
1048
1049 fn deserialize_i256(self) -> Result<i256, Self::Error> {
1050 Err(E::custom("invalid type"))
1051 }
1052
1053 fn deserialize_f32(self) -> Result<f32, Self::Error> {
1054 Err(E::custom("invalid type"))
1055 }
1056
1057 fn deserialize_f64(self) -> Result<f64, Self::Error> {
1058 Err(E::custom("invalid type"))
1059 }
1060
1061 fn deserialize_str<V: SliceVisitor<'de, str>>(self, _visitor: V) -> Result<V::Output, Self::Error> {
1062 Err(E::custom("invalid type"))
1063 }
1064
1065 fn deserialize_bytes<V: SliceVisitor<'de, [u8]>>(self, _visitor: V) -> Result<V::Output, Self::Error> {
1066 Err(E::custom("invalid type"))
1067 }
1068
1069 fn deserialize_array_seed<V: ArrayVisitor<'de, T::Output>, T: DeserializeSeed<'de> + Clone>(
1070 self,
1071 _visitor: V,
1072 _seed: T,
1073 ) -> Result<V::Output, Self::Error> {
1074 Err(E::custom("invalid type"))
1075 }
1076}