animsmith-core 0.6.0

Engine-agnostic data model, sampling, measurements, and checks for the animsmith animation-clip linter
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
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
//! Strict, format-neutral contact-fragment V1 values.
//!
//! This module owns the interchange reader and canonical JSON seam. It does
//! not infer contacts, load assets, perform transforms, or publish files.

use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet};
use std::io::{self, Write};

use serde::de::{DeserializeSeed, Error as _, IgnoredAny, MapAccess, SeqAccess, Visitor};
use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value;

use crate::{DependencyClosureIdentityV1, InputIdentity};

/// Immutable schema identity for contact-fragment V1.
pub const CONTACT_FRAGMENT_V1_ID: &str = "urn:animsmith:schema:contact-fragment:1";
/// Immutable schema version for contact-fragment V1.
pub const CONTACT_FRAGMENT_V1_SCHEMA_VERSION: u32 = 1;
/// Maximum accepted UTF-8 JSON source bytes.
pub const CONTACT_FRAGMENT_V1_MAX_SOURCE_BYTES: usize = 8 * 1024 * 1024;
/// Maximum canonical RFC 8785 JSON bytes.
pub const CONTACT_FRAGMENT_V1_MAX_CANONICAL_BYTES: usize = 8 * 1024 * 1024;
/// Maximum core events per fragment.
pub const CONTACT_FRAGMENT_V1_MAX_EVENTS: usize = 4_096;
/// Maximum strict extension envelopes per fragment.
pub const CONTACT_FRAGMENT_V1_MAX_EXTENSIONS: usize = 256;
/// Maximum UTF-8 bytes in one ordinary authored string or object key.
pub const CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES: usize = 4_096;
/// Maximum UTF-8 bytes in a V1 identifier.
pub const CONTACT_FRAGMENT_V1_MAX_IDENTIFIER_BYTES: usize = 255;
/// Maximum full-envelope object/array depth, including the root object.
pub const CONTACT_FRAGMENT_V1_MAX_DEPTH: usize = 32;
/// Maximum RFC 8785 bytes in one opaque extension payload.
pub const CONTACT_FRAGMENT_V1_MAX_EXTENSION_PAYLOAD_BYTES: usize = 256 * 1024;
/// Maximum object/array depth within one extension payload.
pub const CONTACT_FRAGMENT_V1_MAX_EXTENSION_PAYLOAD_DEPTH: usize = 16;
/// Largest exactly representable integer in the RFC 8785 / IEEE-754 JSON seam.
///
/// Contact-fragment V1 rejects integral JSON values outside this range before
/// canonicalization. This is deliberately a contact-fragment contract, not a
/// general replacement for the crate's identity authority.
pub const CONTACT_FRAGMENT_V1_MAX_SAFE_INTEGER: u64 = 9_007_199_254_740_991;
// A valid opaque payload with this many scalar members necessarily exceeds its
// JCS-byte cap; this bounds generic nested collections before retention.
const MAX_OPAQUE_MEMBERS: usize = CONTACT_FRAGMENT_V1_MAX_EXTENSION_PAYLOAD_BYTES;

/// Reader or contract violation for contact-fragment V1.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ContactFragmentError {
    /// JSON source exceeded the immutable V1 input cap.
    #[error("contact fragment source has {bytes} bytes, exceeding V1 limit {limit}")]
    SourceTooLarge {
        /// Observed source byte count.
        bytes: usize,
        /// Frozen V1 source byte limit.
        limit: usize,
    },
    /// The source was not valid UTF-8 JSON or used duplicate object members.
    #[error("invalid contact fragment JSON: {message}")]
    InvalidJson {
        /// Decoder detail, not a stable machine token.
        message: String,
    },
    /// A required V1 field was absent, malformed, or an object contained an unknown field.
    #[error("invalid contact fragment {field}: {message}")]
    InvalidField {
        /// Stable V1 field path.
        field: &'static str,
        /// Decoder detail, not a stable machine token.
        message: String,
    },
    /// A frozen V1 row or byte bound was exceeded.
    #[error("contact fragment {field} has {found}, exceeding V1 limit {limit}")]
    LimitExceeded {
        /// Stable V1 bounded field.
        field: &'static str,
        /// Observed count or byte length.
        found: usize,
        /// Frozen V1 limit.
        limit: usize,
    },
    /// RFC 8785 canonical output could not be represented within the V1 cap.
    #[error("contact fragment canonical JSON exceeds V1 limit {limit}")]
    CanonicalTooLarge {
        /// Frozen V1 canonical byte limit.
        limit: usize,
    },
}

/// The closed V1 semantic role vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ContactRoleV1 {
    /// Left foot.
    LeftFoot,
    /// Right foot.
    RightFoot,
    /// Left hand.
    LeftHand,
    /// Right hand.
    RightHand,
    /// Left toe.
    LeftToe,
    /// Right toe.
    RightToe,
    /// Left knee.
    LeftKnee,
    /// Right knee.
    RightKnee,
    /// Left elbow.
    LeftElbow,
    /// Right elbow.
    RightElbow,
    /// Root.
    Root,
    /// Prop.
    Prop,
    /// Body.
    Body,
}

/// The closed V1 event-phase vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ContactPhaseV1 {
    /// Start-like phase.
    Begin,
    /// End-like phase.
    End,
    /// One instantaneous marker.
    Marker,
}

/// Exact V1 producer identity.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ContactProducerV1 {
    tool: String,
    version: String,
}

impl ContactProducerV1 {
    /// Construct a bounded producer identity.
    pub fn new(
        tool: impl Into<String>,
        version: impl Into<String>,
    ) -> Result<Self, ContactFragmentError> {
        let value = Self {
            tool: tool.into(),
            version: version.into(),
        };
        identifier(&value.tool, "producer.tool")?;
        identifier(&value.version, "producer.version")?;
        Ok(value)
    }

    /// Producer tool identifier.
    pub fn tool(&self) -> &str {
        &self.tool
    }

    /// Producer version identifier.
    pub fn version(&self) -> &str {
        &self.version
    }
}

/// Exact selected clip witness.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "scope", rename_all = "snake_case")]
pub enum ContactClipReferenceV1 {
    /// A uniquely named clip in one loaded document.
    Document {
        /// Exact embedded clip name.
        clip_name: String,
    },
    /// A collection manifest witness and its exact source take.
    Collection {
        /// Logical clip identifier.
        logical_id: String,
        /// Collection source key.
        source: String,
        /// Exact source-local take index.
        take_index: u32,
        /// Exact source-local take name.
        take_name: String,
    },
}

impl ContactClipReferenceV1 {
    /// Construct a document-scoped witness.
    pub fn document(clip_name: impl Into<String>) -> Result<Self, ContactFragmentError> {
        let clip_name = clip_name.into();
        text(&clip_name, "clip.clip_name")?;
        Ok(Self::Document { clip_name })
    }

    /// Construct a collection-scoped witness.
    pub fn collection(
        logical_id: impl Into<String>,
        source: impl Into<String>,
        take_index: u32,
        take_name: impl Into<String>,
    ) -> Result<Self, ContactFragmentError> {
        let value = Self::Collection {
            logical_id: logical_id.into(),
            source: source.into(),
            take_index,
            take_name: take_name.into(),
        };
        value.validate()?;
        Ok(value)
    }

    fn validate(&self) -> Result<(), ContactFragmentError> {
        match self {
            Self::Document { clip_name } => text(clip_name, "clip.clip_name"),
            Self::Collection {
                logical_id,
                source,
                take_name,
                ..
            } => {
                identifier(logical_id, "clip.logical_id")?;
                identifier(source, "clip.source")?;
                text(take_name, "clip.take_name")
            }
        }
    }
}

/// Inclusive normalized time window.
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub struct ContactEventWindowV1 {
    start: f64,
    end: f64,
}

impl ContactEventWindowV1 {
    /// Construct a finite normalized window with `start <= end`.
    pub fn new(start: f64, end: f64) -> Result<Self, ContactFragmentError> {
        normalized_time(start, "event.window.start")?;
        normalized_time(end, "event.window.end")?;
        if start > end {
            return invalid("event.window", "start must not exceed end");
        }
        Ok(Self {
            start: canonical_number(start),
            end: canonical_number(end),
        })
    }
    /// Window start.
    pub const fn start(self) -> f64 {
        self.start
    }
    /// Window end.
    pub const fn end(self) -> f64 {
        self.end
    }
}

/// The exactly-one point/window event shape.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ContactEventKindV1 {
    /// One normalized instant.
    Point(f64),
    /// One normalized interval.
    Window(ContactEventWindowV1),
}

/// One stable event fact.
#[derive(Debug, Clone, PartialEq)]
pub struct ContactEventV1 {
    event_id: String,
    role: ContactRoleV1,
    phase: ContactPhaseV1,
    kind: ContactEventKindV1,
    confidence: Option<f64>,
}

impl ContactEventV1 {
    /// Construct a point event.
    pub fn point(
        event_id: impl Into<String>,
        role: ContactRoleV1,
        phase: ContactPhaseV1,
        time: f64,
        confidence: Option<f64>,
    ) -> Result<Self, ContactFragmentError> {
        Self::new(
            event_id.into(),
            role,
            phase,
            ContactEventKindV1::Point(time),
            confidence,
        )
    }
    /// Construct a window event.
    pub fn window(
        event_id: impl Into<String>,
        role: ContactRoleV1,
        phase: ContactPhaseV1,
        window: ContactEventWindowV1,
        confidence: Option<f64>,
    ) -> Result<Self, ContactFragmentError> {
        Self::new(
            event_id.into(),
            role,
            phase,
            ContactEventKindV1::Window(window),
            confidence,
        )
    }
    fn new(
        event_id: String,
        role: ContactRoleV1,
        phase: ContactPhaseV1,
        kind: ContactEventKindV1,
        confidence: Option<f64>,
    ) -> Result<Self, ContactFragmentError> {
        identifier(&event_id, "event.event_id")?;
        match kind {
            ContactEventKindV1::Point(time) => normalized_time(time, "event.time")?,
            ContactEventKindV1::Window(window) => {
                let _ = ContactEventWindowV1::new(window.start, window.end)?;
            }
        }
        if let Some(confidence) = confidence {
            finite_range(confidence, 0.0, 1.0, "event.confidence")?;
        }
        let kind = match kind {
            ContactEventKindV1::Point(time) => ContactEventKindV1::Point(canonical_number(time)),
            ContactEventKindV1::Window(window) => ContactEventKindV1::Window(window),
        };
        Ok(Self {
            event_id,
            role,
            phase,
            kind,
            confidence: confidence.map(canonical_number),
        })
    }
    /// Opaque stable event identifier.
    pub fn event_id(&self) -> &str {
        &self.event_id
    }
    /// Event role.
    pub const fn role(&self) -> ContactRoleV1 {
        self.role
    }
    /// Event phase.
    pub const fn phase(&self) -> ContactPhaseV1 {
        self.phase
    }
    /// Point or window shape.
    pub const fn kind(&self) -> ContactEventKindV1 {
        self.kind
    }

    /// Optional confidence in the closed interval `[0, 1]`.
    pub const fn confidence(&self) -> Option<f64> {
        self.confidence
    }
}

impl Serialize for ContactEventV1 {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(Some(5))?;
        map.serialize_entry("event_id", &self.event_id)?;
        map.serialize_entry("role", &self.role)?;
        map.serialize_entry("phase", &self.phase)?;
        match self.kind {
            ContactEventKindV1::Point(time) => map.serialize_entry("time", &time)?,
            ContactEventKindV1::Window(window) => map.serialize_entry("window", &window)?,
        }
        if let Some(confidence) = self.confidence {
            map.serialize_entry("confidence", &confidence)?;
        }
        map.end()
    }
}

/// One strict extension envelope preserved as a generic JSON object payload.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ContactExtensionV1 {
    schema: String,
    schema_version: u32,
    payload: Value,
}

impl ContactExtensionV1 {
    /// Construct and bound an opaque extension payload.
    pub fn new(
        schema: impl Into<String>,
        schema_version: u32,
        payload: Value,
    ) -> Result<Self, ContactFragmentError> {
        let mut value = Self {
            schema: schema.into(),
            schema_version,
            payload,
        };
        identifier(&value.schema, "extension.schema")?;
        if value.schema_version == 0 {
            return invalid("extension.schema_version", "must be positive");
        }
        if !value.payload.is_object() {
            return invalid("extension.payload", "must be an object");
        }
        validate_json_value(
            &value.payload,
            1,
            CONTACT_FRAGMENT_V1_MAX_EXTENSION_PAYLOAD_DEPTH,
            "extension.payload",
        )?;
        let canonical_payload = jcs(
            &value.payload,
            CONTACT_FRAGMENT_V1_MAX_EXTENSION_PAYLOAD_BYTES,
        )?;
        value.payload = serde_json::from_slice(&canonical_payload).map_err(|error| {
            ContactFragmentError::InvalidField {
                field: "extension.payload",
                message: error.to_string(),
            }
        })?;
        Ok(value)
    }

    /// Versioned extension schema identity.
    pub fn schema(&self) -> &str {
        &self.schema
    }

    /// Extension schema version.
    pub const fn schema_version(&self) -> u32 {
        self.schema_version
    }

    /// Opaque, strict-object extension payload.
    pub fn payload(&self) -> &Value {
        &self.payload
    }
}

/// A validated, canonicalizable contact-fragment V1 envelope.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ContactFragmentV1 {
    schema: &'static str,
    schema_version: u32,
    producer: ContactProducerV1,
    artifact: InputIdentity,
    dependency_closure_identity: DependencyClosureIdentityV1,
    clip: ContactClipReferenceV1,
    duration_s: f64,
    events: Vec<ContactEventV1>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    extensions: Vec<ContactExtensionV1>,
}

impl ContactFragmentV1 {
    /// Construct an envelope and impose the deterministic V1 event order.
    pub fn new(
        producer: ContactProducerV1,
        artifact: InputIdentity,
        dependency_closure_identity: DependencyClosureIdentityV1,
        clip: ContactClipReferenceV1,
        duration_s: f64,
        mut events: Vec<ContactEventV1>,
        extensions: Vec<ContactExtensionV1>,
    ) -> Result<Self, ContactFragmentError> {
        safe_identity_bytes(&artifact, "artifact.bytes")?;
        safe_identity_bytes(
            dependency_closure_identity.input_identity(),
            "dependency_closure_identity.bytes",
        )?;
        if !safe_f64(duration_s) {
            return invalid("duration_s", "must be an RFC 8785 safe number");
        }
        positive_finite(duration_s, "duration_s")?;
        let duration_s = canonical_number(duration_s);
        clip.validate()?;
        if events.len() > CONTACT_FRAGMENT_V1_MAX_EVENTS {
            return limit("events", events.len(), CONTACT_FRAGMENT_V1_MAX_EVENTS);
        }
        if extensions.len() > CONTACT_FRAGMENT_V1_MAX_EXTENSIONS {
            return limit(
                "extensions",
                extensions.len(),
                CONTACT_FRAGMENT_V1_MAX_EXTENSIONS,
            );
        }
        let mut ids = BTreeSet::new();
        for event in &events {
            if !ids.insert(event.event_id.clone()) {
                return invalid("events", "event_id must be unique");
            }
        }
        events.sort_by(event_order);
        let result = Self {
            schema: CONTACT_FRAGMENT_V1_ID,
            schema_version: CONTACT_FRAGMENT_V1_SCHEMA_VERSION,
            producer,
            artifact,
            dependency_closure_identity,
            clip,
            duration_s,
            events,
            extensions,
        };
        let _ = result.canonical_json()?;
        Ok(result)
    }

    /// Strictly decode one bounded UTF-8 JSON contact fragment.
    pub fn read_json(bytes: &[u8]) -> Result<Self, ContactFragmentError> {
        if bytes.len() > CONTACT_FRAGMENT_V1_MAX_SOURCE_BYTES {
            return Err(ContactFragmentError::SourceTooLarge {
                bytes: bytes.len(),
                limit: CONTACT_FRAGMENT_V1_MAX_SOURCE_BYTES,
            });
        }
        let mut deserializer = serde_json::Deserializer::from_slice(bytes);
        let wire = ContactFragmentWire::deserialize(&mut deserializer).map_err(|error| {
            ContactFragmentError::InvalidJson {
                message: error.to_string(),
            }
        })?;
        deserializer
            .end()
            .map_err(|error| ContactFragmentError::InvalidJson {
                message: error.to_string(),
            })?;
        if wire.events.overflowed {
            return limit(
                "events",
                CONTACT_FRAGMENT_V1_MAX_EVENTS + 1,
                CONTACT_FRAGMENT_V1_MAX_EVENTS,
            );
        }
        if wire.extensions.overflowed {
            return limit(
                "extensions",
                CONTACT_FRAGMENT_V1_MAX_EXTENSIONS + 1,
                CONTACT_FRAGMENT_V1_MAX_EXTENSIONS,
            );
        }
        if wire.extensions_present && wire.extensions.values.is_empty() {
            return invalid("extensions", "must be omitted when empty");
        }
        let value = wire.into_value();
        parse_fragment(value)
    }

    /// RFC 8785 bytes, with the frozen V1 canonical-output bound.
    pub fn canonical_json(&self) -> Result<Vec<u8>, ContactFragmentError> {
        jcs(self, CONTACT_FRAGMENT_V1_MAX_CANONICAL_BYTES)
    }
    /// Identity of the exact canonical fragment bytes.
    pub fn canonical_identity(&self) -> Result<InputIdentity, ContactFragmentError> {
        Ok(InputIdentity::from_bytes(&self.canonical_json()?))
    }

    /// Exact tool and version that produced this fragment.
    pub fn producer(&self) -> &ContactProducerV1 {
        &self.producer
    }

    /// Events in frozen V1 canonical event order.
    pub fn events(&self) -> &[ContactEventV1] {
        &self.events
    }

    /// Exact source-artifact identity binding.
    pub fn artifact(&self) -> &InputIdentity {
        &self.artifact
    }

    /// Complete dependency-closure identity binding.
    pub fn dependency_closure_identity(&self) -> &DependencyClosureIdentityV1 {
        &self.dependency_closure_identity
    }

    /// Selected clip witness.
    pub fn clip(&self) -> &ContactClipReferenceV1 {
        &self.clip
    }

    /// Positive clip duration in seconds.
    pub const fn duration_s(&self) -> f64 {
        self.duration_s
    }

    /// Strict extension envelopes in declared array order.
    pub fn extensions(&self) -> &[ContactExtensionV1] {
        &self.extensions
    }
}

fn parse_fragment(value: Value) -> Result<ContactFragmentV1, ContactFragmentError> {
    let mut root = object(value, "fragment")?;
    exact_fields(
        &root,
        "fragment",
        &[
            "schema",
            "schema_version",
            "producer",
            "artifact",
            "dependency_closure_identity",
            "clip",
            "duration_s",
            "events",
            "extensions",
        ],
    )?;
    let schema = string(take(&mut root, "schema", "fragment")?, "schema")?;
    if schema != CONTACT_FRAGMENT_V1_ID {
        return invalid("schema", "must equal contact-fragment V1 schema id");
    }
    let version = u32_value(
        take(&mut root, "schema_version", "fragment")?,
        "schema_version",
    )?;
    if version != CONTACT_FRAGMENT_V1_SCHEMA_VERSION {
        return invalid("schema_version", "must equal 1");
    }
    let producer = parse_producer(take(&mut root, "producer", "fragment")?)?;
    let artifact = parse_input_identity(take(&mut root, "artifact", "fragment")?, "artifact")?;
    let dependency_closure_identity = parse_dependency_closure_identity(
        take(&mut root, "dependency_closure_identity", "fragment")?,
        "dependency_closure_identity",
    )?;
    let clip = parse_clip(take(&mut root, "clip", "fragment")?)?;
    let duration_s = number(take(&mut root, "duration_s", "fragment")?, "duration_s")?;
    let events = array(take(&mut root, "events", "fragment")?, "events")?
        .into_iter()
        .map(parse_event)
        .collect::<Result<Vec<_>, _>>()?;
    let extensions = match root.remove("extensions") {
        Some(value) => array(value, "extensions")?
            .into_iter()
            .map(parse_extension)
            .collect::<Result<Vec<_>, _>>()?,
        None => Vec::new(),
    };
    ContactFragmentV1::new(
        producer,
        artifact,
        dependency_closure_identity,
        clip,
        duration_s,
        events,
        extensions,
    )
}

fn parse_producer(value: Value) -> Result<ContactProducerV1, ContactFragmentError> {
    let mut value = object(value, "producer")?;
    exact_fields(&value, "producer", &["tool", "version"])?;
    ContactProducerV1::new(
        string(take(&mut value, "tool", "producer")?, "producer.tool")?,
        string(take(&mut value, "version", "producer")?, "producer.version")?,
    )
}

fn parse_input_identity(
    value: Value,
    field: &'static str,
) -> Result<InputIdentity, ContactFragmentError> {
    serde_json::from_value(normalize_identity_wire(value, field)?).map_err(|error| {
        ContactFragmentError::InvalidField {
            field,
            message: error.to_string(),
        }
    })
}

fn parse_dependency_closure_identity(
    value: Value,
    field: &'static str,
) -> Result<DependencyClosureIdentityV1, ContactFragmentError> {
    serde_json::from_value(normalize_identity_wire(value, field)?).map_err(|error| {
        ContactFragmentError::InvalidField {
            field,
            message: error.to_string(),
        }
    })
}

fn normalize_identity_wire(
    value: Value,
    field: &'static str,
) -> Result<Value, ContactFragmentError> {
    let mut value = object(value, field)?;
    let bytes_field = if field == "artifact" {
        "artifact.bytes"
    } else {
        "dependency_closure_identity.bytes"
    };
    let bytes = unsigned_integer(
        take(&mut value, "bytes", field)?,
        CONTACT_FRAGMENT_V1_MAX_SAFE_INTEGER,
        bytes_field,
    )?;
    value.insert("bytes".into(), Value::Number(bytes.into()));
    Ok(Value::Object(value))
}
fn parse_clip(value: Value) -> Result<ContactClipReferenceV1, ContactFragmentError> {
    let mut value = object(value, "clip")?;
    let scope = string(take(&mut value, "scope", "clip")?, "clip.scope")?;
    match scope.as_str() {
        "document" => {
            exact_fields(&value, "clip", &["scope", "clip_name"])?;
            ContactClipReferenceV1::document(string(
                take(&mut value, "clip_name", "clip")?,
                "clip.clip_name",
            )?)
        }
        "collection" => {
            exact_fields(
                &value,
                "clip",
                &["scope", "logical_id", "source", "take_index", "take_name"],
            )?;
            ContactClipReferenceV1::collection(
                string(take(&mut value, "logical_id", "clip")?, "clip.logical_id")?,
                string(take(&mut value, "source", "clip")?, "clip.source")?,
                u32_value(take(&mut value, "take_index", "clip")?, "clip.take_index")?,
                string(take(&mut value, "take_name", "clip")?, "clip.take_name")?,
            )
        }
        _ => invalid("clip.scope", "must be document or collection"),
    }
}
fn parse_event(value: Value) -> Result<ContactEventV1, ContactFragmentError> {
    let mut value = object(value, "event")?;
    exact_fields(
        &value,
        "event",
        &["event_id", "role", "phase", "time", "window", "confidence"],
    )?;
    let event_id = string(take(&mut value, "event_id", "event")?, "event.event_id")?;
    let role = parse_role(&string(take(&mut value, "role", "event")?, "event.role")?)?;
    let phase = parse_phase(&string(take(&mut value, "phase", "event")?, "event.phase")?)?;
    let confidence = value
        .remove("confidence")
        .map(|value| number(value, "event.confidence"))
        .transpose()?;
    match (value.remove("time"), value.remove("window")) {
        (Some(time), None) => ContactEventV1::point(
            event_id,
            role,
            phase,
            number(time, "event.time")?,
            confidence,
        ),
        (None, Some(window)) => {
            let mut window = object(window, "event.window")?;
            exact_fields(&window, "event.window", &["start", "end"])?;
            ContactEventV1::window(
                event_id,
                role,
                phase,
                ContactEventWindowV1::new(
                    number(
                        take(&mut window, "start", "event.window")?,
                        "event.window.start",
                    )?,
                    number(
                        take(&mut window, "end", "event.window")?,
                        "event.window.end",
                    )?,
                )?,
                confidence,
            )
        }
        _ => invalid("event", "must contain exactly one of time or window"),
    }
}
fn parse_extension(value: Value) -> Result<ContactExtensionV1, ContactFragmentError> {
    let mut value = object(value, "extension")?;
    exact_fields(
        &value,
        "extension",
        &["schema", "schema_version", "payload"],
    )?;
    ContactExtensionV1::new(
        string(take(&mut value, "schema", "extension")?, "extension.schema")?,
        u32_value(
            take(&mut value, "schema_version", "extension")?,
            "extension.schema_version",
        )?,
        take(&mut value, "payload", "extension")?,
    )
}

fn parse_role(value: &str) -> Result<ContactRoleV1, ContactFragmentError> {
    Ok(match value {
        "left_foot" => ContactRoleV1::LeftFoot,
        "right_foot" => ContactRoleV1::RightFoot,
        "left_hand" => ContactRoleV1::LeftHand,
        "right_hand" => ContactRoleV1::RightHand,
        "left_toe" => ContactRoleV1::LeftToe,
        "right_toe" => ContactRoleV1::RightToe,
        "left_knee" => ContactRoleV1::LeftKnee,
        "right_knee" => ContactRoleV1::RightKnee,
        "left_elbow" => ContactRoleV1::LeftElbow,
        "right_elbow" => ContactRoleV1::RightElbow,
        "root" => ContactRoleV1::Root,
        "prop" => ContactRoleV1::Prop,
        "body" => ContactRoleV1::Body,
        _ => return invalid("event.role", "is not a V1 role"),
    })
}
fn parse_phase(value: &str) -> Result<ContactPhaseV1, ContactFragmentError> {
    Ok(match value {
        "begin" => ContactPhaseV1::Begin,
        "end" => ContactPhaseV1::End,
        "marker" => ContactPhaseV1::Marker,
        _ => return invalid("event.phase", "is not a V1 phase"),
    })
}

fn event_order(left: &ContactEventV1, right: &ContactEventV1) -> Ordering {
    let (left_start, left_rank, left_end) = event_key(left);
    let (right_start, right_rank, right_end) = event_key(right);
    left_start
        .total_cmp(&right_start)
        .then(left_rank.cmp(&right_rank))
        .then_with(|| match (left_end, right_end) {
            (None, None) => Ordering::Equal,
            (None, Some(_)) => Ordering::Less,
            (Some(_), None) => Ordering::Greater,
            (Some(a), Some(b)) => a.total_cmp(&b),
        })
        .then(utf16_cmp(role_name(left.role), role_name(right.role)))
        .then(utf16_cmp(phase_name(left.phase), phase_name(right.phase)))
        .then(utf16_cmp(&left.event_id, &right.event_id))
}
fn event_key(event: &ContactEventV1) -> (f64, u8, Option<f64>) {
    match event.kind {
        ContactEventKindV1::Point(time) => (time, 0, None),
        ContactEventKindV1::Window(window) => (window.start, 1, Some(window.end)),
    }
}
fn utf16_cmp(left: &str, right: &str) -> Ordering {
    left.encode_utf16().cmp(right.encode_utf16())
}
fn role_name(value: ContactRoleV1) -> &'static str {
    match value {
        ContactRoleV1::LeftFoot => "left_foot",
        ContactRoleV1::RightFoot => "right_foot",
        ContactRoleV1::LeftHand => "left_hand",
        ContactRoleV1::RightHand => "right_hand",
        ContactRoleV1::LeftToe => "left_toe",
        ContactRoleV1::RightToe => "right_toe",
        ContactRoleV1::LeftKnee => "left_knee",
        ContactRoleV1::RightKnee => "right_knee",
        ContactRoleV1::LeftElbow => "left_elbow",
        ContactRoleV1::RightElbow => "right_elbow",
        ContactRoleV1::Root => "root",
        ContactRoleV1::Prop => "prop",
        ContactRoleV1::Body => "body",
    }
}
fn phase_name(value: ContactPhaseV1) -> &'static str {
    match value {
        ContactPhaseV1::Begin => "begin",
        ContactPhaseV1::End => "end",
        ContactPhaseV1::Marker => "marker",
    }
}

fn object(
    value: Value,
    field: &'static str,
) -> Result<serde_json::Map<String, Value>, ContactFragmentError> {
    match value {
        Value::Object(value) => Ok(value),
        _ => Err(ContactFragmentError::InvalidField {
            field,
            message: "must be an object".into(),
        }),
    }
}
fn array(value: Value, field: &'static str) -> Result<Vec<Value>, ContactFragmentError> {
    match value {
        Value::Array(value) => Ok(value),
        _ => Err(ContactFragmentError::InvalidField {
            field,
            message: "must be an array".into(),
        }),
    }
}
fn string(value: Value, field: &'static str) -> Result<String, ContactFragmentError> {
    value
        .as_str()
        .map(str::to_owned)
        .ok_or_else(|| ContactFragmentError::InvalidField {
            field,
            message: "must be a string".into(),
        })
}
fn number(value: Value, field: &'static str) -> Result<f64, ContactFragmentError> {
    value
        .as_f64()
        .filter(|value| value.is_finite())
        .ok_or_else(|| ContactFragmentError::InvalidField {
            field,
            message: "must be a finite JSON number".into(),
        })
}
fn u32_value(value: Value, field: &'static str) -> Result<u32, ContactFragmentError> {
    Ok(unsigned_integer(value, u64::from(u32::MAX), field)? as u32)
}

fn unsigned_integer(
    value: Value,
    maximum: u64,
    field: &'static str,
) -> Result<u64, ContactFragmentError> {
    let Value::Number(number) = value else {
        return invalid(field, "must be a nonnegative integer-valued JSON number");
    };
    if let Some(value) = number.as_u64() {
        if value <= maximum {
            return Ok(value);
        }
    } else if let Some(value) = number.as_f64()
        && safe_f64(value)
        && value >= 0.0
        && value.fract() == 0.0
        && value <= maximum as f64
    {
        return Ok(value as u64);
    }
    invalid(field, "must be a nonnegative integer-valued JSON number")
}
fn take(
    map: &mut serde_json::Map<String, Value>,
    key: &'static str,
    field: &'static str,
) -> Result<Value, ContactFragmentError> {
    map.remove(key)
        .ok_or_else(|| ContactFragmentError::InvalidField {
            field,
            message: format!("is missing {key:?}"),
        })
}
fn exact_fields(
    map: &serde_json::Map<String, Value>,
    field: &'static str,
    allowed: &[&str],
) -> Result<(), ContactFragmentError> {
    for key in map.keys() {
        if !allowed.contains(&key.as_str()) {
            return invalid(field, "contains an unknown field");
        }
    }
    Ok(())
}
fn invalid<T>(field: &'static str, message: &'static str) -> Result<T, ContactFragmentError> {
    Err(ContactFragmentError::InvalidField {
        field,
        message: message.into(),
    })
}
fn limit<T>(field: &'static str, found: usize, limit: usize) -> Result<T, ContactFragmentError> {
    Err(ContactFragmentError::LimitExceeded {
        field,
        found,
        limit,
    })
}
fn text(value: &str, field: &'static str) -> Result<(), ContactFragmentError> {
    if value.is_empty() {
        return invalid(field, "must not be empty");
    }
    if value.len() > CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES {
        return limit(field, value.len(), CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES);
    }
    Ok(())
}
fn identifier(value: &str, field: &'static str) -> Result<(), ContactFragmentError> {
    text(value, field)?;
    if value.len() > CONTACT_FRAGMENT_V1_MAX_IDENTIFIER_BYTES {
        return limit(field, value.len(), CONTACT_FRAGMENT_V1_MAX_IDENTIFIER_BYTES);
    }
    Ok(())
}
fn positive_finite(value: f64, field: &'static str) -> Result<(), ContactFragmentError> {
    if !value.is_finite() || value <= 0.0 {
        return invalid(field, "must be finite and positive");
    }
    Ok(())
}
fn normalized_time(value: f64, field: &'static str) -> Result<(), ContactFragmentError> {
    finite_range(value, 0.0, 1.0, field)
}
fn finite_range(
    value: f64,
    min: f64,
    max: f64,
    field: &'static str,
) -> Result<(), ContactFragmentError> {
    if !value.is_finite() || value < min || value > max {
        return invalid(field, "must be finite and within its V1 range");
    }
    Ok(())
}
fn canonical_number(value: f64) -> f64 {
    if value == 0.0 { 0.0 } else { value }
}

fn safe_identity_bytes(
    identity: &InputIdentity,
    field: &'static str,
) -> Result<(), ContactFragmentError> {
    if identity.bytes() > CONTACT_FRAGMENT_V1_MAX_SAFE_INTEGER {
        return invalid(field, "must be an RFC 8785 safe integer");
    }
    Ok(())
}

fn safe_i64(value: i64) -> bool {
    value.unsigned_abs() <= CONTACT_FRAGMENT_V1_MAX_SAFE_INTEGER
}

fn safe_u64(value: u64) -> bool {
    value <= CONTACT_FRAGMENT_V1_MAX_SAFE_INTEGER
}

fn safe_f64(value: f64) -> bool {
    value.is_finite() && value.abs() <= CONTACT_FRAGMENT_V1_MAX_SAFE_INTEGER as f64
}

fn validate_jcs_number(
    value: &serde_json::Number,
    field: &'static str,
) -> Result<(), ContactFragmentError> {
    if let Some(value) = value.as_i64() {
        if !safe_i64(value) {
            return invalid(field, "contains an RFC 8785 unsafe integer");
        }
    } else if let Some(value) = value.as_u64() {
        if !safe_u64(value) {
            return invalid(field, "contains an RFC 8785 unsafe integer");
        }
    } else if !value.as_f64().is_some_and(safe_f64) {
        return invalid(field, "contains a non-finite or RFC 8785 unsafe number");
    }
    Ok(())
}

fn validate_json_value(
    value: &Value,
    depth: usize,
    max_depth: usize,
    field: &'static str,
) -> Result<(), ContactFragmentError> {
    if matches!(value, Value::Array(_) | Value::Object(_)) && depth > max_depth {
        return limit(field, depth, max_depth);
    }
    match value {
        Value::String(value) => {
            if value.len() > CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES {
                return limit(field, value.len(), CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES);
            }
        }
        Value::Array(values) => {
            for value in values {
                validate_json_value(value, depth + 1, max_depth, field)?;
            }
        }
        Value::Object(values) => {
            for (key, value) in values {
                if key.len() > CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES {
                    return limit(field, key.len(), CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES);
                }
                validate_json_value(value, depth + 1, max_depth, field)?;
            }
        }
        Value::Number(value) => validate_jcs_number(value, field)?,
        _ => {}
    }
    Ok(())
}

fn jcs<T: Serialize>(value: &T, limit: usize) -> Result<Vec<u8>, ContactFragmentError> {
    let mut output = CappedWriter {
        bytes: Vec::new(),
        limit,
        overflowed: false,
    };
    serde_jcs::to_writer(&mut output, value).map_err(|error| {
        if output.overflowed {
            ContactFragmentError::CanonicalTooLarge { limit }
        } else {
            ContactFragmentError::InvalidJson {
                message: error.to_string(),
            }
        }
    })?;
    Ok(output.bytes)
}
struct CappedWriter {
    bytes: Vec<u8>,
    limit: usize,
    overflowed: bool,
}
impl Write for CappedWriter {
    fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
        if self.bytes.len().saturating_add(bytes.len()) > self.limit {
            self.overflowed = true;
            return Err(io::Error::other("contact-fragment canonical limit"));
        }
        self.bytes.extend_from_slice(bytes);
        Ok(bytes.len())
    }
    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

enum StrictJsonValue {
    Null,
    Bool(bool),
    Number(serde_json::Number),
    String(String),
    Array(Vec<Self>),
    Object(BTreeMap<String, Self>),
}

struct CappedValues {
    values: Vec<StrictJsonValue>,
    overflowed: bool,
}

struct CappedValuesSeed {
    limit: usize,
    depth: usize,
}

struct CappedExtensionValuesSeed;

impl<'de> DeserializeSeed<'de> for CappedExtensionValuesSeed {
    type Value = CappedValues;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct CappedExtensionValuesVisitor;

        impl<'de> Visitor<'de> for CappedExtensionValuesVisitor {
            type Value = CappedValues;

            fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                formatter.write_str("a JSON array with bounded contact extensions")
            }

            fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
            where
                A: SeqAccess<'de>,
            {
                let mut values = Vec::with_capacity(
                    sequence
                        .size_hint()
                        .unwrap_or(0)
                        .min(CONTACT_FRAGMENT_V1_MAX_EXTENSIONS),
                );
                while values.len() < CONTACT_FRAGMENT_V1_MAX_EXTENSIONS {
                    let Some(value) = sequence.next_element::<ContactExtensionWire>()? else {
                        return Ok(CappedValues {
                            values,
                            overflowed: false,
                        });
                    };
                    values.push(value.value);
                }
                let overflowed = sequence.next_element::<IgnoredAny>()?.is_some();
                if overflowed {
                    while sequence.next_element::<IgnoredAny>()?.is_some() {}
                }
                Ok(CappedValues { values, overflowed })
            }
        }

        deserializer.deserialize_seq(CappedExtensionValuesVisitor)
    }
}

struct ContactExtensionWire {
    value: StrictJsonValue,
}

impl<'de> Deserialize<'de> for ContactExtensionWire {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ContactExtensionVisitor;

        impl<'de> Visitor<'de> for ContactExtensionVisitor {
            type Value = ContactExtensionWire;

            fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                formatter.write_str("a strict bounded contact extension object")
            }

            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
            where
                A: MapAccess<'de>,
            {
                let mut fields = BTreeMap::new();
                while let Some(key) = map.next_key::<String>()? {
                    if fields.contains_key(&key) {
                        return Err(A::Error::custom(format!("duplicate object member {key:?}")));
                    }
                    let value = match key.as_str() {
                        "schema" | "schema_version" => {
                            map.next_value_seed(StrictJsonValueSeed { depth: 4 })?
                        }
                        "payload" => {
                            map.next_value_seed(MeasuredJsonValueSeed { depth: 1 })?
                                .value
                        }
                        _ => {
                            let _ = map.next_value::<IgnoredAny>()?;
                            return Err(A::Error::custom(
                                "contact extension contains an unknown field",
                            ));
                        }
                    };
                    fields.insert(key, value);
                }
                Ok(ContactExtensionWire {
                    value: StrictJsonValue::Object(fields),
                })
            }
        }

        deserializer.deserialize_map(ContactExtensionVisitor)
    }
}

impl<'de> DeserializeSeed<'de> for CappedValuesSeed {
    type Value = CappedValues;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct CappedValuesVisitor {
            limit: usize,
            depth: usize,
        }

        impl<'de> Visitor<'de> for CappedValuesVisitor {
            type Value = CappedValues;

            fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(formatter, "a JSON array with at most {} values", self.limit)
            }

            fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
            where
                A: SeqAccess<'de>,
            {
                let mut values =
                    Vec::with_capacity(sequence.size_hint().unwrap_or(0).min(self.limit));
                while values.len() < self.limit {
                    let Some(value) =
                        sequence.next_element_seed(StrictJsonValueSeed { depth: self.depth })?
                    else {
                        return Ok(CappedValues {
                            values,
                            overflowed: false,
                        });
                    };
                    values.push(value);
                }
                let overflowed = sequence.next_element::<IgnoredAny>()?.is_some();
                if overflowed {
                    while sequence.next_element::<IgnoredAny>()?.is_some() {}
                }
                Ok(CappedValues { values, overflowed })
            }
        }

        deserializer.deserialize_seq(CappedValuesVisitor {
            limit: self.limit,
            depth: self.depth,
        })
    }
}

struct ContactFragmentWire {
    fields: BTreeMap<String, StrictJsonValue>,
    events: CappedValues,
    events_present: bool,
    extensions: CappedValues,
    extensions_present: bool,
}

impl ContactFragmentWire {
    fn into_value(self) -> Value {
        let mut fields = self
            .fields
            .into_iter()
            .map(|(key, value)| (key, value.into_value()))
            .collect::<serde_json::Map<_, _>>();
        if self.events_present {
            fields.insert(
                "events".into(),
                Value::Array(
                    self.events
                        .values
                        .into_iter()
                        .map(StrictJsonValue::into_value)
                        .collect(),
                ),
            );
        }
        if !self.extensions.values.is_empty() {
            fields.insert(
                "extensions".into(),
                Value::Array(
                    self.extensions
                        .values
                        .into_iter()
                        .map(StrictJsonValue::into_value)
                        .collect(),
                ),
            );
        }
        Value::Object(fields)
    }
}

impl<'de> Deserialize<'de> for ContactFragmentWire {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ContactFragmentVisitor;

        impl<'de> Visitor<'de> for ContactFragmentVisitor {
            type Value = ContactFragmentWire;

            fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                formatter.write_str("a strict contact-fragment V1 object")
            }

            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
            where
                A: MapAccess<'de>,
            {
                let mut fields = BTreeMap::new();
                let mut seen = BTreeSet::new();
                let mut events = None;
                let mut extensions = None;
                while let Some(key) = map.next_key::<String>()? {
                    if !seen.insert(key.clone()) {
                        return Err(A::Error::custom(format!("duplicate object member {key:?}")));
                    }
                    match key.as_str() {
                        "events" => {
                            events = Some(map.next_value_seed(CappedValuesSeed {
                                limit: CONTACT_FRAGMENT_V1_MAX_EVENTS,
                                depth: 3,
                            })?)
                        }
                        "extensions" => {
                            extensions = Some(map.next_value_seed(CappedExtensionValuesSeed)?)
                        }
                        "schema"
                        | "schema_version"
                        | "producer"
                        | "artifact"
                        | "dependency_closure_identity"
                        | "clip"
                        | "duration_s" => {
                            fields.insert(
                                key,
                                map.next_value_seed(StrictJsonValueSeed { depth: 2 })?,
                            );
                        }
                        _ => {
                            let _ = map.next_value::<IgnoredAny>()?;
                            return Err(A::Error::custom(
                                "contact fragment contains an unknown field",
                            ));
                        }
                    }
                }
                let extensions_present = extensions.is_some();
                Ok(ContactFragmentWire {
                    fields,
                    events_present: events.is_some(),
                    events: events.unwrap_or(CappedValues {
                        values: Vec::new(),
                        overflowed: false,
                    }),
                    extensions: extensions.unwrap_or(CappedValues {
                        values: Vec::new(),
                        overflowed: false,
                    }),
                    extensions_present,
                })
            }
        }

        deserializer.deserialize_map(ContactFragmentVisitor)
    }
}

struct StrictJsonValueSeed {
    depth: usize,
}

impl<'de> DeserializeSeed<'de> for StrictJsonValueSeed {
    type Value = StrictJsonValue;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(StrictJsonValueVisitor { depth: self.depth })
    }
}

struct StrictJsonValueVisitor {
    depth: usize,
}
impl StrictJsonValue {
    fn into_value(self) -> Value {
        match self {
            Self::Null => Value::Null,
            Self::Bool(value) => Value::Bool(value),
            Self::Number(value) => Value::Number(value),
            Self::String(value) => Value::String(value),
            Self::Array(values) => Value::Array(values.into_iter().map(Self::into_value).collect()),
            Self::Object(values) => Value::Object(
                values
                    .into_iter()
                    .map(|(key, value)| (key, value.into_value()))
                    .collect(),
            ),
        }
    }
}
impl<'de> Deserialize<'de> for StrictJsonValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        StrictJsonValueSeed { depth: 1 }.deserialize(deserializer)
    }
}

impl<'de> Visitor<'de> for StrictJsonValueVisitor {
    type Value = StrictJsonValue;
    fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("JSON value")
    }
    fn visit_unit<E: serde::de::Error>(self) -> Result<Self::Value, E> {
        Ok(StrictJsonValue::Null)
    }
    fn visit_bool<E: serde::de::Error>(self, value: bool) -> Result<Self::Value, E> {
        Ok(StrictJsonValue::Bool(value))
    }
    fn visit_i64<E: serde::de::Error>(self, value: i64) -> Result<Self::Value, E> {
        if !safe_i64(value) {
            return Err(E::custom(
                "contact fragment contains an RFC 8785 unsafe integer",
            ));
        }
        Ok(StrictJsonValue::Number(value.into()))
    }
    fn visit_u64<E: serde::de::Error>(self, value: u64) -> Result<Self::Value, E> {
        if !safe_u64(value) {
            return Err(E::custom(
                "contact fragment contains an RFC 8785 unsafe integer",
            ));
        }
        Ok(StrictJsonValue::Number(value.into()))
    }
    fn visit_f64<E: serde::de::Error>(self, value: f64) -> Result<Self::Value, E> {
        if !safe_f64(value) {
            return Err(E::custom(
                "contact fragment contains a non-finite or RFC 8785 unsafe number",
            ));
        }
        serde_json::Number::from_f64(value)
            .map(StrictJsonValue::Number)
            .ok_or_else(|| E::custom("non-finite number"))
    }
    fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<Self::Value, E> {
        if value.len() > CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES {
            return Err(E::custom("contact fragment string exceeds V1 byte limit"));
        }
        Ok(StrictJsonValue::String(value.into()))
    }
    fn visit_string<E: serde::de::Error>(self, value: String) -> Result<Self::Value, E> {
        if value.len() > CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES {
            return Err(E::custom("contact fragment string exceeds V1 byte limit"));
        }
        Ok(StrictJsonValue::String(value))
    }
    fn visit_seq<A: SeqAccess<'de>>(self, mut values: A) -> Result<Self::Value, A::Error> {
        if self.depth > CONTACT_FRAGMENT_V1_MAX_DEPTH {
            return Err(A::Error::custom(
                "contact fragment JSON exceeds V1 nesting depth",
            ));
        }
        let mut output =
            Vec::with_capacity(values.size_hint().unwrap_or(0).min(MAX_OPAQUE_MEMBERS));
        while output.len() < MAX_OPAQUE_MEMBERS {
            let Some(value) = values.next_element_seed(StrictJsonValueSeed {
                depth: self.depth + 1,
            })?
            else {
                return Ok(StrictJsonValue::Array(output));
            };
            output.push(value);
        }
        let _ = values.next_element::<IgnoredAny>()?;
        Err(A::Error::custom(
            "contact fragment nested array exceeds V1 bounded member limit",
        ))
    }
    fn visit_map<A: MapAccess<'de>>(self, mut values: A) -> Result<Self::Value, A::Error> {
        if self.depth > CONTACT_FRAGMENT_V1_MAX_DEPTH {
            return Err(A::Error::custom(
                "contact fragment JSON exceeds V1 nesting depth",
            ));
        }
        let mut output = BTreeMap::new();
        while output.len() < MAX_OPAQUE_MEMBERS {
            let Some(key) = values.next_key::<String>()? else {
                return Ok(StrictJsonValue::Object(output));
            };
            if key.len() > CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES {
                return Err(A::Error::custom(
                    "contact fragment object key exceeds V1 byte limit",
                ));
            }
            let value = values.next_value_seed(StrictJsonValueSeed {
                depth: self.depth + 1,
            })?;
            if output.insert(key.clone(), value).is_some() {
                return Err(A::Error::custom(format!("duplicate object member {key:?}")));
            }
        }
        let _ = values.next_key::<IgnoredAny>()?;
        Err(A::Error::custom(
            "contact fragment nested object exceeds V1 bounded member limit",
        ))
    }
}

/// A payload value plus its exact RFC 8785 byte count. This is deliberately
/// local to contact-fragment decoding: it keeps the payload budget enforced
/// while the parser is still deciding whether to retain a child.
struct MeasuredJsonValue {
    value: StrictJsonValue,
    canonical_len: usize,
}

struct MeasuredJsonValueSeed {
    depth: usize,
}

impl<'de> DeserializeSeed<'de> for MeasuredJsonValueSeed {
    type Value = MeasuredJsonValue;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(MeasuredJsonValueVisitor { depth: self.depth })
    }
}

struct MeasuredJsonValueVisitor {
    depth: usize,
}

impl MeasuredJsonValue {
    fn scalar<E: serde::de::Error>(value: StrictJsonValue) -> Result<Self, E> {
        let json = match &value {
            StrictJsonValue::Null => Value::Null,
            StrictJsonValue::Bool(value) => Value::Bool(*value),
            StrictJsonValue::Number(value) => Value::Number(value.clone()),
            StrictJsonValue::String(value) => Value::String(value.clone()),
            StrictJsonValue::Array(_) | StrictJsonValue::Object(_) => {
                unreachable!("scalar measurement only receives scalars")
            }
        };
        let canonical_len = serde_jcs::to_vec(&json).map_err(E::custom)?.len();
        if canonical_len > CONTACT_FRAGMENT_V1_MAX_EXTENSION_PAYLOAD_BYTES {
            return Err(E::custom(
                "contact extension payload exceeds V1 canonical byte limit",
            ));
        }
        Ok(Self {
            value,
            canonical_len,
        })
    }

    fn bounded<E: serde::de::Error>(canonical_len: usize) -> Result<(), E> {
        if canonical_len > CONTACT_FRAGMENT_V1_MAX_EXTENSION_PAYLOAD_BYTES {
            return Err(E::custom(
                "contact extension payload exceeds V1 canonical byte limit",
            ));
        }
        Ok(())
    }
}

impl<'de> Visitor<'de> for MeasuredJsonValueVisitor {
    type Value = MeasuredJsonValue;

    fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("a bounded JSON extension payload value")
    }

    fn visit_unit<E: serde::de::Error>(self) -> Result<Self::Value, E> {
        MeasuredJsonValue::scalar(StrictJsonValue::Null)
    }

    fn visit_bool<E: serde::de::Error>(self, value: bool) -> Result<Self::Value, E> {
        MeasuredJsonValue::scalar(StrictJsonValue::Bool(value))
    }

    fn visit_i64<E: serde::de::Error>(self, value: i64) -> Result<Self::Value, E> {
        if !safe_i64(value) {
            return Err(E::custom(
                "contact extension payload contains an RFC 8785 unsafe integer",
            ));
        }
        MeasuredJsonValue::scalar(StrictJsonValue::Number(value.into()))
    }

    fn visit_u64<E: serde::de::Error>(self, value: u64) -> Result<Self::Value, E> {
        if !safe_u64(value) {
            return Err(E::custom(
                "contact extension payload contains an RFC 8785 unsafe integer",
            ));
        }
        MeasuredJsonValue::scalar(StrictJsonValue::Number(value.into()))
    }

    fn visit_f64<E: serde::de::Error>(self, value: f64) -> Result<Self::Value, E> {
        if !safe_f64(value) {
            return Err(E::custom(
                "contact extension payload contains a non-finite or RFC 8785 unsafe number",
            ));
        }
        serde_json::Number::from_f64(value)
            .map(StrictJsonValue::Number)
            .ok_or_else(|| E::custom("non-finite number"))
            .and_then(MeasuredJsonValue::scalar)
    }

    fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<Self::Value, E> {
        if value.len() > CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES {
            return Err(E::custom(
                "contact extension payload string exceeds V1 byte limit",
            ));
        }
        MeasuredJsonValue::scalar(StrictJsonValue::String(value.into()))
    }

    fn visit_string<E: serde::de::Error>(self, value: String) -> Result<Self::Value, E> {
        if value.len() > CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES {
            return Err(E::custom(
                "contact extension payload string exceeds V1 byte limit",
            ));
        }
        MeasuredJsonValue::scalar(StrictJsonValue::String(value))
    }

    fn visit_seq<A>(self, mut values: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        if self.depth > CONTACT_FRAGMENT_V1_MAX_EXTENSION_PAYLOAD_DEPTH {
            return Err(A::Error::custom(
                "contact extension payload exceeds V1 nesting depth",
            ));
        }
        let mut output =
            Vec::with_capacity(values.size_hint().unwrap_or(0).min(MAX_OPAQUE_MEMBERS));
        let mut canonical_len = 2; // []
        while output.len() < MAX_OPAQUE_MEMBERS {
            let Some(child) = values.next_element_seed(MeasuredJsonValueSeed {
                depth: self.depth + 1,
            })?
            else {
                return Ok(MeasuredJsonValue {
                    value: StrictJsonValue::Array(output),
                    canonical_len,
                });
            };
            let candidate = canonical_len
                .saturating_add(child.canonical_len)
                .saturating_add(usize::from(!output.is_empty()));
            MeasuredJsonValue::bounded::<A::Error>(candidate)?;
            output.push(child.value);
            canonical_len = candidate;
        }
        let _ = values.next_element::<IgnoredAny>()?;
        Err(A::Error::custom(
            "contact extension payload nested array exceeds V1 bounded member limit",
        ))
    }

    fn visit_map<A>(self, mut values: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        if self.depth > CONTACT_FRAGMENT_V1_MAX_EXTENSION_PAYLOAD_DEPTH {
            return Err(A::Error::custom(
                "contact extension payload exceeds V1 nesting depth",
            ));
        }
        let mut output = BTreeMap::new();
        let mut canonical_len = 2; // {}
        while output.len() < MAX_OPAQUE_MEMBERS {
            let Some(key) = values.next_key::<String>()? else {
                return Ok(MeasuredJsonValue {
                    value: StrictJsonValue::Object(output),
                    canonical_len,
                });
            };
            if key.len() > CONTACT_FRAGMENT_V1_MAX_TEXT_BYTES {
                return Err(A::Error::custom(
                    "contact extension payload object key exceeds V1 byte limit",
                ));
            }
            if output.contains_key(&key) {
                let _ = values.next_value::<IgnoredAny>()?;
                return Err(A::Error::custom(format!("duplicate object member {key:?}")));
            }
            let child = values.next_value_seed(MeasuredJsonValueSeed {
                depth: self.depth + 1,
            })?;
            let key_len = serde_jcs::to_vec(&key).map_err(A::Error::custom)?.len();
            let candidate = canonical_len
                .saturating_add(key_len)
                .saturating_add(1) // colon
                .saturating_add(child.canonical_len)
                .saturating_add(usize::from(!output.is_empty()));
            MeasuredJsonValue::bounded::<A::Error>(candidate)?;
            output.insert(key, child.value);
            canonical_len = candidate;
        }
        let _ = values.next_key::<IgnoredAny>()?;
        Err(A::Error::custom(
            "contact extension payload nested object exceeds V1 bounded member limit",
        ))
    }
}