xmachines-codec-avro 4.0.0

Avro events and snapshots for xmachines-rs machines, in the Schema Registry wire format (an xmachines-codec codec)
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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
//! The schemas generated from a bundle, their registry ids, and the direct
//! binary codec that reads and writes their records.

use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};

use apache_avro::types::Value as Avro;
use apache_avro::Schema;
use serde_json::{json, Map, Number, Value};
use xmachines_codec::varint;

use crate::registry::Registry;
use crate::Error;

/// The Schema Registry wire format's first byte.
pub const MAGIC: u8 = 0;

/// How a JSON value maps to Avro.
#[derive(Debug, Clone)]
enum Shape {
    Bool,
    Long,
    Double,
    Str,
    /// Any JSON, as its text.
    Json,
    Enum(Arc<EnumShape>),
    Array(Box<Shape>),
    Map(Box<Shape>),
    /// An object with any keys, in order: an array of `{ key, value }`.
    Entries(Arc<RecordShape>),
    Record(Arc<RecordShape>),
    /// `["null", T]`, where null is an absent key.
    Optional(Box<Shape>),
    /// `["null", T]`, where null is JSON null.
    Nullable(Box<Shape>),
    /// Records told apart by the string `key` (not a field of theirs).
    Union(Arc<UnionShape>),
}

#[derive(Debug)]
struct EnumShape {
    name: String,
    symbols: Vec<String>,
}

#[derive(Debug)]
struct RecordShape {
    name: String,
    fields: Vec<FieldShape>,
    /// Keep what the fields do not describe in an `extra` list (snapshots).
    extra: bool,
}

#[derive(Debug)]
struct FieldShape {
    /// The JSON key.
    key: String,
    /// The Avro field name.
    name: String,
    shape: Shape,
}

#[derive(Debug)]
struct UnionShape {
    key: String,
    branches: Vec<(String, Arc<RecordShape>)>,
}

/// A valid Avro name for `s` (letters, digits and `_`, not starting with a
/// digit).
fn avro_name(s: &str) -> String {
    let mut out: String = s.chars().map(|c| if c.is_ascii_alphanumeric() || c == '_' { c } else { '_' }).collect();
    if out.is_empty() || out.starts_with(|c: char| c.is_ascii_digit()) {
        out.insert(0, '_');
    }
    out
}

fn is_avro_name(s: &str) -> bool {
    avro_name(s) == s
}

fn pascal(s: &str) -> String {
    let mut out = String::new();
    let mut upper = true;
    for c in s.chars() {
        if c.is_ascii_alphanumeric() {
            out.push(if upper { c.to_ascii_uppercase() } else { c });
            upper = false;
        } else {
            upper = true;
        }
    }
    avro_name(&out)
}

/// The Avro schema (JSON) of a shape; named types are defined once.
fn schema_json(shape: &Shape, defined: &mut HashSet<String>) -> Value {
    match shape {
        Shape::Bool => json!("boolean"),
        Shape::Long => json!("long"),
        Shape::Double => json!("double"),
        Shape::Str => json!("string"),
        Shape::Json => json!({ "type": "string", "xstate.json": true }),
        Shape::Enum(e) => {
            if !defined.insert(e.name.clone()) {
                return json!(e.name);
            }
            json!({ "type": "enum", "name": e.name, "symbols": e.symbols })
        }
        Shape::Array(items) => json!({ "type": "array", "items": schema_json(items, defined) }),
        Shape::Map(values) => json!({ "type": "map", "values": schema_json(values, defined) }),
        Shape::Entries(entry) => json!({ "type": "array", "items": record_json(entry, defined) }),
        Shape::Record(r) => record_json(r, defined),
        Shape::Optional(inner) | Shape::Nullable(inner) => json!(["null", schema_json(inner, defined)]),
        Shape::Union(u) => Value::Array(u.branches.iter().map(|(_, r)| record_json(r, defined)).collect()),
    }
}

fn record_json(r: &RecordShape, defined: &mut HashSet<String>) -> Value {
    if !defined.insert(r.name.clone()) {
        return json!(r.name);
    }
    let mut fields: Vec<Value> = r
        .fields
        .iter()
        .map(|f| {
            let mut field = json!({ "name": f.name, "type": schema_json(&f.shape, defined) });
            if matches!(f.shape, Shape::Optional(_)) {
                field["default"] = Value::Null;
            }
            if f.name != f.key {
                field["json"] = json!(f.key);
            }
            field
        })
        .collect();
    if r.extra {
        let entry = format!("{}Extra", r.name);
        let extra_json = if defined.insert(entry.clone()) {
            json!({ "type": "record", "name": entry, "fields": [
                { "name": "key", "type": "string" },
                { "name": "value", "type": { "type": "string", "xstate.json": true } }
            ] })
        } else {
            json!(entry)
        };
        fields.push(json!({ "name": "extra", "type": { "type": "array", "items": extra_json }, "default": [] }));
    }
    json!({ "type": "record", "name": r.name, "fields": fields })
}

/// A JSON number from an `f64`, written as JavaScript writes it.
fn number(f: f64) -> Result<Value, String> {
    if f.fract() == 0.0 && f.abs() < 9_007_199_254_740_992.0 {
        return Ok(Value::from(f as i64));
    }
    Number::from_f64(f).map(Value::Number).ok_or_else(|| format!("{f} is not a JSON number"))
}

/// The `apache-avro` value of `v` (the reference the direct writer is
/// tested against).
#[cfg(test)]
fn to_avro(shape: &Shape, v: &Value) -> Result<Avro, String> {
    let mismatch = || format!("{v} does not fit {}", schema_json(shape, &mut HashSet::new()));
    Ok(match shape {
        Shape::Bool => Avro::Boolean(v.as_bool().ok_or_else(mismatch)?),
        Shape::Long => Avro::Long(match v.as_i64() {
            Some(i) => i,
            None => {
                v.as_f64().filter(|f| f.fract() == 0.0 && f.abs() < 9.2e18).map(|f| f as i64).ok_or_else(mismatch)?
            }
        }),
        Shape::Double => Avro::Double(v.as_f64().ok_or_else(mismatch)?),
        Shape::Str => Avro::String(v.as_str().ok_or_else(mismatch)?.to_owned()),
        Shape::Json => Avro::String(v.to_string()),
        Shape::Enum(e) => {
            let s = v.as_str().ok_or_else(mismatch)?;
            let i = e.symbols.iter().position(|x| x == s).ok_or_else(mismatch)?;
            Avro::Enum(i as u32, s.to_owned())
        }
        Shape::Array(items) => {
            Avro::Array(v.as_array().ok_or_else(mismatch)?.iter().map(|x| to_avro(items, x)).collect::<Result<_, _>>()?)
        }
        Shape::Map(values) => Avro::Map(
            v.as_object()
                .ok_or_else(mismatch)?
                .iter()
                .map(|(k, x)| Ok((k.clone(), to_avro(values, x)?)))
                .collect::<Result<_, String>>()?,
        ),
        Shape::Entries(entry) => {
            let value_shape = &entry.fields[1].shape;
            Avro::Array(
                v.as_object()
                    .ok_or_else(mismatch)?
                    .iter()
                    .map(|(k, x)| {
                        Ok(Avro::Record(vec![
                            ("key".into(), Avro::String(k.clone())),
                            ("value".into(), to_avro(value_shape, x)?),
                        ]))
                    })
                    .collect::<Result<_, String>>()?,
            )
        }
        Shape::Record(r) => record_to_avro(r, v.as_object().ok_or_else(mismatch)?, None)?,
        Shape::Optional(inner) | Shape::Nullable(inner) => match v {
            Value::Null => Avro::Union(0, Box::new(Avro::Null)),
            v => Avro::Union(1, Box::new(to_avro(inner, v)?)),
        },
        Shape::Union(u) => {
            let o = v.as_object().ok_or_else(mismatch)?;
            let tag =
                o.get(&u.key).and_then(Value::as_str).ok_or_else(|| format!("{v} has no string \"{}\"", u.key))?;
            let (i, (_, r)) = u
                .branches
                .iter()
                .enumerate()
                .find(|(_, (t, _))| t == tag)
                .ok_or_else(|| format!("\"{}\": \"{tag}\" is not in the schema", u.key))?;
            Avro::Union(i as u32, Box::new(record_to_avro(r, o, Some(&u.key))?))
        }
    })
}

/// A record from a JSON object (`skip`: a union's tag key). A record with
/// `extra` keeps what its fields cannot hold in the `extra` list.
#[cfg(test)]
fn record_to_avro(r: &RecordShape, o: &Map<String, Value>, skip: Option<&str>) -> Result<Avro, String> {
    let mut fields = Vec::with_capacity(r.fields.len() + 1);
    let mut extra = Vec::new();
    for f in &r.fields {
        let v = o.get(&f.key);
        let avro = match (&f.shape, v) {
            (Shape::Optional(_), None) => Avro::Union(0, Box::new(Avro::Null)),
            (Shape::Optional(inner), Some(v)) if r.extra => match to_avro(inner, v) {
                Ok(a) => Avro::Union(1, Box::new(a)),
                Err(_) => {
                    extra.push((f.key.clone(), v));
                    Avro::Union(0, Box::new(Avro::Null))
                }
            },
            (_, Some(v)) => to_avro(&f.shape, v).map_err(|e| format!("\"{}\": {e}", f.key))?,
            (_, None) => return Err(format!("missing \"{}\"", f.key)),
        };
        fields.push((f.name.clone(), avro));
    }
    for (k, v) in o {
        if Some(k.as_str()) == skip || r.fields.iter().any(|f| &f.key == k) {
            continue;
        }
        if !r.extra {
            return Err(format!("\"{k}\" is not in the schema of {}", r.name));
        }
        extra.push((k.clone(), v));
    }
    if r.extra {
        let items = extra
            .into_iter()
            .map(|(k, v)| {
                Avro::Record(vec![("key".into(), Avro::String(k)), ("value".into(), Avro::String(v.to_string()))])
            })
            .collect();
        fields.push(("extra".into(), Avro::Array(items)));
    }
    Ok(Avro::Record(fields))
}

fn from_avro(shape: &Shape, a: Avro) -> Result<Value, String> {
    let mismatch = |a: &Avro| format!("{a:?} does not fit {}", schema_json(shape, &mut HashSet::new()));
    Ok(match (shape, a) {
        (Shape::Bool, Avro::Boolean(b)) => Value::Bool(b),
        (Shape::Long, Avro::Long(i)) => Value::from(i),
        (Shape::Long, Avro::Int(i)) => Value::from(i),
        (Shape::Double, Avro::Double(f)) => number(f)?,
        (Shape::Double, Avro::Float(f)) => number(f64::from(f))?,
        (Shape::Double, Avro::Long(i)) => Value::from(i),
        (Shape::Double, Avro::Int(i)) => Value::from(i),
        (Shape::Str, Avro::String(s)) => Value::String(s),
        (Shape::Json, Avro::String(s)) => serde_json::from_str(&s).map_err(|e| format!("not JSON text: {e}"))?,
        (Shape::Enum(_), Avro::Enum(_, s)) => Value::String(s),
        (Shape::Array(items), Avro::Array(xs)) => {
            Value::Array(xs.into_iter().map(|x| from_avro(items, x)).collect::<Result<_, _>>()?)
        }
        (Shape::Map(values), Avro::Map(m)) => {
            // Avro maps are unordered: sort the keys for a stable result.
            let mut entries: Vec<(String, Avro)> = m.into_iter().collect();
            entries.sort_by(|a, b| a.0.cmp(&b.0));
            Value::Object(
                entries.into_iter().map(|(k, x)| Ok((k, from_avro(values, x)?))).collect::<Result<_, String>>()?,
            )
        }
        (Shape::Entries(entry), Avro::Array(xs)) => {
            let value_shape = &entry.fields[1].shape;
            let mut o = Map::with_capacity(xs.len());
            for x in xs {
                let Avro::Record(mut kv) = x else { return Err("an entry is not a record".into()) };
                let (Some((_, value)), Some((_, Avro::String(key)))) = (kv.pop(), kv.pop()) else {
                    return Err("an entry needs a key and a value".into());
                };
                o.insert(key, from_avro(value_shape, value)?);
            }
            Value::Object(o)
        }
        (Shape::Record(r), Avro::Record(fields)) => Value::Object(record_from_avro(r, fields)?),
        (Shape::Optional(inner) | Shape::Nullable(inner), Avro::Union(_, a)) => match *a {
            Avro::Null => Value::Null,
            a => from_avro(inner, a)?,
        },
        (Shape::Optional(_) | Shape::Nullable(_), Avro::Null) => Value::Null,
        (Shape::Union(u), Avro::Union(i, a)) => {
            let (tag, r) = u.branches.get(i as usize).ok_or("a union branch out of range")?;
            let Avro::Record(fields) = *a else { return Err("a union branch is not a record".into()) };
            let mut o = Map::with_capacity(fields.len() + 1);
            o.insert(u.key.clone(), Value::String(tag.clone()));
            o.extend(record_from_avro(r, fields)?);
            Value::Object(o)
        }
        (_, a) => return Err(mismatch(&a)),
    })
}

fn record_from_avro(r: &RecordShape, fields: Vec<(String, Avro)>) -> Result<Map<String, Value>, String> {
    let mut o = Map::with_capacity(fields.len());
    for (name, a) in fields {
        if r.extra && name == "extra" {
            let Avro::Array(items) = a else { return Err("\"extra\" is not an array".into()) };
            for item in items {
                let Avro::Record(mut kv) = item else { return Err("an extra entry is not a record".into()) };
                let (Some((_, Avro::String(value))), Some((_, Avro::String(key)))) = (kv.pop(), kv.pop()) else {
                    return Err("an extra entry needs a key and a value".into());
                };
                o.insert(key, serde_json::from_str(&value).map_err(|e| format!("extra \"{}\": {e}", value))?);
            }
            continue;
        }
        let Some(f) = r.fields.iter().find(|f| f.name == name) else { continue };
        match (&f.shape, a) {
            (Shape::Optional(_), Avro::Union(_, a)) if matches!(*a, Avro::Null) => {}
            (Shape::Optional(_), Avro::Null) => {}
            (shape, a) => {
                o.insert(f.key.clone(), from_avro(shape, a).map_err(|e| format!("\"{}\": {e}", f.key))?);
            }
        }
    }
    Ok(o)
}

// The binary encoding, written and read straight from the shapes, for
// records with the generated schemas' own ids (snapshots, outputs, and
// events from producers using the registered schema): no `Value` tree, no
// schema validation per record.

fn put_long(out: &mut Vec<u8>, v: i64) {
    varint::put_varint(out, varint::zigzag(v));
}

fn put_str(out: &mut Vec<u8>, s: &str) {
    put_long(out, s.len() as i64);
    out.extend_from_slice(s.as_bytes());
}

/// Writes `v` as `shape`. On an error, part of it may have been written:
/// the caller truncates.
fn write(shape: &Shape, v: &Value, out: &mut Vec<u8>) -> Result<(), String> {
    let mismatch = || format!("{v} does not fit {}", schema_json(shape, &mut HashSet::new()));
    match shape {
        Shape::Bool => out.push(u8::from(v.as_bool().ok_or_else(mismatch)?)),
        Shape::Long => put_long(
            out,
            match v.as_i64() {
                Some(i) => i,
                None => v
                    .as_f64()
                    .filter(|f| f.fract() == 0.0 && f.abs() < 9.2e18)
                    .map(|f| f as i64)
                    .ok_or_else(mismatch)?,
            },
        ),
        Shape::Double => out.extend_from_slice(&v.as_f64().ok_or_else(mismatch)?.to_le_bytes()),
        Shape::Str => put_str(out, v.as_str().ok_or_else(mismatch)?),
        Shape::Json => put_str(out, &v.to_string()),
        Shape::Enum(e) => {
            let s = v.as_str().ok_or_else(mismatch)?;
            put_long(out, e.symbols.iter().position(|x| x == s).ok_or_else(mismatch)? as i64);
        }
        Shape::Array(items) => {
            let a = v.as_array().ok_or_else(mismatch)?;
            if !a.is_empty() {
                put_long(out, a.len() as i64);
                for x in a {
                    write(items, x, out)?;
                }
            }
            put_long(out, 0);
        }
        Shape::Map(values) => {
            let o = v.as_object().ok_or_else(mismatch)?;
            if !o.is_empty() {
                put_long(out, o.len() as i64);
                for (k, x) in o {
                    put_str(out, k);
                    write(values, x, out)?;
                }
            }
            put_long(out, 0);
        }
        Shape::Entries(entry) => {
            let o = v.as_object().ok_or_else(mismatch)?;
            if !o.is_empty() {
                put_long(out, o.len() as i64);
                for (k, x) in o {
                    put_str(out, k);
                    write(&entry.fields[1].shape, x, out)?;
                }
            }
            put_long(out, 0);
        }
        Shape::Record(r) => write_record(r, v.as_object().ok_or_else(mismatch)?, None, out)?,
        Shape::Optional(inner) | Shape::Nullable(inner) => match v {
            Value::Null => put_long(out, 0),
            v => {
                put_long(out, 1);
                write(inner, v, out)?;
            }
        },
        Shape::Union(u) => {
            let o = v.as_object().ok_or_else(mismatch)?;
            let tag =
                o.get(&u.key).and_then(Value::as_str).ok_or_else(|| format!("{v} has no string \"{}\"", u.key))?;
            let (i, (_, r)) = u
                .branches
                .iter()
                .enumerate()
                .find(|(_, (t, _))| t == tag)
                .ok_or_else(|| format!("\"{}\": \"{tag}\" is not in the schema", u.key))?;
            put_long(out, i as i64);
            write_record(r, o, Some(&u.key), out)?;
        }
    }
    Ok(())
}

fn write_record(r: &RecordShape, o: &Map<String, Value>, skip: Option<&str>, out: &mut Vec<u8>) -> Result<(), String> {
    let mut extra: Vec<(&str, &Value)> = Vec::new();
    for f in &r.fields {
        match (&f.shape, o.get(&f.key)) {
            (Shape::Optional(_), None) => put_long(out, 0),
            (Shape::Optional(inner), Some(v)) if r.extra => {
                let mark = out.len();
                put_long(out, 1);
                if write(inner, v, out).is_err() {
                    out.truncate(mark);
                    put_long(out, 0);
                    extra.push((&f.key, v));
                }
            }
            (shape, Some(v)) => write(shape, v, out).map_err(|e| format!("\"{}\": {e}", f.key))?,
            (_, None) => return Err(format!("missing \"{}\"", f.key)),
        }
    }
    for (k, v) in o {
        if Some(k.as_str()) == skip || r.fields.iter().any(|f| &f.key == k) {
            continue;
        }
        if !r.extra {
            return Err(format!("\"{k}\" is not in the schema of {}", r.name));
        }
        extra.push((k, v));
    }
    if r.extra {
        if !extra.is_empty() {
            put_long(out, extra.len() as i64);
            for (k, v) in extra {
                put_str(out, k);
                put_str(out, &v.to_string());
            }
        }
        put_long(out, 0);
    }
    Ok(())
}

/// A datum being read.
struct Reader<'a>(&'a [u8]);

impl<'a> Reader<'a> {
    fn long(&mut self) -> Result<i64, String> {
        match varint::read_varint(&mut self.0) {
            Ok(v) => Ok(varint::unzigzag(v)),
            Err(_) if self.0.is_empty() => Err("truncated Avro datum".into()),
            Err(_) => Err("an Avro long is too long".into()),
        }
    }

    fn take(&mut self, n: usize) -> Result<&'a [u8], String> {
        if self.0.len() < n {
            return Err("truncated Avro datum".into());
        }
        let (head, rest) = self.0.split_at(n);
        self.0 = rest;
        Ok(head)
    }

    fn str(&mut self) -> Result<&'a str, String> {
        let n = usize::try_from(self.long()?).map_err(|_| "a negative string length")?;
        std::str::from_utf8(self.take(n)?).map_err(|_| "a string is not UTF-8".into())
    }

    /// Calls `item` for each item of an array or map (blocks of items).
    fn blocks(&mut self, mut item: impl FnMut(&mut Self) -> Result<(), String>) -> Result<(), String> {
        loop {
            let mut n = self.long()?;
            if n == 0 {
                return Ok(());
            }
            if n < 0 {
                n = -n;
                self.long()?; // the block's size in bytes
            }
            for _ in 0..n {
                item(self)?;
            }
        }
    }
}

fn read(shape: &Shape, r: &mut Reader<'_>) -> Result<Value, String> {
    Ok(match shape {
        Shape::Bool => Value::Bool(r.take(1)?[0] != 0),
        Shape::Long => Value::from(r.long()?),
        Shape::Double => number(f64::from_le_bytes(r.take(8)?.try_into().expect("8 bytes")))?,
        Shape::Str => Value::String(r.str()?.to_owned()),
        Shape::Json => serde_json::from_str(r.str()?).map_err(|e| format!("not JSON text: {e}"))?,
        Shape::Enum(e) => {
            let i = r.long()?;
            Value::String(e.symbols.get(i as usize).ok_or("an enum index out of range")?.clone())
        }
        Shape::Array(items) => {
            let mut a = Vec::new();
            r.blocks(|r| {
                a.push(read(items, r)?);
                Ok(())
            })?;
            Value::Array(a)
        }
        Shape::Map(values) => {
            let mut entries = Vec::new();
            r.blocks(|r| {
                let k = r.str()?.to_owned();
                entries.push((k, read(values, r)?));
                Ok(())
            })?;
            // Avro maps are unordered: sort the keys for a stable result.
            entries.sort_by(|a, b| a.0.cmp(&b.0));
            Value::Object(entries.into_iter().collect())
        }
        Shape::Entries(entry) => {
            let mut o = Map::new();
            r.blocks(|r| {
                let k = r.str()?.to_owned();
                o.insert(k, read(&entry.fields[1].shape, r)?);
                Ok(())
            })?;
            Value::Object(o)
        }
        Shape::Record(rec) => Value::Object(read_record(rec, r)?),
        Shape::Optional(inner) | Shape::Nullable(inner) => match r.long()? {
            0 => Value::Null,
            1 => read(inner, r)?,
            _ => return Err("a union index out of range".into()),
        },
        Shape::Union(u) => {
            let (tag, rec) = u.branches.get(r.long()? as usize).ok_or("a union index out of range")?;
            let mut o = Map::with_capacity(rec.fields.len() + 1);
            o.insert(u.key.clone(), Value::String(tag.clone()));
            o.extend(read_record(rec, r)?);
            Value::Object(o)
        }
    })
}

fn read_record(rec: &RecordShape, r: &mut Reader<'_>) -> Result<Map<String, Value>, String> {
    let mut o = Map::with_capacity(rec.fields.len());
    for f in &rec.fields {
        match &f.shape {
            Shape::Optional(inner) => match r.long()? {
                0 => {}
                1 => {
                    o.insert(f.key.clone(), read(inner, r).map_err(|e| format!("\"{}\": {e}", f.key))?);
                }
                _ => return Err("a union index out of range".into()),
            },
            shape => {
                o.insert(f.key.clone(), read(shape, r).map_err(|e| format!("\"{}\": {e}", f.key))?);
            }
        }
    }
    if rec.extra {
        r.blocks(|r| {
            let k = r.str()?.to_owned();
            let v = serde_json::from_str(r.str()?).map_err(|e| format!("extra \"{k}\": {e}"))?;
            o.insert(k, v);
            Ok(())
        })?;
    }
    Ok(o)
}

/// Builds shapes from JSON Schemas.
struct Builder<'a> {
    namespace: String,
    names: HashMap<String, usize>,
    defs: Option<&'a Map<String, Value>>,
}

impl<'a> Builder<'a> {
    fn unique(&mut self, name: &str) -> String {
        let n = self.names.entry(name.to_owned()).or_insert(0);
        *n += 1;
        let local = if *n == 1 { name.to_owned() } else { format!("{name}{n}") };
        format!("{}.{local}", self.namespace)
    }

    fn resolve(&self, s: &'a Value) -> &'a Value {
        let mut s = s;
        for _ in 0..16 {
            match s.get("$ref").and_then(Value::as_str).and_then(|r| r.strip_prefix("#/$defs/")) {
                Some(name) => match self.defs.and_then(|d| d.get(name)) {
                    Some(def) => s = def,
                    None => return s,
                },
                None => return s,
            }
        }
        s
    }

    /// The shape of `s`; `depth` bounds recursive references (deeper parts
    /// become JSON text).
    fn shape(&mut self, s: &'a Value, hint: &str, depth: usize) -> Shape {
        if depth > 8 {
            return Shape::Json;
        }
        let s = self.resolve(s);
        for key in ["anyOf", "oneOf"] {
            if let Some(options) = s.get(key).and_then(Value::as_array) {
                let non_null: Vec<&Value> = options
                    .iter()
                    .map(|o| self.resolve(o))
                    .filter(|o| o.get("type").and_then(Value::as_str) != Some("null"))
                    .collect();
                let nullable = non_null.len() < options.len();
                let inner = if non_null.len() == 1 {
                    self.shape(non_null[0], hint, depth + 1)
                } else {
                    self.union(&non_null, hint, depth).unwrap_or(Shape::Json)
                };
                return if nullable { nullable_of(inner) } else { inner };
            }
        }
        let (ty, nullable) = match s.get("type") {
            Some(Value::Array(types)) => {
                let non_null: Vec<&str> = types.iter().filter_map(Value::as_str).filter(|t| *t != "null").collect();
                match non_null.as_slice() {
                    [t] => (*t, non_null.len() < types.len()),
                    _ => return Shape::Json,
                }
            }
            Some(Value::String(t)) => (t.as_str(), false),
            _ => return Shape::Json,
        };
        let shape = match ty {
            "string" => match s.get("enum").and_then(Value::as_array) {
                Some(values) if !values.is_empty() && values.iter().all(|v| v.as_str().is_some_and(is_avro_name)) => {
                    let symbols: Vec<String> = values.iter().filter_map(|v| v.as_str().map(str::to_owned)).collect();
                    let name = self.unique(hint);
                    Shape::Enum(Arc::new(EnumShape { name, symbols }))
                }
                _ => Shape::Str,
            },
            "boolean" => Shape::Bool,
            "integer" => Shape::Long,
            "number" => Shape::Double,
            "array" => match s.get("items").filter(|i| i.is_object()) {
                Some(items) if s.get("prefixItems").is_none() => {
                    Shape::Array(Box::new(self.shape(items, &format!("{hint}Item"), depth + 1)))
                }
                _ => Shape::Json,
            },
            "object" => match s.get("properties").and_then(Value::as_object) {
                Some(p) if !p.is_empty() => Shape::Record(self.record(s, hint, depth, None)),
                _ => match s.get("additionalProperties").filter(|a| a.is_object()) {
                    Some(values) => Shape::Map(Box::new(self.shape(values, &format!("{hint}Value"), depth + 1))),
                    None => Shape::Json,
                },
            },
            _ => Shape::Json,
        };
        if nullable {
            nullable_of(shape)
        } else {
            shape
        }
    }

    /// A discriminated union of objects.
    fn union(&mut self, options: &[&'a Value], hint: &str, depth: usize) -> Option<Shape> {
        if options.len() < 2 || !options.iter().all(|o| o.get("type").and_then(Value::as_str) == Some("object")) {
            return None;
        }
        let first = options[0].get("properties").and_then(Value::as_object)?;
        let key = first
            .iter()
            .find(|(k, _)| {
                options.iter().all(|o| {
                    o.get("properties")
                        .and_then(|p| p.get(k.as_str()))
                        .and_then(|d| d.get("const"))
                        .is_some_and(Value::is_string)
                        && o.get("required")
                            .and_then(Value::as_array)
                            .is_some_and(|r| r.iter().any(|x| x == k.as_str()))
                })
            })
            .map(|(k, _)| k.clone())?;
        let branches = options
            .iter()
            .map(|o| {
                let tag = o["properties"][key.as_str()]["const"].as_str().unwrap_or_default().to_owned();
                let record = self.record(o, &format!("{hint}{}", pascal(&tag)), depth, Some(&key));
                (tag, record)
            })
            .collect();
        Some(Shape::Union(Arc::new(UnionShape { key, branches })))
    }

    fn record(&mut self, s: &'a Value, hint: &str, depth: usize, skip: Option<&str>) -> Arc<RecordShape> {
        let name = self.unique(hint);
        let required: Vec<&str> =
            s.get("required").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).collect();
        let mut fields: Vec<FieldShape> = Vec::new();
        for (key, prop) in s.get("properties").and_then(Value::as_object).into_iter().flatten() {
            if Some(key.as_str()) == skip {
                continue;
            }
            let shape = self.shape(prop, &format!("{hint}{}", pascal(key)), depth + 1);
            let shape = if required.contains(&key.as_str()) {
                shape
            } else {
                // Optional (and an optional nullable field is one option).
                match shape {
                    Shape::Nullable(inner) | Shape::Optional(inner) => Shape::Optional(inner),
                    shape => Shape::Optional(Box::new(shape)),
                }
            };
            let mut field_name = avro_name(key);
            while fields.iter().any(|f| f.name == field_name) {
                field_name.push('_');
            }
            fields.push(FieldShape { key: key.clone(), name: field_name, shape });
        }
        Arc::new(RecordShape { name, fields, extra: false })
    }
}

fn nullable_of(shape: Shape) -> Shape {
    match shape {
        Shape::Nullable(_) | Shape::Optional(_) | Shape::Json => shape,
        shape => Shape::Nullable(Box::new(shape)),
    }
}

/// Event records from `schemas.events` / `schemas.emitted`: a union keyed by
/// `type`.
fn events_shape(schemas: &Map<String, Value>, namespace: String) -> Result<Shape, String> {
    let mut b = Builder { namespace, names: HashMap::new(), defs: None };
    let mut branches = Vec::new();
    for (event_type, schema) in schemas {
        b.defs = schema.get("$defs").and_then(Value::as_object);
        let resolved = b.resolve(schema);
        if resolved.get("type").and_then(Value::as_str) != Some("object") {
            return Err(format!("the schema of \"{event_type}\" must be an object"));
        }
        branches.push((event_type.clone(), b.record(resolved, &pascal(event_type), 0, Some("type"))));
    }
    if branches.is_empty() {
        return Err("no event schemas".into());
    }
    Ok(Shape::Union(Arc::new(UnionShape { key: "type".into(), branches })))
}

/// The snapshot shape, with the root context typed by `context` when given.
fn snapshot_shape(context: Option<&Value>) -> Shape {
    let ns = "xmachines.snapshot.v1";
    let field = |key: &str, shape: Shape| FieldShape {
        key: key.into(),
        name: avro_name(key),
        shape: Shape::Optional(Box::new(shape)),
    };
    let record = |name: &str, fields: Vec<FieldShape>| {
        Arc::new(RecordShape { name: format!("{ns}.{name}"), fields, extra: true })
    };
    let entries = |name: &str, value: Shape| {
        Shape::Entries(Arc::new(RecordShape {
            name: format!("{ns}.{name}"),
            fields: vec![
                FieldShape { key: "key".into(), name: "key".into(), shape: Shape::Str },
                FieldShape { key: "value".into(), name: "value".into(), shape: value },
            ],
            extra: false,
        }))
    };
    let actor_ref = record("ActorRef", vec![field("@actor", Shape::Str), field("incarnation", Shape::Str)]);
    let context = match context {
        Some(schema) => {
            let mut b = Builder {
                namespace: format!("{ns}.context"),
                names: HashMap::new(),
                defs: schema.get("$defs").and_then(Value::as_object),
            };
            b.shape(schema, "Context", 0)
        }
        None => Shape::Json,
    };
    let logical_timer = record(
        "LogicalTimer",
        vec![
            field("occurrence", Shape::Long),
            field("id", Shape::Str),
            field("seq", Shape::Long),
            field("delay", Shape::Long),
            field("type", Shape::Str),
            field("event", Shape::Json),
            field("target", Shape::Str),
        ],
    );
    let machine = record(
        "ActorSnapshot",
        vec![
            field("status", Shape::Str),
            field("output", Shape::Json),
            field("error", Shape::Json),
            field("context", context),
            field("value", Shape::Json),
            field("children", entries("ActorRefEntry", Shape::Record(actor_ref.clone()))),
            field("timers", Shape::Array(Box::new(Shape::Record(logical_timer)))),
            field("historyValue", Shape::Json),
            field("stateInputs", Shape::Json),
            field("nextTimerId", Shape::Long),
            field("nextTimerOccurrence", Shape::Long),
            field("nodeIds", Shape::Array(Box::new(Shape::Str))),
        ],
    );
    let actor = record(
        "ActorEntry",
        vec![
            field("id", Shape::Str),
            field("address", Shape::Str),
            field("incarnation", Shape::Str),
            field("src", Shape::Str),
            field("logicVersion", Shape::Str),
            field("parent", Shape::Record(actor_ref.clone())),
            field("registryKey", Shape::Str),
            field("syncSnapshot", Shape::Bool),
            field("started", Shape::Bool),
            field("input", Shape::Json),
            field("snapshot", Shape::Record(machine)),
        ],
    );
    let system_timer = record(
        "SystemTimer",
        vec![
            field("source", Shape::Record(actor_ref.clone())),
            field("occurrence", Shape::Long),
            field("scheduledAt", Shape::Long),
            field("dueAt", Shape::Long),
            field("sequence", Shape::Long),
            field("event", Shape::Json),
        ],
    );
    let counters = record(
        "Counters",
        vec![
            field("actor", Shape::Long),
            field("timer", Shape::Long),
            field("sequence", Shape::Long),
            field("effect", Shape::Long),
        ],
    );
    Shape::Record(record(
        "SystemSnapshot",
        vec![
            field("root", Shape::Str),
            field("executionId", Shape::Str),
            field("now", Shape::Long),
            field("actors", entries("ActorEntryEntry", Shape::Record(actor))),
            field("registry", entries("RegistryEntry", Shape::Record(actor_ref))),
            field("messages", Shape::Json),
            field("timers", Shape::Array(Box::new(Shape::Record(system_timer)))),
            field("externalEffects", Shape::Json),
            field("counters", Shape::Record(counters)),
        ],
    ))
}

/// A generated schema: its shape, parsed Avro schema, JSON text, and id
/// once registered.
#[derive(Debug)]
pub struct Root {
    shape: Shape,
    schema: Schema,
    json: String,
    /// Each branch alone (for writers that use one record per event type),
    /// by full name.
    branches: Vec<(String, Schema)>,
    id: Option<u32>,
}

impl Root {
    fn new(shape: Shape) -> Result<Self, String> {
        let value = schema_json(&shape, &mut HashSet::new());
        let json = value.to_string();
        let schema = Schema::parse_str(&json).map_err(|e| format!("generated Avro schema: {e}"))?;
        let mut branches = Vec::new();
        if let Shape::Union(u) = &shape {
            for (_, r) in &u.branches {
                let alone = record_json(r, &mut HashSet::new()).to_string();
                branches.push((
                    r.name.clone(),
                    Schema::parse_str(&alone).map_err(|e| format!("generated Avro schema: {e}"))?,
                ));
            }
        }
        Ok(Self { shape, schema, json, branches, id: None })
    }

    /// The Avro schema (JSON text).
    pub fn schema(&self) -> &str {
        &self.json
    }

    /// Its registry id, once registered or looked up.
    pub fn id(&self) -> Option<u32> {
        self.id
    }
}

/// Avro support for one machine: generated schemas, their registry ids, and
/// the writer schemas of records read (by id).
pub struct AvroSchemas {
    registry: Arc<dyn Registry>,
    /// The input events' schema, when the bundle has `schemas.events`.
    pub events: Option<Root>,
    /// The emitted events' schema, when the bundle has `schemas.emitted`.
    pub emitted: Option<Root>,
    /// The snapshots' schema.
    pub snapshot: Root,
    writers: Mutex<HashMap<u32, Arc<Schema>>>,
}

impl std::fmt::Debug for AvroSchemas {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AvroSchemas").field("events", &self.events.as_ref().map(|r| r.id)).finish_non_exhaustive()
    }
}

/// Splits a record in the wire format into its schema id and datum.
fn split(record: &[u8]) -> Result<(u32, &[u8]), String> {
    match record {
        [MAGIC, a, b, c, d, rest @ ..] => Ok((u32::from_be_bytes([*a, *b, *c, *d]), rest)),
        _ => Err("not in the Schema Registry wire format (magic byte 0, schema id)".into()),
    }
}

#[cfg(test)]
fn frame(id: u32, datum: &[u8], out: &mut Vec<u8>) {
    out.push(MAGIC);
    out.extend_from_slice(&id.to_be_bytes());
    out.extend_from_slice(datum);
}

impl AvroSchemas {
    /// Schemas for a bundle (`{ "machine", "schemas" }`): its events,
    /// emitted events and context (each when declared).
    pub fn from_bundle(bundle: &Value, registry: Arc<dyn Registry>) -> Result<Self, Error> {
        Self::build(bundle, registry).map_err(Error::Schema)
    }

    fn build(bundle: &Value, registry: Arc<dyn Registry>) -> Result<Self, String> {
        let schemas = bundle.get("schemas");
        let id = bundle.get("machine").and_then(|m| m.get("id")).and_then(Value::as_str).unwrap_or("machine");
        let ns = |kind: &str| format!("xmachines.{kind}.{}", avro_name(&id.to_ascii_lowercase()));
        let events = match schemas.and_then(|s| s.get("events")).and_then(Value::as_object) {
            Some(e) if !e.is_empty() => {
                Some(Root::new(events_shape(e, ns("events")).map_err(|e| format!("schemas.events: {e}"))?)?)
            }
            _ => None,
        };
        let emitted = match schemas.and_then(|s| s.get("emitted")).and_then(Value::as_object) {
            Some(e) if !e.is_empty() => {
                Some(Root::new(events_shape(e, ns("emitted")).map_err(|e| format!("schemas.emitted: {e}"))?)?)
            }
            _ => None,
        };
        let snapshot = Root::new(snapshot_shape(schemas.and_then(|s| s.get("context"))))?;
        Ok(Self { registry, events, emitted, snapshot, writers: Mutex::new(HashMap::new()) })
    }

    /// Registers the generated schemas under these subjects (or, without
    /// `auto_register`, looks them up), recording their ids.
    pub fn register(&mut self, subjects: [Option<&str>; 3], auto_register: bool) -> Result<(), Error> {
        let registry = self.registry.clone();
        for (root, subject) in
            [self.events.as_mut(), self.emitted.as_mut(), Some(&mut self.snapshot)].into_iter().zip(subjects)
        {
            let (Some(root), Some(subject)) = (root, subject) else { continue };
            let id = if auto_register {
                registry.register(subject, &root.json)
            } else {
                registry.lookup(subject, &root.json)
            }
            .map_err(|e| Error::Registry(format!("subject {subject}: {e}")))?;
            self.writers.lock().unwrap_or_else(|e| e.into_inner()).insert(id, Arc::new(root.schema.clone()));
            root.id = Some(id);
        }
        Ok(())
    }

    /// The schema a record was written with.
    fn writer(&self, id: u32) -> Result<Arc<Schema>, String> {
        if let Some(s) = self.writers.lock().unwrap_or_else(|e| e.into_inner()).get(&id) {
            return Ok(s.clone());
        }
        let text = self.registry.schema(id).map_err(|e| e.to_string())?;
        let schema = Arc::new(Schema::parse_str(&text).map_err(|e| format!("schema {id}: {e}"))?);
        self.writers.lock().unwrap_or_else(|e| e.into_inner()).insert(id, schema.clone());
        Ok(schema)
    }

    fn decode(&self, root: &Root, record: &[u8]) -> Result<Value, String> {
        let (id, mut datum) = split(record)?;
        if Some(id) == root.id {
            let mut r = Reader(datum);
            let value = read(&root.shape, &mut r).map_err(|e| format!("Avro datum (schema {id}): {e}"))?;
            if !r.0.is_empty() {
                return Err(format!("Avro datum (schema {id}): {} bytes left over", r.0.len()));
            }
            return Ok(value);
        }
        let writer = self.writer(id)?;
        let value = if let (Schema::Record(w), Shape::Union(_)) = (&*writer, &root.shape) {
            // One record per event type: resolve against that branch.
            let name = w.name.fullname(None);
            let index = root
                .branches
                .iter()
                .position(|(n, _)| *n == name)
                .ok_or_else(|| format!("record {name} is not one of this machine's events"))?;
            apache_avro::from_avro_datum(&writer, &mut datum, Some(&root.branches[index].1))
                .map(|v| Avro::Union(index as u32, Box::new(v)))
        } else {
            apache_avro::from_avro_datum(&writer, &mut datum, Some(&root.schema))
        }
        .map_err(|e| format!("Avro datum (schema {id}): {e}"))?;
        from_avro(&root.shape, value)
    }

    fn encode(root: &Root, value: &Value, out: &mut Vec<u8>) -> Result<(), String> {
        let id = root.id.ok_or("the schema is not registered")?;
        let start = out.len();
        out.push(MAGIC);
        out.extend_from_slice(&id.to_be_bytes());
        write(&root.shape, value, out).inspect_err(|_| out.truncate(start))
    }

    /// An input event (`{ "type", ... }`) from a record.
    pub fn decode_event(&self, record: &[u8]) -> Result<Value, Error> {
        let root = self.events.as_ref().ok_or_else(|| Error::Decode("Avro events need schemas.events".into()))?;
        self.decode(root, record).map_err(Error::Decode)
    }

    /// An input event as a record (for producers and tests).
    pub fn encode_event(&self, event: &Value) -> Result<Vec<u8>, Error> {
        let mut out = Vec::new();
        self.encode_event_into(event, &mut out)?;
        Ok(out)
    }

    /// [`AvroSchemas::encode_event`], appending to `out`.
    pub fn encode_event_into(&self, event: &Value, out: &mut Vec<u8>) -> Result<(), Error> {
        let root = self.events.as_ref().ok_or_else(|| Error::Encode("Avro events need schemas.events".into()))?;
        Self::encode(root, event, out).map_err(Error::Encode)
    }

    /// An emitted event as an output record.
    pub fn encode_emitted(&self, event: &Value) -> Result<Vec<u8>, Error> {
        let mut out = Vec::new();
        self.encode_emitted_into(event, &mut out)?;
        Ok(out)
    }

    /// [`AvroSchemas::encode_emitted`], appending to `out`.
    pub fn encode_emitted_into(&self, event: &Value, out: &mut Vec<u8>) -> Result<(), Error> {
        let root = self.emitted.as_ref().ok_or_else(|| Error::Encode("Avro outputs need schemas.emitted".into()))?;
        Self::encode(root, event, out).map_err(Error::Encode)
    }

    /// An output record back as its event (for consumers and tests).
    pub fn decode_emitted(&self, record: &[u8]) -> Result<Value, Error> {
        let root = self.emitted.as_ref().ok_or_else(|| Error::Decode("Avro outputs need schemas.emitted".into()))?;
        self.decode(root, record).map_err(Error::Decode)
    }

    /// Appends a snapshot (XState's `SystemSnapshot` JSON) as a record.
    pub fn encode_snapshot(&self, snapshot: &Value, out: &mut Vec<u8>) -> Result<(), Error> {
        Self::encode(&self.snapshot, snapshot, out).map_err(Error::Encode)
    }

    /// A snapshot (XState's `SystemSnapshot` JSON) from a record.
    pub fn decode_snapshot(&self, record: &[u8]) -> Result<Value, Error> {
        self.decode(&self.snapshot, record).map_err(Error::Decode)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::registry::MemoryRegistry;

    fn checkout() -> Value {
        let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../../examples/checkout/machines/checkout.machine.json");
        serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()
    }

    fn schemas() -> AvroSchemas {
        let mut s = AvroSchemas::from_bundle(&checkout(), Arc::new(MemoryRegistry::default())).unwrap();
        s.register([Some("in-value"), Some("out-value"), Some("changelog-value")], true).unwrap();
        s
    }

    #[test]
    fn events_round_trip() {
        let s = schemas();
        for event in [
            json!({ "type": "cart.add", "line": { "sku": "A-1", "quantity": 3 } }),
            json!({ "type": "checkout", "total": 120.5 }),
            json!({ "type": "payment.method", "method": { "kind": "card", "last4": "4242" } }),
            json!({ "type": "payment.method", "method": { "kind": "invoice" } }),
            json!({ "type": "cancel" }),
        ] {
            let record = s.encode_event(&event).unwrap();
            assert_eq!(record[0], MAGIC);
            assert_eq!(s.decode_event(&record).unwrap(), event);
        }
        assert!(s.encode_event(&json!({ "type": "nope" })).unwrap_err().to_string().contains("not in the schema"));
        assert!(s.encode_event(&json!({ "type": "checkout" })).unwrap_err().to_string().contains("missing \"total\""));
        assert!(s
            .encode_event(&json!({ "type": "checkout", "total": 1, "x": 2 }))
            .unwrap_err()
            .to_string()
            .contains("\"x\""));
    }

    #[test]
    fn writers_with_other_schemas_are_resolved() {
        let s = schemas();
        // A producer's own schema for one event type, an earlier version
        // without the optional field, registered under its own subject.
        let ns = s.events.as_ref().unwrap().branches[0].0.rsplit_once('.').unwrap().0.to_owned();
        let writer = json!({ "type": "record", "name": format!("{ns}.Checkout"), "fields": [{ "name": "total", "type": "float" }] });
        let id = s.registry.register("checkout-value", &writer.to_string()).unwrap();
        let schema = Schema::parse_str(&writer.to_string()).unwrap();
        let datum =
            apache_avro::to_avro_datum(&schema, Avro::Record(vec![("total".into(), Avro::Float(2.5))])).unwrap();
        let mut record = Vec::new();
        frame(id, &datum, &mut record);
        assert_eq!(s.decode_event(&record).unwrap(), json!({ "type": "checkout", "total": 2.5 }));
    }

    #[test]
    fn snapshots_round_trip_with_typed_context() {
        let s = schemas();
        let snapshot = json!({
            "root": "checkout", "now": 5,
            "actors": { "checkout": {
                "id": "checkout", "src": "root", "incarnation": "0", "started": true, "logicVersion": null,
                "snapshot": { "status": "active", "context": s_context(), "value": "cart", "nodeIds": ["checkout", "checkout.cart"],
                              "nextTimerId": 0, "somethingNew": [1, 2] } } },
            "timers": [], "counters": { "actor": 1, "timer": 0, "sequence": 3, "effect": 0 }
        });
        let mut record = Vec::new();
        s.encode_snapshot(&snapshot, &mut record).unwrap();
        assert_eq!(s.decode_snapshot(&record).unwrap(), snapshot);
    }

    /// The direct writer writes exactly what `apache-avro` writes for the
    /// same value, and `apache-avro` reads it back: over every recorded
    /// snapshot and the checkout example's events.
    #[test]
    fn the_direct_codec_matches_apache_avro() {
        let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures");
        let mut snapshots = Vec::new();
        let mut add = |records: &Value| {
            for r in records.as_array().into_iter().flatten() {
                if let Some(s) = r.get("snapshot").filter(|s| s.is_object()) {
                    snapshots.push(s.clone());
                }
            }
        };
        for entry in std::fs::read_dir(root.join("system")).unwrap() {
            let path = entry.unwrap().path();
            if path.to_string_lossy().ends_with(".expected.json") {
                add(&serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap());
            }
        }
        let corpus: Value =
            serde_json::from_str(&std::fs::read_to_string(root.join("fuzz/system-corpus.json")).unwrap()).unwrap();
        for case in corpus.as_array().unwrap() {
            add(&case["expected"]);
        }
        let s = schemas();
        let events = [
            json!({ "type": "cart.add", "line": { "sku": "A-1", "quantity": 3 } }),
            json!({ "type": "payment.method", "method": { "kind": "invoice" } }),
            json!({ "type": "checkout", "total": 120.5 }),
        ];
        let cases =
            snapshots.iter().map(|v| (&s.snapshot, v)).chain(events.iter().map(|v| (s.events.as_ref().unwrap(), v)));
        let mut n = 0;
        for (root, v) in cases {
            let mut direct = Vec::new();
            write(&root.shape, v, &mut direct).unwrap();
            let reference = apache_avro::to_avro_datum(&root.schema, to_avro(&root.shape, v).unwrap()).unwrap();
            assert_eq!(direct, reference, "{v}");
            let back = apache_avro::from_avro_datum(&root.schema, &mut &direct[..], None).unwrap();
            assert_eq!(&from_avro(&root.shape, back).unwrap(), v);
            n += 1;
        }
        assert!(n > 400, "{n} cases");
    }

    fn s_context() -> Value {
        json!({ "status": "open", "lines": [{ "sku": "A-1", "quantity": 2 }], "coupon": "SPRING" })
    }
}