aws-smithy-schema 0.3.0

Schema types for the smithy-rs ecosystem
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! Round-trip tests through [`Document`] for every Smithy data-model type.
//!
//! Each test follows the same pattern:
//!
//! 1. Construct a typed value.
//! 2. Serialize it to a [`Document`] via [`Document::from_struct`].
//! 3. Deserialize the [`Document`] back to the same typed value via
//!    [`Document::as_shape`].
//! 4. Assert structural equality between the original and the restored
//!    value.
//!
//! Coverage:
//! - All simple types: byte, short, integer, long, float, double,
//!   bigInteger, bigDecimal, boolean, string, blob, timestamp.
//! - Aggregates: dense list, sparse list (with intermixed nulls),
//!   nested list, typed map, list of structures, map of structures.
//! - Recursive structures (a tree shape with self-referential members).
//! - Discriminator preservation across the round-trip.
//!
//! The unit tests scattered through [`super::serializer`] and
//! [`super::deserializer`] cover individual write / read paths and their
//! error cases. This module exercises the *combination* — the same
//! lossless contract every codec is supposed to honour.

use std::collections::HashMap;
use std::str::FromStr;

use aws_smithy_types::{BigDecimal, BigInteger, Blob, DateTime, DiscriminatedDocument};

use super::{DiscriminatedDocumentExt, DocumentShapeDeserializer, DocumentShapeSerializer};
use crate::serde::{SerdeError, SerializableStruct, ShapeDeserializer, ShapeSerializer};
use crate::{prelude, shape_id, Schema, ShapeId, ShapeType};

// -- AllTypes: every Smithy simple type + a typed list and map --------

const ALL_TYPES_ID: ShapeId<'static> = shape_id!("smithy.example", "AllTypes");

// Every member is optional so the same struct can drive both
// "everything populated" and "everything absent" tests.
static M_BYTE: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_byte"),
    ShapeType::Byte,
    "a_byte",
    0,
);
static M_SHORT: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_short"),
    ShapeType::Short,
    "a_short",
    1,
);
static M_INTEGER: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "an_integer"),
    ShapeType::Integer,
    "an_integer",
    2,
);
static M_LONG: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_long"),
    ShapeType::Long,
    "a_long",
    3,
);
static M_FLOAT: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_float"),
    ShapeType::Float,
    "a_float",
    4,
);
static M_DOUBLE: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_double"),
    ShapeType::Double,
    "a_double",
    5,
);
static M_BIG_INTEGER: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_big_integer"),
    ShapeType::BigInteger,
    "a_big_integer",
    6,
);
static M_BIG_DECIMAL: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_big_decimal"),
    ShapeType::BigDecimal,
    "a_big_decimal",
    7,
);
static M_BOOLEAN: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_boolean"),
    ShapeType::Boolean,
    "a_boolean",
    8,
);
static M_STRING: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_string"),
    ShapeType::String,
    "a_string",
    9,
);
static M_BLOB: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_blob"),
    ShapeType::Blob,
    "a_blob",
    10,
);
static M_TIMESTAMP: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_timestamp"),
    ShapeType::Timestamp,
    "a_timestamp",
    11,
);
static M_LIST: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_list"),
    ShapeType::List,
    "a_list",
    12,
);
static M_MAP: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "AllTypes", "a_map"),
    ShapeType::Map,
    "a_map",
    13,
);

static ALL_TYPES_SCHEMA: Schema<'static> = Schema::new_struct(
    ALL_TYPES_ID,
    ShapeType::Structure,
    &[
        &M_BYTE,
        &M_SHORT,
        &M_INTEGER,
        &M_LONG,
        &M_FLOAT,
        &M_DOUBLE,
        &M_BIG_INTEGER,
        &M_BIG_DECIMAL,
        &M_BOOLEAN,
        &M_STRING,
        &M_BLOB,
        &M_TIMESTAMP,
        &M_LIST,
        &M_MAP,
    ],
);

#[derive(Debug, Default, PartialEq)]
struct AllTypes {
    a_byte: Option<i8>,
    a_short: Option<i16>,
    an_integer: Option<i32>,
    a_long: Option<i64>,
    a_float: Option<f32>,
    a_double: Option<f64>,
    a_big_integer: Option<BigInteger>,
    a_big_decimal: Option<BigDecimal>,
    a_boolean: Option<bool>,
    a_string: Option<String>,
    a_blob: Option<Blob>,
    a_timestamp: Option<DateTime>,
    a_list: Option<Vec<String>>,
    a_map: Option<HashMap<String, String>>,
}

impl SerializableStruct for AllTypes {
    fn serialize_members(&self, ser: &mut dyn ShapeSerializer) -> Result<(), SerdeError> {
        if let Some(v) = self.a_byte {
            ser.write_byte(&M_BYTE, v)?;
        }
        if let Some(v) = self.a_short {
            ser.write_short(&M_SHORT, v)?;
        }
        if let Some(v) = self.an_integer {
            ser.write_integer(&M_INTEGER, v)?;
        }
        if let Some(v) = self.a_long {
            ser.write_long(&M_LONG, v)?;
        }
        if let Some(v) = self.a_float {
            ser.write_float(&M_FLOAT, v)?;
        }
        if let Some(v) = self.a_double {
            ser.write_double(&M_DOUBLE, v)?;
        }
        if let Some(v) = &self.a_big_integer {
            ser.write_big_integer(&M_BIG_INTEGER, v)?;
        }
        if let Some(v) = &self.a_big_decimal {
            ser.write_big_decimal(&M_BIG_DECIMAL, v)?;
        }
        if let Some(v) = self.a_boolean {
            ser.write_boolean(&M_BOOLEAN, v)?;
        }
        if let Some(v) = &self.a_string {
            ser.write_string(&M_STRING, v)?;
        }
        if let Some(v) = &self.a_blob {
            ser.write_blob(&M_BLOB, v.clone())?;
        }
        if let Some(v) = self.a_timestamp {
            ser.write_timestamp(&M_TIMESTAMP, &v)?;
        }
        if let Some(items) = &self.a_list {
            ser.write_list(&M_LIST, &|inner| {
                for s in items {
                    inner.write_string(&prelude::STRING, s)?;
                }
                Ok(())
            })?;
        }
        if let Some(entries) = &self.a_map {
            ser.write_map(&M_MAP, &|inner| {
                for (k, v) in entries {
                    inner.write_string(&prelude::STRING, k)?;
                    inner.write_string(&prelude::STRING, v)?;
                }
                Ok(())
            })?;
        }
        Ok(())
    }
}

fn deserialize_all_types(deser: &mut dyn ShapeDeserializer) -> Result<AllTypes, SerdeError> {
    let mut out = AllTypes::default();
    deser.read_struct(&ALL_TYPES_SCHEMA, &mut |member, sub| {
        match member.member_index() {
            Some(0) => out.a_byte = Some(sub.read_byte(member)?),
            Some(1) => out.a_short = Some(sub.read_short(member)?),
            Some(2) => out.an_integer = Some(sub.read_integer(member)?),
            Some(3) => out.a_long = Some(sub.read_long(member)?),
            Some(4) => out.a_float = Some(sub.read_float(member)?),
            Some(5) => out.a_double = Some(sub.read_double(member)?),
            Some(6) => out.a_big_integer = Some(sub.read_big_integer(member)?),
            Some(7) => out.a_big_decimal = Some(sub.read_big_decimal(member)?),
            Some(8) => out.a_boolean = Some(sub.read_boolean(member)?),
            Some(9) => out.a_string = Some(sub.read_string(member)?),
            Some(10) => out.a_blob = Some(sub.read_blob(member)?),
            Some(11) => out.a_timestamp = Some(sub.read_timestamp(member)?),
            Some(12) => {
                let mut items = Vec::new();
                sub.read_list(member, &mut |item| {
                    items.push(item.read_string(&prelude::STRING)?);
                    Ok(())
                })?;
                out.a_list = Some(items);
            }
            Some(13) => {
                let mut entries = HashMap::new();
                sub.read_map(member, &mut |key, value| {
                    entries.insert(key, value.read_string(&prelude::STRING)?);
                    Ok(())
                })?;
                out.a_map = Some(entries);
            }
            _ => {}
        }
        Ok(())
    })?;
    Ok(out)
}

#[test]
fn round_trip_every_simple_type_and_simple_aggregates() {
    let original = AllTypes {
        a_byte: Some(-12),
        a_short: Some(1234),
        an_integer: Some(-987_654),
        a_long: Some(9_876_543_210),
        a_float: Some(3.5),
        a_double: Some(2.123_456_789),
        a_big_integer: Some("123456789012345678901234567890".parse().unwrap()),
        a_big_decimal: Some("123456789012345678901234567890.5".parse().unwrap()),
        a_boolean: Some(true),
        a_string: Some("hello".into()),
        a_blob: Some(Blob::new(vec![0x01, 0x02, 0x03, 0xff])),
        a_timestamp: Some(DateTime::from_secs(1_700_000_000)),
        a_list: Some(vec!["a".into(), "b".into(), "c".into()]),
        a_map: Some(HashMap::from([
            ("k1".into(), "v1".into()),
            ("k2".into(), "v2".into()),
        ])),
    };

    let doc = DiscriminatedDocument::from_struct(&ALL_TYPES_SCHEMA, &original).unwrap();
    let restored = doc.as_shape(deserialize_all_types).unwrap();
    assert_eq!(restored, original);
}

#[test]
fn round_trip_with_all_members_absent_yields_empty_struct() {
    let original = AllTypes::default();
    let doc = DiscriminatedDocument::from_struct(&ALL_TYPES_SCHEMA, &original).unwrap();
    let restored = doc.as_shape(deserialize_all_types).unwrap();
    assert_eq!(restored, original);
}

#[test]
fn round_trip_preserves_struct_discriminator() {
    let original = AllTypes {
        a_string: Some("anchor".into()),
        ..AllTypes::default()
    };
    let doc = DiscriminatedDocument::from_struct(&ALL_TYPES_SCHEMA, &original).unwrap();
    assert_eq!(doc.discriminator(), Some("smithy.example#AllTypes"));
    // The discriminator does not interfere with deserialization.
    let restored = doc.as_shape(deserialize_all_types).unwrap();
    assert_eq!(restored, original);
}

// -- Sparse list of strings -----------------------------------------------

const SPARSE_LIST_HOLDER_ID: ShapeId<'static> = shape_id!("smithy.example", "SparseListHolder");
static M_SPARSE_LIST: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "SparseListHolder", "values"),
    ShapeType::List,
    "values",
    0,
);
static SPARSE_LIST_HOLDER_SCHEMA: Schema<'static> = Schema::new_struct(
    SPARSE_LIST_HOLDER_ID,
    ShapeType::Structure,
    &[&M_SPARSE_LIST],
);

#[derive(Debug, Default, PartialEq)]
struct SparseListHolder {
    values: Vec<Option<String>>,
}

impl SerializableStruct for SparseListHolder {
    fn serialize_members(&self, ser: &mut dyn ShapeSerializer) -> Result<(), SerdeError> {
        ser.write_list(&M_SPARSE_LIST, &|inner| {
            for v in &self.values {
                match v {
                    Some(s) => inner.write_string(&prelude::STRING, s)?,
                    None => inner.write_null(&prelude::STRING)?,
                }
            }
            Ok(())
        })
    }
}

fn deserialize_sparse_list_holder(
    deser: &mut dyn ShapeDeserializer,
) -> Result<SparseListHolder, SerdeError> {
    let mut out = SparseListHolder::default();
    deser.read_struct(&SPARSE_LIST_HOLDER_SCHEMA, &mut |member, sub| {
        if member.member_index() == Some(0) {
            sub.read_list(member, &mut |item| {
                if item.is_null() {
                    out.values.push(None);
                } else {
                    out.values.push(Some(item.read_string(&prelude::STRING)?));
                }
                Ok(())
            })?;
        }
        Ok(())
    })?;
    Ok(out)
}

#[test]
fn round_trip_sparse_list_preserves_null_positions() {
    let original = SparseListHolder {
        values: vec![
            Some("first".into()),
            None,
            Some("third".into()),
            None,
            None,
            Some("sixth".into()),
        ],
    };
    let doc = DiscriminatedDocument::from_struct(&SPARSE_LIST_HOLDER_SCHEMA, &original).unwrap();
    let restored = doc.as_shape(deserialize_sparse_list_holder).unwrap();
    assert_eq!(restored, original);
}

// -- Nested aggregates: list of struct, map of struct ---------------------

// `struct Item { id: String, count: i32 }` used as element / value type.

const ITEM_ID: ShapeId<'static> = shape_id!("smithy.example", "Item");
static M_ITEM_ID: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "Item", "id"),
    ShapeType::String,
    "id",
    0,
);
static M_ITEM_COUNT: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "Item", "count"),
    ShapeType::Integer,
    "count",
    1,
);
static ITEM_SCHEMA: Schema<'static> =
    Schema::new_struct(ITEM_ID, ShapeType::Structure, &[&M_ITEM_ID, &M_ITEM_COUNT]);

#[derive(Debug, Default, Eq, PartialEq, Hash, Clone)]
struct Item {
    id: String,
    count: i32,
}

impl SerializableStruct for Item {
    fn serialize_members(&self, ser: &mut dyn ShapeSerializer) -> Result<(), SerdeError> {
        ser.write_string(&M_ITEM_ID, &self.id)?;
        ser.write_integer(&M_ITEM_COUNT, self.count)?;
        Ok(())
    }
}

fn deserialize_item(deser: &mut dyn ShapeDeserializer) -> Result<Item, SerdeError> {
    let mut out = Item::default();
    deser.read_struct(&ITEM_SCHEMA, &mut |member, sub| {
        match member.member_index() {
            Some(0) => out.id = sub.read_string(member)?,
            Some(1) => out.count = sub.read_integer(member)?,
            _ => {}
        }
        Ok(())
    })?;
    Ok(out)
}

const ITEM_BAG_ID: ShapeId<'static> = shape_id!("smithy.example", "ItemBag");
static M_ITEM_LIST: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "ItemBag", "items"),
    ShapeType::List,
    "items",
    0,
);
static M_ITEM_MAP: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "ItemBag", "named_items"),
    ShapeType::Map,
    "named_items",
    1,
);
static ITEM_BAG_SCHEMA: Schema<'static> = Schema::new_struct(
    ITEM_BAG_ID,
    ShapeType::Structure,
    &[&M_ITEM_LIST, &M_ITEM_MAP],
);

#[derive(Debug, Default, PartialEq)]
struct ItemBag {
    items: Vec<Item>,
    named_items: HashMap<String, Item>,
}

impl SerializableStruct for ItemBag {
    fn serialize_members(&self, ser: &mut dyn ShapeSerializer) -> Result<(), SerdeError> {
        ser.write_list(&M_ITEM_LIST, &|inner| {
            for item in &self.items {
                inner.write_struct(&ITEM_SCHEMA, item)?;
            }
            Ok(())
        })?;
        ser.write_map(&M_ITEM_MAP, &|inner| {
            for (k, v) in &self.named_items {
                inner.write_string(&prelude::STRING, k)?;
                inner.write_struct(&ITEM_SCHEMA, v)?;
            }
            Ok(())
        })?;
        Ok(())
    }
}

fn deserialize_item_bag(deser: &mut dyn ShapeDeserializer) -> Result<ItemBag, SerdeError> {
    let mut out = ItemBag::default();
    deser.read_struct(&ITEM_BAG_SCHEMA, &mut |member, sub| {
        match member.member_index() {
            Some(0) => {
                sub.read_list(member, &mut |item_de| {
                    out.items.push(deserialize_item(item_de)?);
                    Ok(())
                })?;
            }
            Some(1) => {
                sub.read_map(member, &mut |key, value_de| {
                    out.named_items.insert(key, deserialize_item(value_de)?);
                    Ok(())
                })?;
            }
            _ => {}
        }
        Ok(())
    })?;
    Ok(out)
}

#[test]
fn round_trip_list_of_structs_and_map_of_structs() {
    let original = ItemBag {
        items: vec![
            Item {
                id: "alpha".into(),
                count: 1,
            },
            Item {
                id: "beta".into(),
                count: 22,
            },
            Item {
                id: "gamma".into(),
                count: 333,
            },
        ],
        named_items: HashMap::from([
            (
                "primary".to_string(),
                Item {
                    id: "p".into(),
                    count: 9,
                },
            ),
            (
                "secondary".to_string(),
                Item {
                    id: "s".into(),
                    count: 81,
                },
            ),
        ]),
    };
    let doc = DiscriminatedDocument::from_struct(&ITEM_BAG_SCHEMA, &original).unwrap();
    let restored = doc.as_shape(deserialize_item_bag).unwrap();
    assert_eq!(restored, original);
}

// -- Recursive structure --------------------------------------------------

// `struct Tree { value: i32, left: Option<Box<Tree>>, right: Option<Box<Tree>> }`

const TREE_ID: ShapeId<'static> = shape_id!("smithy.example", "Tree");
static M_TREE_VALUE: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "Tree", "value"),
    ShapeType::Integer,
    "value",
    0,
);
static M_TREE_LEFT: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "Tree", "left"),
    ShapeType::Structure,
    "left",
    1,
);
static M_TREE_RIGHT: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "Tree", "right"),
    ShapeType::Structure,
    "right",
    2,
);
static TREE_SCHEMA: Schema<'static> = Schema::new_struct(
    TREE_ID,
    ShapeType::Structure,
    &[&M_TREE_VALUE, &M_TREE_LEFT, &M_TREE_RIGHT],
);

#[derive(Debug, Default, PartialEq)]
struct Tree {
    value: i32,
    left: Option<Box<Tree>>,
    right: Option<Box<Tree>>,
}

impl SerializableStruct for Tree {
    fn serialize_members(&self, ser: &mut dyn ShapeSerializer) -> Result<(), SerdeError> {
        ser.write_integer(&M_TREE_VALUE, self.value)?;
        if let Some(left) = &self.left {
            // For nested struct members, codegen passes the *member*
            // schema (not the target's root schema) so commit_value can
            // resolve the member name from the parent struct frame.
            // See SchemaGenerator.kt's write_struct emission for member
            // contexts.
            ser.write_struct(&M_TREE_LEFT, left.as_ref())?;
        }
        if let Some(right) = &self.right {
            ser.write_struct(&M_TREE_RIGHT, right.as_ref())?;
        }
        Ok(())
    }
}

fn deserialize_tree(deser: &mut dyn ShapeDeserializer) -> Result<Tree, SerdeError> {
    let mut out = Tree::default();
    deser.read_struct(&TREE_SCHEMA, &mut |member, sub| {
        match member.member_index() {
            Some(0) => out.value = sub.read_integer(member)?,
            Some(1) => out.left = Some(Box::new(deserialize_tree(sub)?)),
            Some(2) => out.right = Some(Box::new(deserialize_tree(sub)?)),
            _ => {}
        }
        Ok(())
    })?;
    Ok(out)
}

#[test]
fn round_trip_recursive_tree_structure() {
    // Tree:
    //         1
    //        / \
    //       2   3
    //      /     \
    //     4       5
    let original = Tree {
        value: 1,
        left: Some(Box::new(Tree {
            value: 2,
            left: Some(Box::new(Tree {
                value: 4,
                left: None,
                right: None,
            })),
            right: None,
        })),
        right: Some(Box::new(Tree {
            value: 3,
            left: None,
            right: Some(Box::new(Tree {
                value: 5,
                left: None,
                right: None,
            })),
        })),
    };
    let doc = DiscriminatedDocument::from_struct(&TREE_SCHEMA, &original).unwrap();
    let restored = doc.as_shape(deserialize_tree).unwrap();
    assert_eq!(restored, original);
}

// -- Nested list-of-list --------------------------------------------------

const MATRIX_HOLDER_ID: ShapeId<'static> = shape_id!("smithy.example", "MatrixHolder");
static M_MATRIX: Schema<'static> = Schema::new_member(
    shape_id!("smithy.example", "MatrixHolder", "matrix"),
    ShapeType::List,
    "matrix",
    0,
);
static MATRIX_HOLDER_SCHEMA: Schema<'static> =
    Schema::new_struct(MATRIX_HOLDER_ID, ShapeType::Structure, &[&M_MATRIX]);

#[derive(Debug, Default, PartialEq)]
struct MatrixHolder {
    matrix: Vec<Vec<i32>>,
}

impl SerializableStruct for MatrixHolder {
    fn serialize_members(&self, ser: &mut dyn ShapeSerializer) -> Result<(), SerdeError> {
        ser.write_list(&M_MATRIX, &|outer| {
            for row in &self.matrix {
                outer.write_list(&prelude::DOCUMENT, &|inner| {
                    for v in row {
                        inner.write_integer(&prelude::INTEGER, *v)?;
                    }
                    Ok(())
                })?;
            }
            Ok(())
        })
    }
}

fn deserialize_matrix_holder(
    deser: &mut dyn ShapeDeserializer,
) -> Result<MatrixHolder, SerdeError> {
    let mut out = MatrixHolder::default();
    deser.read_struct(&MATRIX_HOLDER_SCHEMA, &mut |member, sub| {
        if member.member_index() == Some(0) {
            sub.read_list(member, &mut |row_de| {
                let mut row = Vec::new();
                row_de.read_list(&prelude::DOCUMENT, &mut |item| {
                    row.push(item.read_integer(&prelude::INTEGER)?);
                    Ok(())
                })?;
                out.matrix.push(row);
                Ok(())
            })?;
        }
        Ok(())
    })?;
    Ok(out)
}

#[test]
fn round_trip_nested_list_of_lists() {
    let original = MatrixHolder {
        matrix: vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]],
    };
    let doc = DiscriminatedDocument::from_struct(&MATRIX_HOLDER_SCHEMA, &original).unwrap();
    let restored = doc.as_shape(deserialize_matrix_holder).unwrap();
    assert_eq!(restored, original);
}

// -- Direct (non-helper) round-trip exercising both adapters explicitly ---

#[test]
fn round_trip_via_explicit_serializer_and_deserializer() {
    // Same shape as the AllTypes test, but uses the
    // DocumentShapeSerializer / DocumentShapeDeserializer directly to
    // make sure the helper entry points (`from_struct`, `as_shape`)
    // are not the only path that works.
    let original = AllTypes {
        a_string: Some("explicit".into()),
        an_integer: Some(123),
        a_big_decimal: Some(BigDecimal::from_str("0.0001").unwrap()),
        ..AllTypes::default()
    };

    let mut ser = DocumentShapeSerializer::new();
    ser.write_struct(&ALL_TYPES_SCHEMA, &original).unwrap();
    let doc = ser.finish().unwrap();

    let mut deser = DocumentShapeDeserializer::new(doc.document());
    let restored = deserialize_all_types(&mut deser).unwrap();

    assert_eq!(restored, original);
}