asun 1.0.1

ASUN (Array-Schema Unified Notation) - A high-performance, token-efficient serde data format with schema-data separation
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
use asun::{decode, decode_binary, encode, encode_binary, encode_typed};
use serde::{Deserialize, Serialize};

// ===========================================================================
// Basic types (existing)
// ===========================================================================

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Department {
    title: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Employee {
    id: i64,
    name: String,
    dept: Department,
    skills: Vec<String>,
    active: bool,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct AttrEntry {
    key: String,
    value: i64,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct StringEntry {
    key: String,
    value: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct WithEntries {
    name: String,
    attrs: Vec<AttrEntry>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Nested {
    name: String,
    addr: Address,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Address {
    city: String,
    zip: i64,
}

// ===========================================================================
// All-types struct — every primitive and compound type ASUN supports
// ===========================================================================

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct AllTypes {
    b: bool,
    i8v: i8,
    i16v: i16,
    i32v: i32,
    i64v: i64,
    u8v: u8,
    u16v: u16,
    u32v: u32,
    u64v: u64,
    f32v: f32,
    f64v: f64,
    ch: char,
    s: String,
    opt_some: Option<i64>,
    opt_none: Option<i64>,
    vec_int: Vec<i64>,
    vec_str: Vec<String>,
    nested_vec: Vec<Vec<i64>>,
}

// ===========================================================================
// 5-level deep nesting: Country > Region > City > District > Street > Building
// ===========================================================================

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Building {
    name: String,
    floors: i64,
    residential: bool,
    height_m: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Street {
    name: String,
    length_km: f64,
    buildings: Vec<Building>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct District {
    name: String,
    population: i64,
    streets: Vec<Street>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct City {
    name: String,
    population: i64,
    area_km2: f64,
    districts: Vec<District>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Region {
    name: String,
    cities: Vec<City>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Country {
    name: String,
    code: String,
    population: i64,
    gdp_trillion: f64,
    regions: Vec<Region>,
}

// ===========================================================================
// 7-level deep: Universe > Galaxy > SolarSystem > Planet > Continent > Nation > State
// ===========================================================================

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct State {
    name: String,
    capital: String,
    population: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Nation {
    name: String,
    states: Vec<State>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Continent {
    name: String,
    nations: Vec<Nation>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Planet {
    name: String,
    radius_km: f64,
    has_life: bool,
    continents: Vec<Continent>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct SolarSystem {
    name: String,
    star_type: String,
    planets: Vec<Planet>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Galaxy {
    name: String,
    star_count_billions: f64,
    systems: Vec<SolarSystem>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Universe {
    name: String,
    age_billion_years: f64,
    galaxies: Vec<Galaxy>,
}

// ===========================================================================
// Enum variants — all forms
// ===========================================================================

#[derive(Debug, Serialize, Deserialize, PartialEq)]
enum Color {
    Red,
    Green,
    Blue,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
enum Shape {
    Circle(f64),
    Rectangle(f64, f64),
    Named { name: String, sides: i64 },
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Drawing {
    title: String,
    color: Color,
    shape: Shape,
    score: f64,
}

// ===========================================================================
// Large config-like struct with entry lists and optional fields
// ===========================================================================

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct DbConfig {
    host: String,
    port: i64,
    max_connections: i64,
    ssl: bool,
    timeout_ms: f64,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct CacheConfig {
    enabled: bool,
    ttl_seconds: i64,
    max_size_mb: i64,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct LogConfig {
    level: String,
    file: Option<String>,
    rotate: bool,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct ServiceConfig {
    name: String,
    version: String,
    db: DbConfig,
    cache: CacheConfig,
    log: LogConfig,
    features: Vec<String>,
    env: Vec<StringEntry>,
}

fn main() {
    println!("=== ASUN Complex Examples ===\n");

    // -----------------------------------------------------------------------
    // 1. Nested struct (existing)
    // -----------------------------------------------------------------------
    println!("1. Nested struct:");
    let emp: Employee =
        decode("{id,name,dept@{title},skills@[],active}:(1,Alice,(Manager),[rust],true)").unwrap();
    println!("   {:?}\n", emp);

    // -----------------------------------------------------------------------
    // 2. Vec with nested structs (existing)
    // -----------------------------------------------------------------------
    println!("2. Vec with nested structs:");
    let input = "[{id@int,name@str,dept@{title@str},skills@[str],active@bool}]:
  (1, Alice, (Manager), [Rust, Go], true),
  (2, Bob, (Engineer), [Python], false),
  (3, \"Carol Smith\", (Director), [Leadership, Strategy], true)";
    let employees: Vec<Employee> = decode(input).unwrap();
    for e in &employees {
        println!("   {:?}", e);
    }

    // -----------------------------------------------------------------------
    // 3. Entry-list field
    // -----------------------------------------------------------------------
    println!("\n3. Entry-list field:");
    let input = "{name,attrs@[{key,value}]}:(Alice,[(age,30),(score,95)])";
    let item: WithEntries = decode(input).unwrap();
    println!("   {:?}", item);

    // -----------------------------------------------------------------------
    // 4. Serialize nested struct roundtrip (existing)
    // -----------------------------------------------------------------------
    println!("\n4. Nested struct roundtrip:");
    let nested = Nested {
        name: "Alice".into(),
        addr: Address {
            city: "NYC".into(),
            zip: 10001,
        },
    };
    let s = encode(&nested).unwrap();
    println!("   serialized:   {}", s);
    let deserialized: Nested = decode(&s).unwrap();
    assert_eq!(nested, deserialized);
    println!("   ✓ roundtrip OK");

    // -----------------------------------------------------------------------
    // 5. Escaped strings (existing)
    // -----------------------------------------------------------------------
    println!("\n5. Escaped strings:");
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct Note {
        text: String,
    }
    let note = Note {
        text: "say \"hi\", then (wave)\tnewline\nend".into(),
    };
    let s = encode(&note).unwrap();
    println!("   serialized:   {}", s);
    let note2: Note = decode(&s).unwrap();
    assert_eq!(note, note2);
    println!("   ✓ escape roundtrip OK");

    // -----------------------------------------------------------------------
    // 6. Float fields (existing)
    // -----------------------------------------------------------------------
    println!("\n6. Float fields:");
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct Measurement {
        id: i64,
        value: f64,
        label: String,
    }
    let m = Measurement {
        id: 2,
        value: 95.0,
        label: "score".into(),
    };
    let s = encode(&m).unwrap();
    println!("   serialized: {}", s);
    let m2: Measurement = decode(&s).unwrap();
    assert_eq!(m, m2);
    println!("   ✓ float roundtrip OK");

    // -----------------------------------------------------------------------
    // 7. Negative numbers (existing)
    // -----------------------------------------------------------------------
    println!("\n7. Negative numbers:");
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct Nums {
        a: i64,
        b: f64,
        c: i64,
    }
    let n = Nums {
        a: -42,
        b: -3.15,
        c: i64::MIN + 1,
    };
    let s = encode(&n).unwrap();
    println!("   serialized:   {}", s);
    let n2: Nums = decode(&s).unwrap();
    assert_eq!(n, n2);
    println!("   ✓ negative roundtrip OK");

    // -----------------------------------------------------------------------
    // 8. All types struct
    // -----------------------------------------------------------------------
    println!("\n8. All types struct:");
    let all = AllTypes {
        b: true,
        i8v: -128,
        i16v: -32768,
        i32v: -2147483648,
        i64v: -9223372036854775807,
        u8v: 255,
        u16v: 65535,
        u32v: 4294967295,
        u64v: 18446744073709551615,
        f32v: 3.15,
        f64v: 2.718281828459045,
        ch: 'Z',
        s: "hello, world (test) [arr]".into(),
        opt_some: Some(42),
        opt_none: None,
        vec_int: vec![1, 2, 3, -4, 0],
        vec_str: vec!["alpha".into(), "beta gamma".into(), "delta".into()],
        nested_vec: vec![vec![1, 2], vec![3, 4, 5]],
    };
    let s = encode(&all).unwrap();
    println!("   serialized ({} bytes):", s.len());
    println!("   {}", s);
    let all2: AllTypes = decode(&s).unwrap();
    assert_eq!(all, all2);
    println!("   ✓ all-types roundtrip OK");

    // -----------------------------------------------------------------------
    // 9. Enum variants — unit, newtype, tuple, struct
    // -----------------------------------------------------------------------
    println!("\n9. Enum variants:");
    let d1 = Drawing {
        title: "Circle".into(),
        color: Color::Red,
        shape: Shape::Circle(5.0),
        score: 9.5,
    };
    let s1 = encode(&d1).unwrap();
    println!("   unit+newtype: {}", s1);
    let d1b: Drawing = decode(&s1).unwrap();
    assert_eq!(d1, d1b);

    let d2 = Drawing {
        title: "Rect".into(),
        color: Color::Blue,
        shape: Shape::Rectangle(3.0, 4.0),
        score: 8.2,
    };
    let s2 = encode(&d2).unwrap();
    println!("   tuple variant: {}", s2);
    let d2b: Drawing = decode(&s2).unwrap();
    assert_eq!(d2, d2b);

    let d3 = Drawing {
        title: "Polygon".into(),
        color: Color::Green,
        shape: Shape::Named {
            name: "hexagon".into(),
            sides: 6,
        },
        score: 7.0,
    };
    let s3 = encode(&d3).unwrap();
    println!("   struct variant: {}", s3);
    let d3b: Drawing = decode(&s3).unwrap();
    assert_eq!(d3, d3b);
    println!("   ✓ all enum variants roundtrip OK");

    // -----------------------------------------------------------------------
    // 10. 5-level deep: Country > Region > City > District > Street > Building
    // -----------------------------------------------------------------------
    println!("\n10. Five-level nesting (Country>Region>City>District>Street>Building):");
    let country = Country {
        name: "Rustland".into(),
        code: "RL".into(),
        population: 50_000_000,
        gdp_trillion: 1.5,
        regions: vec![
            Region {
                name: "Northern".into(),
                cities: vec![City {
                    name: "Ferriton".into(),
                    population: 2_000_000,
                    area_km2: 350.5,
                    districts: vec![
                        District {
                            name: "Downtown".into(),
                            population: 500_000,
                            streets: vec![
                                Street {
                                    name: "Main St".into(),
                                    length_km: 2.5,
                                    buildings: vec![
                                        Building {
                                            name: "Tower A".into(),
                                            floors: 50,
                                            residential: false,
                                            height_m: 200.0,
                                        },
                                        Building {
                                            name: "Apt Block 1".into(),
                                            floors: 12,
                                            residential: true,
                                            height_m: 40.5,
                                        },
                                    ],
                                },
                                Street {
                                    name: "Oak Ave".into(),
                                    length_km: 1.2,
                                    buildings: vec![Building {
                                        name: "Library".into(),
                                        floors: 3,
                                        residential: false,
                                        height_m: 15.0,
                                    }],
                                },
                            ],
                        },
                        District {
                            name: "Harbor".into(),
                            population: 150_000,
                            streets: vec![Street {
                                name: "Dock Rd".into(),
                                length_km: 0.8,
                                buildings: vec![Building {
                                    name: "Warehouse 7".into(),
                                    floors: 1,
                                    residential: false,
                                    height_m: 8.0,
                                }],
                            }],
                        },
                    ],
                }],
            },
            Region {
                name: "Southern".into(),
                cities: vec![City {
                    name: "Crabville".into(),
                    population: 800_000,
                    area_km2: 120.0,
                    districts: vec![District {
                        name: "Old Town".into(),
                        population: 200_000,
                        streets: vec![Street {
                            name: "Heritage Ln".into(),
                            length_km: 0.5,
                            buildings: vec![
                                Building {
                                    name: "Museum".into(),
                                    floors: 2,
                                    residential: false,
                                    height_m: 12.0,
                                },
                                Building {
                                    name: "Town Hall".into(),
                                    floors: 4,
                                    residential: false,
                                    height_m: 20.0,
                                },
                            ],
                        }],
                    }],
                }],
            },
        ],
    };
    let s = encode(&country).unwrap();
    println!("   serialized ({} bytes)", s.len());
    println!("   first 200 chars: {}...", &s[..200.min(s.len())]);
    let country2: Country = decode(&s).unwrap();
    assert_eq!(country, country2);
    println!("   ✓ 5-level ASUN-text roundtrip OK");

    // ASUN binary roundtrip
    let bin = encode_binary(&country).unwrap();
    let country3: Country = decode_binary(&bin).unwrap();
    assert_eq!(country, country3);
    println!("   ✓ 5-level ASUN-bin roundtrip OK");

    // Size comparison: ASUN-text vs ASUN-bin vs JSON
    let json = serde_json::to_string(&country).unwrap();
    println!(
        "   ASUN text: {} B | ASUN bin: {} B | JSON: {} B",
        s.len(),
        bin.len(),
        json.len()
    );
    println!(
        "   BIN vs JSON: {:.0}% smaller | TEXT vs JSON: {:.0}% smaller",
        (1.0 - bin.len() as f64 / json.len() as f64) * 100.0,
        (1.0 - s.len() as f64 / json.len() as f64) * 100.0
    );

    // -----------------------------------------------------------------------
    // 11. 7-level deep: Universe > Galaxy > SolarSystem > Planet > Continent > Nation > State
    // -----------------------------------------------------------------------
    println!(
        "\n11. Seven-level nesting (Universe>Galaxy>SolarSystem>Planet>Continent>Nation>State):"
    );
    let universe = Universe {
        name: "Observable".into(),
        age_billion_years: 13.8,
        galaxies: vec![Galaxy {
            name: "Milky Way".into(),
            star_count_billions: 250.0,
            systems: vec![SolarSystem {
                name: "Sol".into(),
                star_type: "G2V".into(),
                planets: vec![
                    Planet {
                        name: "Earth".into(),
                        radius_km: 6371.0,
                        has_life: true,
                        continents: vec![
                            Continent {
                                name: "Asia".into(),
                                nations: vec![
                                    Nation {
                                        name: "Japan".into(),
                                        states: vec![
                                            State {
                                                name: "Tokyo".into(),
                                                capital: "Shinjuku".into(),
                                                population: 14_000_000,
                                            },
                                            State {
                                                name: "Osaka".into(),
                                                capital: "Osaka City".into(),
                                                population: 8_800_000,
                                            },
                                        ],
                                    },
                                    Nation {
                                        name: "China".into(),
                                        states: vec![State {
                                            name: "Beijing".into(),
                                            capital: "Beijing".into(),
                                            population: 21_500_000,
                                        }],
                                    },
                                ],
                            },
                            Continent {
                                name: "Europe".into(),
                                nations: vec![Nation {
                                    name: "Germany".into(),
                                    states: vec![
                                        State {
                                            name: "Bavaria".into(),
                                            capital: "Munich".into(),
                                            population: 13_000_000,
                                        },
                                        State {
                                            name: "Berlin".into(),
                                            capital: "Berlin".into(),
                                            population: 3_600_000,
                                        },
                                    ],
                                }],
                            },
                        ],
                    },
                    Planet {
                        name: "Mars".into(),
                        radius_km: 3389.5,
                        has_life: false,
                        continents: vec![],
                    },
                ],
            }],
        }],
    };
    let s = encode(&universe).unwrap();
    println!("   serialized ({} bytes)", s.len());
    let universe2: Universe = decode(&s).unwrap();
    assert_eq!(universe, universe2);
    println!("   ✓ 7-level ASUN-text roundtrip OK");

    // ASUN binary roundtrip
    let bin = encode_binary(&universe).unwrap();
    let universe3: Universe = decode_binary(&bin).unwrap();
    assert_eq!(universe, universe3);
    println!("   ✓ 7-level ASUN-bin roundtrip OK");

    let json = serde_json::to_string(&universe).unwrap();
    println!(
        "   ASUN text: {} B | ASUN bin: {} B | JSON: {} B",
        s.len(),
        bin.len(),
        json.len()
    );
    println!(
        "   BIN vs JSON: {:.0}% smaller | TEXT vs JSON: {:.0}% smaller",
        (1.0 - bin.len() as f64 / json.len() as f64) * 100.0,
        (1.0 - s.len() as f64 / json.len() as f64) * 100.0
    );

    // -----------------------------------------------------------------------
    // 12. Service config with entry lists + optional + nested
    // -----------------------------------------------------------------------
    println!("\n12. Complex config struct (nested + entry-list + optional):");
    let env = vec![
        StringEntry {
            key: "RUST_LOG".into(),
            value: "debug".into(),
        },
        StringEntry {
            key: "DATABASE_URL".into(),
            value: "postgres://localhost:5432/mydb".into(),
        },
        StringEntry {
            key: "SECRET_KEY".into(),
            value: "abc123!@#".into(),
        },
    ];

    let config = ServiceConfig {
        name: "my-service".into(),
        version: "2.1.0".into(),
        db: DbConfig {
            host: "db.example.com".into(),
            port: 5432,
            max_connections: 100,
            ssl: true,
            timeout_ms: 3000.5,
        },
        cache: CacheConfig {
            enabled: true,
            ttl_seconds: 3600,
            max_size_mb: 512,
        },
        log: LogConfig {
            level: "info".into(),
            file: Some("/var/log/app.log".into()),
            rotate: true,
        },
        features: vec!["auth".into(), "rate-limit".into(), "websocket".into()],
        env,
    };
    let s = encode(&config).unwrap();
    println!("   serialized ({} bytes):", s.len());
    println!("   {}", s);
    let config2: ServiceConfig = decode(&s).unwrap();
    assert_eq!(config, config2);
    println!("   ✓ config roundtrip OK");

    let json = serde_json::to_string(&config).unwrap();
    println!(
        "   ASUN text: {} B | JSON: {} B | TEXT vs JSON: {:.0}% smaller",
        s.len(),
        json.len(),
        (1.0 - s.len() as f64 / json.len() as f64) * 100.0
    );
    // Binary roundtrip
    let bin = encode_binary(&config).unwrap();
    let config3: ServiceConfig = decode_binary(&bin).unwrap();
    assert_eq!(config, config3);
    println!("   ✓ config ASUN-bin roundtrip OK");
    println!(
        "   ASUN bin: {} B | BIN vs JSON: {:.0}% smaller",
        bin.len(),
        (1.0 - bin.len() as f64 / json.len() as f64) * 100.0
    );

    // -----------------------------------------------------------------------
    // 13. Large structure — 100 countries, each with regions/cities/etc
    // -----------------------------------------------------------------------
    println!("\n13. Large structure (100 countries × nested regions):");
    let countries: Vec<Country> = (0..100)
        .map(|i| Country {
            name: format!("Country_{}", i),
            code: format!("C{:02}", i % 100),
            population: 1_000_000 + i * 500_000,
            gdp_trillion: (i as f64) * 0.5,
            regions: (0..3)
                .map(|r| Region {
                    name: format!("Region_{}_{}", i, r),
                    cities: (0..2)
                        .map(|c| City {
                            name: format!("City_{}_{}_{}", i, r, c),
                            population: 100_000 + c * 50_000,
                            area_km2: 50.0 + (c as f64) * 25.5,
                            districts: vec![District {
                                name: format!("Dist_{}", c),
                                population: 50_000 + c * 10_000,
                                streets: vec![Street {
                                    name: format!("St_{}", c),
                                    length_km: 1.0 + c as f64 * 0.5,
                                    buildings: (0..2)
                                        .map(|b| Building {
                                            name: format!("Bldg_{}_{}", c, b),
                                            floors: 5 + b * 3,
                                            residential: b % 2 == 0,
                                            height_m: 15.0 + b as f64 * 10.5,
                                        })
                                        .collect(),
                                }],
                            }],
                        })
                        .collect(),
                })
                .collect(),
        })
        .collect();

    // Serialize each country individually and measure total
    let mut total_asun_bytes = 0usize;
    let mut total_json_bytes = 0usize;
    let mut total_bin_bytes = 0usize;
    for c in &countries {
        let s = encode(c).unwrap();
        let j = serde_json::to_string(c).unwrap();
        let b = encode_binary(c).unwrap();
        // Verify text roundtrip
        let c2: Country = decode(&s).unwrap();
        assert_eq!(c, &c2);
        // Verify binary roundtrip
        let c3: Country = decode_binary(&b).unwrap();
        assert_eq!(c, &c3);
        total_asun_bytes += s.len();
        total_json_bytes += j.len();
        total_bin_bytes += b.len();
    }
    println!("   100 countries with 5-level nesting:");
    println!(
        "   Total ASUN text: {} bytes ({:.1} KB)",
        total_asun_bytes,
        total_asun_bytes as f64 / 1024.0
    );
    println!(
        "   Total ASUN bin:  {} bytes ({:.1} KB)",
        total_bin_bytes,
        total_bin_bytes as f64 / 1024.0
    );
    println!(
        "   Total JSON:      {} bytes ({:.1} KB)",
        total_json_bytes,
        total_json_bytes as f64 / 1024.0
    );
    println!(
        "   TEXT vs JSON: {:.0}% smaller | BIN vs JSON: {:.0}% smaller",
        (1.0 - total_asun_bytes as f64 / total_json_bytes as f64) * 100.0,
        (1.0 - total_bin_bytes as f64 / total_json_bytes as f64) * 100.0
    );
    println!("   ✓ all 100 countries roundtrip OK (text + bin)");

    // -----------------------------------------------------------------------
    // 14. Deserialize from ASUN with deeply nested schema type hints
    // -----------------------------------------------------------------------
    println!("\n14. Deserialize with nested schema type hints:");
    let input = "{name@str,code@str,population@int,gdp_trillion@float,regions@[{name@str,cities@[{name@str,population@int,area_km2@float,districts@[{name@str,population@int,streets@[{name@str,length_km@float,buildings@[{name@str,floors@int,residential@bool,height_m@float}]}]}]}]}]}:(TestLand,TL,1000000,0.5,[(TestRegion,[(TestCity,500000,100.0,[(Central,250000,[(Main St,2.5,[(HQ,10,false,45.0)])])])])])";
    let c: Country = decode(input).unwrap();
    assert_eq!(c.name, "TestLand");
    assert_eq!(
        c.regions[0].cities[0].districts[0].streets[0].buildings[0].name,
        "HQ"
    );
    println!("   ✓ deep schema type-hint parse OK");
    println!(
        "   Building at depth 6: {:?}",
        c.regions[0].cities[0].districts[0].streets[0].buildings[0]
    );

    // -----------------------------------------------------------------------
    // 15. Typed serialization (encode_typed)
    // -----------------------------------------------------------------------
    println!("\n15. Typed serialization (encode_typed):");

    // Simple struct
    let user_typed = encode_typed(&Employee {
        id: 1,
        name: "Alice".into(),
        dept: Department {
            title: "Engineering".into(),
        },
        skills: vec!["Rust".into(), "Go".into()],
        active: true,
    })
    .unwrap();
    println!("   nested struct: {}", user_typed);
    let emp_back: Employee = decode(&user_typed).unwrap();
    assert_eq!(emp_back.name, "Alice");
    println!("   ✓ typed nested struct roundtrip OK");

    // All-types struct
    let all_typed = encode_typed(&all).unwrap();
    println!(
        "   all-types ({} bytes): {}...",
        all_typed.len(),
        &all_typed[..80.min(all_typed.len())]
    );
    let all_back: AllTypes = decode(&all_typed).unwrap();
    assert_eq!(all, all_back);
    println!("   ✓ typed all-types roundtrip OK");

    // Config struct
    let config_typed = encode_typed(&config).unwrap();
    println!(
        "   config ({} bytes): {}...",
        config_typed.len(),
        &config_typed[..100.min(config_typed.len())]
    );
    let config_back: ServiceConfig = decode(&config_typed).unwrap();
    assert_eq!(config, config_back);
    println!("   ✓ typed config roundtrip OK");

    // Compare typed vs untyped output
    let untyped = encode(&config).unwrap();
    println!(
        "   untyped schema: {} bytes | typed schema: {} bytes | overhead: {} bytes",
        untyped.len(),
        config_typed.len(),
        config_typed.len() - untyped.len()
    );

    // -----------------------------------------------------------------------
    // 16. Edge cases — empty collections, special chars
    // -----------------------------------------------------------------------
    println!("\n16. Edge cases:");

    // Empty vec
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct WithVec {
        items: Vec<i64>,
    }
    let wv = WithVec { items: vec![] };
    let s = encode(&wv).unwrap();
    println!("   empty vec: {}", s);
    let wv2: WithVec = decode(&s).unwrap();
    assert_eq!(wv, wv2);

    // String with all special chars
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct Special {
        val: String,
    }
    let sp = Special {
        val: "tabs\there, newlines\nhere, quotes\"and\\backslash".into(),
    };
    let s = encode(&sp).unwrap();
    println!("   special chars: {}", s);
    let sp2: Special = decode(&s).unwrap();
    assert_eq!(sp, sp2);

    // Boolean edge
    let sp3 = Special { val: "true".into() };
    let s = encode(&sp3).unwrap();
    println!("   bool-like string: {}", s);
    let sp4: Special = decode(&s).unwrap();
    assert_eq!(sp3, sp4);

    // Number-like string
    let sp5 = Special {
        val: "12345".into(),
    };
    let s = encode(&sp5).unwrap();
    println!("   number-like string: {}", s);
    let sp6: Special = decode(&s).unwrap();
    assert_eq!(sp5, sp6);

    println!("   ✓ all edge cases OK");

    // -----------------------------------------------------------------------
    // 17. Array of arrays of arrays (3-level)
    // -----------------------------------------------------------------------
    println!("\n17. Triple-nested arrays:");
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct Matrix3D {
        data: Vec<Vec<Vec<i64>>>,
    }
    let m3 = Matrix3D {
        data: vec![vec![vec![1, 2], vec![3, 4]], vec![vec![5, 6, 7], vec![8]]],
    };
    let s = encode(&m3).unwrap();
    println!("   {}", s);
    let m3b: Matrix3D = decode(&s).unwrap();
    assert_eq!(m3, m3b);
    println!("   ✓ triple-nested array roundtrip OK");

    // -----------------------------------------------------------------------
    // 18. Comments in ASUN
    // -----------------------------------------------------------------------
    println!("\n18. Comments:");
    let _input = "/* Top-level comment */
[{id,name,active}]:
  /* row 1 */ (1, Alice, true)";
    let emp: Employee =
        decode("{id,name,dept@{title},skills@[],active}:/* inline */ (1,Alice,(HR),[rust],true)")
            .unwrap();
    println!("   with inline comment: {:?}", emp);
    println!("   ✓ comment parsing OK");

    // -----------------------------------------------------------------------
    // Summary
    // -----------------------------------------------------------------------
    println!("\n=== All {} complex examples passed! ===", 18);
}