smithy4rs-core 0.0.1

Core functionality for smithy4rs.
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Schema-guided serialization of data from a Smithy Shape
//!
//! TODO(docs): Implementation docs
use std::{error::Error as StdError, fmt::Display};

use crate::{
    BigDecimal, BigInt, ByteBuffer, IndexMap, Instant,
    schema::{Document, SchemaRef, SchemaShape, ShapeId},
};

// ============================================================================
// Shape Traits
// ============================================================================

/// Serialize a shape with its pre-defined schema.
///
/// This trait provides an automatic, blanket implementation for all shapes
/// with both a [`SchemaShape`], and [`SerializeWithSchema`] implementation.
///
pub trait SerializableShape: SchemaShape + SerializeWithSchema {
    /// Serialize a shape with its pre-defined schema
    ///
    /// # Errors
    /// Returns an [`Error`] if the shape could not be serialized.
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>;
}

// Blanket implementation of serialization for all Implement
impl<T: SchemaShape + SerializeWithSchema> SerializableShape for T {
    #[inline]
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.serialize_with_schema(self.schema(), serializer)
    }
}

/// Schema-Guided serialization implementation.
pub trait SerializeWithSchema {
    /// Serialize a Shape using a schema to guide the process
    ///
    /// # Errors
    /// Returns an [`Error`] if the shape could not be serialized.
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error>;
}

// ============================================================================
// Error Trait
// ============================================================================

/// Error trait for serialization errors.
///
/// <div class="note">
/// **NOTE**: This is essentially a clone of the `serde::Error` trait, but
/// we use our own to ensure we don't enforce a `serde` dependency
/// on consumers.
/// </div>
///
pub trait Error: Sized + StdError {
    /// Create an error with a custom message
    fn custom<T: Display>(msg: T) -> Self;
}

// ============================================================================
// Core Serialize Traits
// ============================================================================

/// List Serializer that can be called in a loop to serialize list values
pub trait ListSerializer {
    /// Must match the `Error` type of our `Serializer`.
    type Error: Error;

    /// Must match the `Ok` type of our `Serializer`.
    type Ok;

    /// Serialize a sequence element.
    ///
    /// # Errors
    /// Returns an [`Error`] matching the parent serializer if
    /// the element could not be serialized.
    fn serialize_element<T>(
        &mut self,
        element_schema: &SchemaRef,
        value: &T,
    ) -> Result<(), Self::Error>
    where
        T: SerializeWithSchema;

    /// Finish serializing a sequence.
    ///
    /// # Errors
    /// [`Error`] if the sequence could not be closed
    fn end(self, schema: &SchemaRef) -> Result<Self::Ok, Self::Error>;
}

/// Map Serializer that can be called in a loop to serialize map values
pub trait MapSerializer {
    /// Must match the `Error` type of our [`Serializer`].
    type Error: Error;

    /// Must match the `Ok` type of our `Serializer`.
    type Ok;

    /// Serialize a single map entry
    ///
    /// # Errors
    /// Returns an [`Error`] matching the parent serializer if
    /// the entry could not be serialized.
    fn serialize_entry<K, V>(
        &mut self,
        key_schema: &SchemaRef,
        value_schema: &SchemaRef,
        key: &K,
        value: &V,
    ) -> Result<(), Self::Error>
    where
        K: SerializeWithSchema,
        V: SerializeWithSchema;

    /// Finish serializing a map.
    ///
    /// # Errors
    /// Returns an [`Error`] matching the parent serializer if
    /// the map could not be closed.
    fn end(self, schema: &SchemaRef) -> Result<Self::Ok, Self::Error>;
}

/// Struct Serializer that can be called to serialize struct member values
pub trait StructSerializer {
    /// Must match the `Error` type of our [`Serializer`].
    type Error: Error;

    /// Must match the `Ok` type of our `Serializer`.
    type Ok;

    /// Optionally serializes the discriminator of a shape.
    ///
    /// In general this is only done for document types to allow for
    /// over-the-wire polymorphism, and by default this method does nothing.
    ///
    /// # Errors
    /// Returns an [`Error`] matching the parent serializer if
    /// the discriminator could not be serialized.
    #[inline]
    fn serialize_discriminator(&mut self, _discriminator: &ShapeId) -> Result<(), Self::Error> {
        Ok(())
    }

    /// Serialize a member on the struct
    ///
    /// # Errors
    /// Returns an [`Error`] matching the parent serializer if
    /// the member could not be serialized.
    fn serialize_member<T>(
        &mut self,
        member_schema: &SchemaRef,
        value: &T,
    ) -> Result<(), Self::Error>
    where
        T: SerializeWithSchema;

    /// Serialize a member on the struct with a pre-known field name.
    /// This is an optimization to avoid extracting the name from the schema.
    ///
    /// # Errors
    /// Returns an [`Error`] matching the parent serializer if
    /// the member could not be serialized.
    #[inline]
    fn serialize_member_named<T>(
        &mut self,
        _member_name: &str,
        member_schema: &SchemaRef,
        value: &T,
    ) -> Result<(), Self::Error>
    where
        T: SerializeWithSchema,
    {
        // Default implementation falls back to regular serialize_member
        self.serialize_member(member_schema, value)
    }

    /// Serializes an optional member.
    ///
    /// This method will call [`StructSerializer::skip_member`] on any optional members
    /// that are `None`, otherwise the `Some` value is unwrapped and serialized as normal.
    ///
    /// # Errors
    /// Returns an [`Error`] matching the parent serializer if
    /// the member could not be serialized.
    fn serialize_optional_member<T: SerializeWithSchema>(
        &mut self,
        member_schema: &SchemaRef,
        value: &Option<T>,
    ) -> Result<(), Self::Error> {
        if let Some(value) = value {
            self.serialize_member(member_schema, value)
        } else {
            self.skip_member(member_schema)
        }
    }

    /// Serializes an optional member with a pre-known field name.
    /// This is an optimization to avoid extracting the name from the schema.
    ///
    /// # Errors
    /// Returns an [`Error`] matching the parent serializer if
    /// the member could not be serialized.
    #[inline]
    fn serialize_optional_member_named<T: SerializeWithSchema>(
        &mut self,
        member_name: &str,
        member_schema: &SchemaRef,
        value: &Option<T>,
    ) -> Result<(), Self::Error> {
        if let Some(value) = value {
            self.serialize_member_named(member_name, member_schema, value)
        } else {
            self.skip_member(member_schema)
        }
    }

    /// Skips a member in a structure.
    ///
    /// # Errors
    /// Returns an [`Error`] matching the parent serializer if
    /// the member could not be skipped.
    #[inline]
    fn skip_member(&mut self, _schema: &SchemaRef) -> Result<(), Self::Error> {
        /* Do nothing on skip by default */
        Ok(())
    }

    /// Handle unknown values.
    ///
    /// # Errors
    /// Returns an [`Error`] matching the parent serializer if
    /// the unknown member could not be serialized.
    #[inline]
    fn serialize_unknown(&mut self, _schema: &SchemaRef, name: &String) -> Result<(), Self::Error> {
        // Error out on unknown by default
        // TODO(unknown members): Is this the correct default behavior?
        Err(Self::Error::custom(format!(
            "Attempted to serialize unknown value: {name:?}"
        )))
    }

    /// Finish serializing a structure
    ///
    /// # Errors
    /// Returns an [`Error`] matching the parent serializer if
    /// the structure could not be closed.
    fn end(self, schema: &SchemaRef) -> Result<Self::Ok, Self::Error>;
}

// TODO(streams): How should we handle data stream serialization?
// TODO(events): Do we need any custom handling for event streams?
/// A `Serialize` writes data from an output sink, guided by Smithy schemas.
///
/// This trait mirrors the [`Serializer`](serde::Serializer) trait, providing
/// schema-guided serialization for all Smithy data types.
///
/// The serializer is stateful and methods take `self`. Implementations should,
/// consider implement `Serializer` for `&mut` variants.
pub trait Serializer: Sized {
    /// Error type emitted on failed serialization.
    ///
    /// <div class ="note">
    /// **NOTE**: Serializers need to be able to catch and convert dyn Errors from their code.
    /// </div>
    type Error: Error;

    /// Ok return type. Should usually be `()`
    type Ok;

    /// Type returned from [`write_list`] for serializing the contents of a
    /// list.
    ///
    /// [`write_list`]: #tymethod.write_list
    type SerializeList: ListSerializer<Ok = Self::Ok, Error = Self::Error>;

    /// Type returned from [`write_map`] for serializing the contents of a
    /// map.
    ///
    /// [`write_map`]: #tymethod.write_map
    type SerializeMap: MapSerializer<Ok = Self::Ok, Error = Self::Error>;

    /// Type returned from [`write_struct`] for serializing the contents of a
    /// struct or union.
    ///
    /// [`write_struct`]: #tymethod.write_struct
    type SerializeStruct: StructSerializer<Ok = Self::Ok, Error = Self::Error>;

    /// Begin to serialize a variably sized structure or union. This call must be
    /// followed by zero or more calls to `serialize_member`, then a call to
    /// `end`.
    ///
    /// # Errors
    /// `Self::Error` if the structure could not be opened.
    fn write_struct(
        self,
        schema: &SchemaRef,
        len: usize,
    ) -> Result<Self::SerializeStruct, Self::Error>;

    /// Begin to serialize a variably sized map. This call must be
    /// followed by zero or more calls to `serialize_entry`, then a call to
    /// `end`.
    ///
    /// # Errors
    /// `Self::Error` if the map could not be opened.
    fn write_map(self, schema: &SchemaRef, len: usize) -> Result<Self::SerializeMap, Self::Error>;

    /// Begin to serialize a variably sized list. This call must be
    /// followed by zero or more calls to `serialize_element`, then a call to
    /// `end`.
    ///
    /// # Errors
    /// `Self::Error` if the list could not be opened.
    fn write_list(self, schema: &SchemaRef, len: usize)
    -> Result<Self::SerializeList, Self::Error>;

    /// Serialize a `boolean`
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as a boolean.
    fn write_boolean(self, schema: &SchemaRef, value: bool) -> Result<Self::Ok, Self::Error>;

    /// Serialize a byte (`i8`)
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as a `byte`.
    fn write_byte(self, schema: &SchemaRef, value: i8) -> Result<Self::Ok, Self::Error>;

    /// Serialize a short (`i16`)
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as a `short`.
    fn write_short(self, schema: &SchemaRef, value: i16) -> Result<Self::Ok, Self::Error>;

    /// Serialize an integer (`i32`)
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as an integer.
    fn write_integer(self, schema: &SchemaRef, value: i32) -> Result<Self::Ok, Self::Error>;

    /// Serialize a long (`i64`)
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as a `long`.
    fn write_long(self, schema: &SchemaRef, value: i64) -> Result<Self::Ok, Self::Error>;

    /// Serialize a float (`f32`)
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as a `float`.
    fn write_float(self, schema: &SchemaRef, value: f32) -> Result<Self::Ok, Self::Error>;

    /// Serialize a double (`f64`)
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as a `double`.
    fn write_double(self, schema: &SchemaRef, value: f64) -> Result<Self::Ok, Self::Error>;

    /// Serialize a [`BigInt`]
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as a `bigInteger`.
    fn write_big_integer(self, schema: &SchemaRef, value: &BigInt)
    -> Result<Self::Ok, Self::Error>;

    /// Serialize a [`BigDecimal`]
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as a `bigDecimal`.
    fn write_big_decimal(
        self,
        schema: &SchemaRef,
        value: &BigDecimal,
    ) -> Result<Self::Ok, Self::Error>;

    /// Serialize a string (`&str`)
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as a `string`.
    fn write_string(self, schema: &SchemaRef, value: &str) -> Result<Self::Ok, Self::Error>;

    /// Serialize a blob (i.e. a buffer)
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as a `blob`.
    fn write_blob(self, schema: &SchemaRef, value: &ByteBuffer) -> Result<Self::Ok, Self::Error>;

    /// Serialize a timestamp
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as a `timestamp`.
    fn write_timestamp(self, schema: &SchemaRef, value: &Instant) -> Result<Self::Ok, Self::Error>;

    /// Serialize an untyped [`Document`]
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as a `document`.
    #[allow(clippy::borrowed_box)]
    fn write_document(
        self,
        schema: &SchemaRef,
        value: &Box<dyn Document>,
    ) -> Result<Self::Ok, Self::Error>;

    /// Serialize a `null` value
    ///
    /// # Errors
    /// `Self::Error` if the value could not be serialized as an empty (`null`) value.
    fn write_null(self, schema: &SchemaRef) -> Result<Self::Ok, Self::Error>;

    /// Write a missing expected value.
    ///
    /// Default implementation simply `skip()`s the missing value.
    ///
    /// # Errors
    /// `Self::Error` if the missing value could not be serialized.
    fn write_missing(self, schema: &SchemaRef) -> Result<Self::Ok, Self::Error> {
        self.skip(schema)
    }

    /// Skip the serialization of a value.
    ///
    /// # Errors
    /// `Self::Error` if the value could not be skipped.
    fn skip(self, _schema: &SchemaRef) -> Result<Self::Ok, Self::Error>;

    /// Flush all remaining data.
    ///
    /// # Errors
    /// `Self::Error` if the underlying data source was not flushed successfully.
    fn flush(self) -> Result<Self::Ok, Self::Error> {
        todo!();
    }
}

// ============================================================================
// Default Implementations
// ============================================================================

// === Collection implementations ===
impl<T: SerializeWithSchema> SerializeWithSchema for Vec<T> {
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        let mut list = serializer.write_list(schema, self.len())?;
        let value_schema = schema.expect_member("member");
        for element in self {
            list.serialize_element(value_schema, element)?;
        }
        list.end(schema)
    }
}

impl<K, V> SerializeWithSchema for IndexMap<K, V>
where
    K: SerializeWithSchema,
    V: SerializeWithSchema,
{
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        let mut map = serializer.write_map(schema, self.len())?;
        // TODO(performance): is there a more efficient way to store/get these schemas?
        let key_schema = schema.expect_member("key");
        let value_schema = schema.expect_member("value");
        for (k, v) in self {
            map.serialize_entry(key_schema, value_schema, k, v)?;
        }
        map.end(schema)
    }
}

// === Scalar type implementations ===

impl SerializeWithSchema for bool {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.write_boolean(schema, *self)
    }
}

impl SerializeWithSchema for i8 {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.write_byte(schema, *self)
    }
}

impl SerializeWithSchema for i16 {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.write_short(schema, *self)
    }
}

impl SerializeWithSchema for i32 {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.write_integer(schema, *self)
    }
}

impl SerializeWithSchema for i64 {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.write_long(schema, *self)
    }
}

impl SerializeWithSchema for f32 {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.write_float(schema, *self)
    }
}

impl SerializeWithSchema for f64 {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.write_double(schema, *self)
    }
}

impl SerializeWithSchema for BigInt {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.write_big_integer(schema, self)
    }
}

impl SerializeWithSchema for BigDecimal {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.write_big_decimal(schema, self)
    }
}

impl SerializeWithSchema for ByteBuffer {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.write_blob(schema, self)
    }
}

impl SerializeWithSchema for Instant {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.write_timestamp(schema, self)
    }
}

impl SerializeWithSchema for String {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.write_string(schema, self)
    }
}

// === Wrapper-type implementations ===

impl<T: SerializeWithSchema> SerializeWithSchema for Option<T> {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        if let Some(value) = self.as_ref() {
            value.serialize_with_schema(schema, serializer)
        } else {
            serializer.skip(schema)
        }
    }
}

impl<T: SerializeWithSchema> SerializeWithSchema for Box<T> {
    #[inline]
    fn serialize_with_schema<S: Serializer>(
        &self,
        schema: &SchemaRef,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        (**self).serialize_with_schema(schema, serializer)
    }
}