1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
mod impls;
#[cfg(feature = "serde")]
pub mod serde;

#[doc(hidden)]
pub use impls::{visit_named_product, visit_seq_product};

use std::borrow::Borrow;
use std::collections::BTreeMap;
use std::fmt;
use std::marker::PhantomData;

use crate::{fmt_fn, FDisplay};

pub trait Deserializer<'de>: Sized {
    type Error: Error;

    fn deserialize_product<V: ProductVisitor<'de>>(self, visitor: V) -> Result<V::Output, Self::Error>;

    fn deserialize_sum<V: SumVisitor<'de>>(self, visitor: V) -> Result<V::Output, Self::Error>;

    fn deserialize_bool(self) -> Result<bool, Self::Error>;
    fn deserialize_u8(self) -> Result<u8, Self::Error>;
    fn deserialize_u16(self) -> Result<u16, Self::Error>;
    fn deserialize_u32(self) -> Result<u32, Self::Error>;
    fn deserialize_u64(self) -> Result<u64, Self::Error>;
    fn deserialize_u128(self) -> Result<u128, Self::Error>;
    fn deserialize_i8(self) -> Result<i8, Self::Error>;
    fn deserialize_i16(self) -> Result<i16, Self::Error>;
    fn deserialize_i32(self) -> Result<i32, Self::Error>;
    fn deserialize_i64(self) -> Result<i64, Self::Error>;
    fn deserialize_i128(self) -> Result<i128, Self::Error>;
    fn deserialize_f32(self) -> Result<f32, Self::Error>;
    fn deserialize_f64(self) -> Result<f64, Self::Error>;

    fn deserialize_str<V: SliceVisitor<'de, str>>(self, visitor: V) -> Result<V::Output, Self::Error>;

    fn deserialize_bytes<V: SliceVisitor<'de, [u8]>>(self, visitor: V) -> Result<V::Output, Self::Error>;

    fn deserialize_array<V: ArrayVisitor<'de, T>, T: Deserialize<'de>>(
        self,
        visitor: V,
    ) -> Result<V::Output, Self::Error> {
        self.deserialize_array_seed(visitor, PhantomData)
    }

    fn deserialize_array_seed<V: ArrayVisitor<'de, T::Output>, T: DeserializeSeed<'de> + Clone>(
        self,
        visitor: V,
        seed: T,
    ) -> Result<V::Output, Self::Error>;

    fn deserialize_map<Vi: MapVisitor<'de, K, V>, K: Deserialize<'de>, V: Deserialize<'de>>(
        self,
        visitor: Vi,
    ) -> Result<Vi::Output, Self::Error> {
        self.deserialize_map_seed(visitor, PhantomData, PhantomData)
    }

    fn deserialize_map_seed<
        Vi: MapVisitor<'de, K::Output, V::Output>,
        K: DeserializeSeed<'de> + Clone,
        V: DeserializeSeed<'de> + Clone,
    >(
        self,
        visitor: Vi,
        kseed: K,
        vseed: V,
    ) -> Result<Vi::Output, Self::Error>;
}

pub trait Error: Sized {
    fn custom(msg: impl fmt::Display) -> Self;

    fn invalid_product_length<'de, T: ProductVisitor<'de>>(len: usize, expected: &T) -> Self {
        Self::custom(format_args!(
            "invalid length {}, expected {}",
            len,
            fmt_invalid_len(expected)
        ))
    }

    fn missing_field<'de, T: ProductVisitor<'de>>(field: usize, field_name: Option<&str>, prod: &T) -> Self {
        Self::custom(error_on_field("missing ", field, field_name, prod))
    }

    fn duplicate_field<'de, T: ProductVisitor<'de>>(field: usize, field_name: Option<&str>, prod: &T) -> Self {
        Self::custom(error_on_field("duplicate ", field, field_name, prod))
    }

    fn unknown_field_name<'de, T: FieldNameVisitor<'de>>(field_name: &str, expected: &T) -> Self {
        let el_ty = match expected.kind() {
            ProductKind::Normal => "field",
            ProductKind::ReducerArgs => "reducer argument",
        };
        if let Some(one_of) = one_of_names(|n| expected.field_names(n)) {
            Self::custom(format_args!("unknown {el_ty} `{field_name}`, expected {one_of}",))
        } else {
            Self::custom(format_args!("unknown {el_ty} `{field_name}`, there are no {el_ty}s"))
        }
    }

    fn unknown_variant_tag<'de, T: SumVisitor<'de>>(tag: u8, expected: &T) -> Self {
        Self::custom(format_args!(
            "unknown tag {tag:#x} for sum type {}",
            expected.sum_name().unwrap_or("<sum>"),
        ))
    }

    fn unknown_variant_name<T: VariantVisitor>(name: &str, expected: &T) -> Self {
        if let Some(one_of) = one_of_names(|n| expected.variant_names(n)) {
            Self::custom(format_args!("unknown variant `{name}`, expected {one_of}",))
        } else {
            Self::custom(format_args!("unknown variant `{name}`, there are no variants"))
        }
    }
}

fn error_on_field<'a, 'de, T: ProductVisitor<'de>>(
    problem: &'static str,
    field: usize,
    field_name: Option<&'a str>,
    prod: &T,
) -> impl fmt::Display + 'a {
    let field_kind = match prod.product_kind() {
        ProductKind::Normal => "field",
        ProductKind::ReducerArgs => "reducer argument",
    };
    fmt_fn(move |f| {
        // e.g. "missing field `foo`"
        f.write_str(problem)?;
        f.write_str(field_kind)?;
        if let Some(name) = field_name {
            write!(f, " `{}`", name)
        } else {
            write!(f, " (index {})", field)
        }
    })
}

fn fmt_invalid_len<'de, T: ProductVisitor<'de>>(
    expected: &T,
) -> FDisplay<impl Fn(&mut fmt::Formatter) -> fmt::Result + '_> {
    fmt_fn(|f| {
        let ty = match expected.product_kind() {
            ProductKind::Normal => "product type",
            ProductKind::ReducerArgs => "reducer args for",
        };
        let name = expected.product_name().unwrap_or("<product>");
        let len = expected.product_len();

        write!(f, "{ty} {name} with {len} elements")
    })
}

pub trait ProductVisitor<'de> {
    type Output;

    fn product_name(&self) -> Option<&str>;
    fn product_len(&self) -> usize;
    fn product_kind(&self) -> ProductKind {
        ProductKind::Normal
    }

    fn visit_seq_product<A: SeqProductAccess<'de>>(self, prod: A) -> Result<Self::Output, A::Error>;
    fn visit_named_product<A: NamedProductAccess<'de>>(self, prod: A) -> Result<Self::Output, A::Error>;
}

#[derive(Clone, Copy)]
pub enum ProductKind {
    Normal,
    ReducerArgs,
}

pub trait SeqProductAccess<'de> {
    type Error: Error;

    fn next_element<T: Deserialize<'de>>(&mut self) -> Result<Option<T>, Self::Error> {
        self.next_element_seed(PhantomData)
    }

    fn next_element_seed<T: DeserializeSeed<'de>>(&mut self, seed: T) -> Result<Option<T::Output>, Self::Error>;
}

pub trait NamedProductAccess<'de> {
    type Error: Error;

    fn get_field_ident<V: FieldNameVisitor<'de>>(&mut self, visitor: V) -> Result<Option<V::Output>, Self::Error>;

    fn get_field_value<T: Deserialize<'de>>(&mut self) -> Result<T, Self::Error> {
        self.get_field_value_seed(PhantomData)
    }

    fn get_field_value_seed<T: DeserializeSeed<'de>>(&mut self, seed: T) -> Result<T::Output, Self::Error>;
}

pub trait FieldNameVisitor<'de> {
    type Output;

    fn kind(&self) -> ProductKind {
        ProductKind::Normal
    }
    fn field_names(&self, names: &mut dyn ValidNames);

    fn visit<E: Error>(self, name: &str) -> Result<Self::Output, E>;
}

pub trait ValidNames {
    fn push(&mut self, s: &str);
}
impl dyn ValidNames + '_ {
    pub fn extend<I: IntoIterator>(&mut self, i: I)
    where
        I::Item: AsRef<str>,
    {
        for name in i {
            self.push(name.as_ref())
        }
    }
}

pub trait SumVisitor<'de> {
    type Output;

    fn sum_name(&self) -> Option<&str>;

    fn visit_sum<A: SumAccess<'de>>(self, data: A) -> Result<Self::Output, A::Error>;
}

pub trait SumAccess<'de> {
    type Error: Error;
    type Variant: VariantAccess<'de, Error = Self::Error>;

    fn variant<V: VariantVisitor>(self, visitor: V) -> Result<(V::Output, Self::Variant), Self::Error>;
}

pub trait VariantVisitor {
    type Output;

    fn variant_names(&self, names: &mut dyn ValidNames);

    fn visit_tag<E: Error>(self, tag: u8) -> Result<Self::Output, E>;
    fn visit_name<E: Error>(self, name: &str) -> Result<Self::Output, E>;
}

pub trait VariantAccess<'de>: Sized {
    type Error: Error;

    fn deserialize<T: Deserialize<'de>>(self) -> Result<T, Self::Error> {
        self.deserialize_seed(PhantomData)
    }

    fn deserialize_seed<T: DeserializeSeed<'de>>(self, seed: T) -> Result<T::Output, Self::Error>;
}

pub trait SliceVisitor<'de, T: ToOwned + ?Sized>: Sized {
    type Output;

    fn visit<E: Error>(self, slice: &T) -> Result<Self::Output, E>;

    fn visit_owned<E: Error>(self, buf: T::Owned) -> Result<Self::Output, E> {
        self.visit(buf.borrow())
    }

    fn visit_borrowed<E: Error>(self, borrowed_slice: &'de T) -> Result<Self::Output, E> {
        self.visit(borrowed_slice)
    }
}

pub trait ArrayVisitor<'de, T> {
    type Output;

    fn visit<A: ArrayAccess<'de, Element = T>>(self, vec: A) -> Result<Self::Output, A::Error>;
}

pub trait ArrayAccess<'de> {
    type Element;
    type Error;

    fn next_element(&mut self) -> Result<Option<Self::Element>, Self::Error>;

    fn size_hint(&self) -> Option<usize> {
        None
    }
}

pub trait MapVisitor<'de, K, V> {
    type Output;

    fn visit<A: MapAccess<'de, Key = K, Value = V>>(self, map: A) -> Result<Self::Output, A::Error>;
}

pub trait MapAccess<'de> {
    type Key;
    type Value;
    type Error;

    #[allow(clippy::type_complexity)]
    fn next_entry(&mut self) -> Result<Option<(Self::Key, Self::Value)>, Self::Error>;

    fn size_hint(&self) -> Option<usize> {
        None
    }
}

pub trait DeserializeSeed<'de> {
    type Output;
    fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Output, D::Error>;
}

pub use spacetimedb_bindings_macro::Deserialize;
pub trait Deserialize<'de>: Sized {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>;

    /// used in the Deserialize for Vec<T> impl to allow specializing deserializing Vec<T> as bytes
    #[doc(hidden)]
    #[inline(always)]
    fn __deserialize_vec<D: Deserializer<'de>>(deserializer: D) -> Result<Vec<Self>, D::Error> {
        deserializer.deserialize_array(BasicVecVisitor)
    }
}

pub trait DeserializeOwned: for<'de> Deserialize<'de> {}
impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}

impl<'de, T: Deserialize<'de>> DeserializeSeed<'de> for PhantomData<T> {
    type Output = T;

    fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Output, D::Error> {
        T::deserialize(deserializer)
    }
}

pub struct BasicVecVisitor;

impl<'de, T> ArrayVisitor<'de, T> for BasicVecVisitor {
    type Output = Vec<T>;

    fn visit<A: ArrayAccess<'de, Element = T>>(self, mut vec: A) -> Result<Self::Output, A::Error> {
        let mut v = Vec::with_capacity(vec.size_hint().unwrap_or(0));
        while let Some(el) = vec.next_element()? {
            v.push(el)
        }
        Ok(v)
    }
}

pub struct BasicMapVisitor;

impl<'de, K: Ord, V> MapVisitor<'de, K, V> for BasicMapVisitor {
    type Output = BTreeMap<K, V>;

    fn visit<A: MapAccess<'de, Key = K, Value = V>>(self, mut map: A) -> Result<Self::Output, A::Error> {
        let mut m = Vec::with_capacity(map.size_hint().unwrap_or(0));
        while let Some(entry) = map.next_entry()? {
            m.push(entry)
        }
        Ok(m.into_iter().collect())
    }
}

fn one_of_names(names: impl Fn(&mut dyn ValidNames)) -> Option<impl fmt::Display> {
    let mut n = NNames(0);
    names(&mut n);
    let NNames(n) = n;
    (n != 0).then(|| {
        fmt_fn(move |f| {
            let mut f = OneOfNames::new(n != 2, f);
            names(&mut f);
            f.f.map(drop)
        })
    })
}

struct NNames(usize);
impl ValidNames for NNames {
    fn push(&mut self, _: &str) {
        self.0 += 1
    }
}

struct OneOfNames<'a, 'b> {
    at_start: bool,
    many: bool,
    f: Result<&'a mut fmt::Formatter<'b>, fmt::Error>,
}
impl<'a, 'b> OneOfNames<'a, 'b> {
    fn new(many: bool, f: &'a mut fmt::Formatter<'b>) -> Self {
        Self {
            at_start: true,
            many,
            f: Ok(f),
        }
    }
}
impl ValidNames for OneOfNames<'_, '_> {
    fn push(&mut self, name: &str) {
        let (start, sep) = if self.many { ("", " or ") } else { ("one of", ", ") };
        if let Ok(f) = &mut self.f {
            let mut go = || -> fmt::Result {
                f.write_str(if std::mem::take(&mut self.at_start) { start } else { sep })?;
                write!(f, "`{name}`")
            };
            if let Err(e) = go() {
                self.f = Err(e);
            }
        }
    }
}