spacetimedb_sats/ser.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(feature = "serde")]
6pub mod serde;
7
8use core::fmt;
9
10/// A data format that can deserialize any data structure supported by SATs.
11///
12/// The `Serializer` trait in SATS performs the same function as `serde::Serializer` in [`serde`].
13/// See the documentation of `serde::Serializer` for more information on the data model.
14///
15/// [`serde`]: https://crates.io/crates/serde
16pub trait Serializer: Sized {
17 /// The output type produced by this `Serializer` during successful serialization.
18 ///
19 /// Most serializers that produce text or binary output should set `Ok = ()`
20 /// and serialize into an [`io::Write`] or buffer contained within the `Serializer` instance.
21 /// Serializers that build in-memory data structures may be simplified by using `Ok` to propagate
22 /// the data structure around.
23 ///
24 /// [`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
25 type Ok;
26
27 /// The error type when some error occurs during serialization.
28 type Error: Error;
29
30 /// Type returned from [`serialize_array`](Serializer::serialize_array)
31 /// for serializing the contents of the array.
32 type SerializeArray: SerializeArray<Ok = Self::Ok, Error = Self::Error>;
33
34 /// Type returned from [`serialize_seq_product`](Serializer::serialize_seq_product)
35 /// for serializing the contents of the *unnamed* product.
36 type SerializeSeqProduct: SerializeSeqProduct<Ok = Self::Ok, Error = Self::Error>;
37
38 /// Type returned from [`serialize_named_product`](Serializer::serialize_named_product)
39 /// for serializing the contents of the *named* product.
40 type SerializeNamedProduct: SerializeNamedProduct<Ok = Self::Ok, Error = Self::Error>;
41
42 /// Serialize a `bool` value.
43 fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error>;
44
45 /// Serialize a `u8` value.
46 fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error>;
47
48 /// Serialize a `u16` value.
49 fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error>;
50
51 /// Serialize a `u32` value.
52 fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error>;
53
54 /// Serialize a `u64` value.
55 fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error>;
56
57 /// Serialize a `u128` value.
58 fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error>;
59
60 /// Serialize a `u256` value.
61 fn serialize_u256(self, v: u256) -> Result<Self::Ok, Self::Error>;
62
63 /// Serialize an `i8` value.
64 fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error>;
65
66 /// Serialize an `i16` value.
67 fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error>;
68
69 /// Serialize an `i32` value.
70 fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error>;
71
72 /// Serialize an `i64` value.
73 fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error>;
74
75 /// Serialize an `i128` value.
76 fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error>;
77
78 /// Serialize an `i256` value.
79 fn serialize_i256(self, v: i256) -> Result<Self::Ok, Self::Error>;
80
81 /// Serialize an `f32` value.
82 fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error>;
83
84 /// Serialize an `f64` value.
85 fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error>;
86
87 /// Serialize a `&str` string slice.
88 fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error>;
89
90 /// Serialize a `&[u8]` byte slice.
91 fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error>;
92
93 /// Begin to serialize a variably sized array.
94 /// This call must be followed by zero or more calls to [`SerializeArray::serialize_element`],
95 /// then a call to [`SerializeArray::end`].
96 ///
97 /// The argument is the number of elements in the sequence.
98 fn serialize_array(self, len: usize) -> Result<Self::SerializeArray, Self::Error>;
99
100 /// Begin to serialize a product with unnamed fields.
101 /// This call must be followed by zero or more calls to [`SerializeSeqProduct::serialize_element`],
102 /// then a call to [`SerializeSeqProduct::end`].
103 ///
104 /// The argument is the number of fields in the product.
105 fn serialize_seq_product(self, len: usize) -> Result<Self::SerializeSeqProduct, Self::Error>;
106
107 /// Begin to serialize a product with named fields.
108 /// This call must be followed by zero or more calls to [`SerializeNamedProduct::serialize_element`],
109 /// then a call to [`SerializeNamedProduct::end`].
110 ///
111 /// The argument is the number of fields in the product.
112 fn serialize_named_product(self, len: usize) -> Result<Self::SerializeNamedProduct, Self::Error>;
113
114 /// Serialize a sum value provided the chosen `tag`, `name`, and `value`.
115 fn serialize_variant<T: Serialize + ?Sized>(
116 self,
117 tag: u8,
118 name: Option<&str>,
119 value: &T,
120 ) -> Result<Self::Ok, Self::Error>;
121
122 /// Serialize the given `bsatn` encoded data of type `ty`.
123 ///
124 /// This is a concession to performance,
125 /// allowing some implementations to write the buffer directly.
126 ///
127 /// # Safety
128 ///
129 /// - `AlgebraicValue::decode(ty, &mut bsatn).is_ok()`.
130 /// That is, `bsatn` encodes a valid element of `ty`.
131 unsafe fn serialize_bsatn(self, ty: &AlgebraicType, bsatn: &[u8]) -> Result<Self::Ok, Self::Error>;
132
133 /// Serialize the given `bsatn` encoded data of type `ty`.
134 ///
135 /// The data is provided as an iterator of chunks, at arbitrary boundaries,
136 /// with a total concatenated length of `total_bsatn_len` which callers can assume.
137 ///
138 /// An implementation of this method is semantically the same as:
139 /// ```rust,ignore
140 /// let mut buf = Vec::new();
141 /// for chunk in bsatn {
142 /// buf.extend(chunk);
143 /// }
144 /// ser.serialize_bsatn(&buf);
145 /// ```
146 ///
147 /// This method is a concession to performance,
148 /// allowing some implementations to write the buffer directly.
149 ///
150 /// The parameter `I` is required to be `Clone` only for `debug_assert!` purposes.
151 ///
152 /// # Safety
153 ///
154 /// - `total_bsatn_len == bsatn.map(|c| c.len()).sum() <= isize::MAX`
155 /// - Let `buf` be defined as above, i.e., the bytes of `bsatn` concatenated.
156 /// Then `AlgebraicValue::decode(ty, &mut buf).is_ok()`.
157 /// That is, `buf` encodes a valid element of `ty`.
158 unsafe fn serialize_bsatn_in_chunks<'a, I: Clone + Iterator<Item = &'a [u8]>>(
159 self,
160 ty: &AlgebraicType,
161 total_bsatn_len: usize,
162 bsatn: I,
163 ) -> Result<Self::Ok, Self::Error>;
164
165 /// Serialize the given `string`.
166 ///
167 /// The string is provided as an iterator of chunks, at arbitrary boundaries,
168 /// with a total concatenated length of `total_len` which callers can trust.
169 ///
170 /// An implementation of this method is semantically the same as:
171 /// ```rust,ignore
172 /// let mut buf = Vec::new();
173 /// for chunk in string {
174 /// buf.extend(chunk);
175 /// }
176 /// let str = unsafe { core::str::from_utf8_unchecked(&buf) };
177 /// ser.serialize_str(str);
178 /// ```
179 ///
180 /// This method is a concession to performance,
181 /// allowing some implementations to write the buffer directly.
182 ///
183 /// The parameter `I` is required to be `Clone` only for `debug_assert!` purposes.
184 ///
185 /// # Safety
186 ///
187 /// - `total_len == string.map(|c| c.len()).sum() <= isize::MAX`
188 /// - Let `buf` be the bytes of `string` concatenated.
189 /// Then `core::str::from_utf8(&buf).is_ok()`.
190 /// That is, `buf` is valid UTF-8.
191 /// Note however that individual chunks need not be valid UTF-8,
192 /// as multi-byte characters may be split across chunk boundaries.
193 unsafe fn serialize_str_in_chunks<'a, I: Clone + Iterator<Item = &'a [u8]>>(
194 self,
195 total_len: usize,
196 string: I,
197 ) -> Result<Self::Ok, Self::Error>;
198}
199
200use ethnum::{i256, u256};
201pub use spacetimedb_bindings_macro::Serialize;
202
203use crate::{bsatn, buffer::BufWriter, AlgebraicType};
204
205/// A **data structure** that can be serialized into any data format supported by
206/// the SpacetimeDB Algebraic Type System.
207///
208/// In most cases, implementations of `Serialize` may be `#[derive(Serialize)]`d.
209///
210/// The `Serialize` trait in SATS performs the same function as `serde::Serialize` in [`serde`].
211/// See the documentation of `serde::Serialize` for more information of the data model.
212///
213/// Do not manually implement this trait unless you know what you are doing.
214/// Implementations must be consistent with `Deerialize<'de> for T`, `SpacetimeType for T` and `Serialize, Deserialize for AlgebraicValue`.
215/// Implementations that are inconsistent across these traits may result in data loss.
216///
217/// [`serde`]: https://crates.io/crates/serde
218pub trait Serialize {
219 /// Serialize `self` in the data format of `S` using the provided `serializer`.
220 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>;
221
222 #[doc(hidden)]
223 /// Serialize `self` in the data format BSATN using the provided BSATN `serializer`.
224 fn serialize_into_bsatn<W: BufWriter>(
225 &self,
226 serializer: bsatn::Serializer<'_, W>,
227 ) -> Result<(), bsatn::EncodeError> {
228 self.serialize(serializer)
229 }
230
231 /// Used in the `Serialize for Vec<T>` implementation
232 /// to allow a specialized serialization of `Vec<T>` as bytes.
233 #[doc(hidden)]
234 #[inline(always)]
235 fn __serialize_array<S: Serializer>(this: &[Self], serializer: S) -> Result<S::Ok, S::Error>
236 where
237 Self: Sized,
238 {
239 let mut vec = serializer.serialize_array(this.len())?;
240 for elem in this {
241 vec.serialize_element(elem)?;
242 }
243 vec.end()
244 }
245}
246
247/// The base trait serialization error types must implement.
248pub trait Error {
249 /// Returns an error derived from `msg: impl Display`.
250 fn custom<T: fmt::Display>(msg: T) -> Self;
251}
252
253impl Error for String {
254 fn custom<T: fmt::Display>(msg: T) -> Self {
255 msg.to_string()
256 }
257}
258
259impl Error for std::convert::Infallible {
260 fn custom<T: fmt::Display>(msg: T) -> Self {
261 panic!("error generated for Infallible serializer: {msg}")
262 }
263}
264
265/// Returned from [`Serializer::serialize_array`].
266///
267/// This provides a continuation of sorts
268/// where you can call [`serialize_element`](SerializeArray::serialize_element) however many times
269/// and then finally the [`end`](SerializeArray::end) is reached.
270pub trait SerializeArray {
271 /// Must match the `Ok` type of any `Serializer` that uses this type.
272 type Ok;
273
274 /// Must match the `Error` type of any `Serializer` that uses this type.
275 type Error: Error;
276
277 /// Serialize an array `element`.
278 fn serialize_element<T: Serialize + ?Sized>(&mut self, element: &T) -> Result<(), Self::Error>;
279
280 /// Consumes and finalizes the array serializer returning the `Self::Ok` data.
281 fn end(self) -> Result<Self::Ok, Self::Error>;
282}
283
284/// Returned from [`Serializer::serialize_seq_product`].
285///
286/// This provides a continuation of sorts
287/// where you can call [`serialize_element`](SerializeSeqProduct::serialize_element) however many times
288/// and then finally the [`end`](SerializeSeqProduct::end) is reached.
289pub trait SerializeSeqProduct {
290 /// Must match the `Ok` type of any `Serializer` that uses this type.
291 type Ok;
292
293 /// Must match the `Error` type of any `Serializer` that uses this type.
294 type Error: Error;
295
296 /// Serialize an unnamed product `element`.
297 fn serialize_element<T: Serialize + ?Sized>(&mut self, element: &T) -> Result<(), Self::Error>;
298
299 /// Consumes and finalizes the product serializer returning the `Self::Ok` data.
300 fn end(self) -> Result<Self::Ok, Self::Error>;
301}
302
303/// Returned from [`Serializer::serialize_named_product`].
304///
305/// This provides a continuation of sorts
306/// where you can call [`serialize_element`](SerializeNamedProduct::serialize_element) however many times
307/// and then finally the [`end`](SerializeNamedProduct::end) is reached.
308pub trait SerializeNamedProduct {
309 /// Must match the `Ok` type of any `Serializer` that uses this type.
310 type Ok;
311
312 /// Must match the `Error` type of any `Serializer` that uses this type.
313 type Error: Error;
314
315 /// Serialize a named product `element` with `name`.
316 fn serialize_element<T: Serialize + ?Sized>(&mut self, name: Option<&str>, elem: &T) -> Result<(), Self::Error>;
317
318 /// Consumes and finalizes the product serializer returning the `Self::Ok` data.
319 fn end(self) -> Result<Self::Ok, Self::Error>;
320}
321
322/// Forwards the implementation of a named product value
323/// to the implementation of the unnamed kind,
324/// thereby ignoring any field names.
325pub struct ForwardNamedToSeqProduct<S> {
326 /// The unnamed product serializer.
327 tup: S,
328}
329
330impl<S> ForwardNamedToSeqProduct<S> {
331 /// Returns a forwarder based on the provided unnamed product serializer.
332 pub fn new(tup: S) -> Self {
333 Self { tup }
334 }
335
336 /// Forwards the serialization of a named product of `len` fields
337 /// to an unnamed serialization format.
338 pub fn forward<Ser>(ser: Ser, len: usize) -> Result<Self, Ser::Error>
339 where
340 Ser: Serializer<SerializeSeqProduct = S>,
341 {
342 ser.serialize_seq_product(len).map(Self::new)
343 }
344}
345
346impl<S: SerializeSeqProduct> SerializeNamedProduct for ForwardNamedToSeqProduct<S> {
347 type Ok = S::Ok;
348 type Error = S::Error;
349
350 fn serialize_element<T: Serialize + ?Sized>(&mut self, _name: Option<&str>, elem: &T) -> Result<(), Self::Error> {
351 self.tup.serialize_element(elem)
352 }
353
354 fn end(self) -> Result<Self::Ok, Self::Error> {
355 self.tup.end()
356 }
357}