zenkey-model 0.24.0

zk2 keys and contracts without a Zenoh session: the key grammar, the contract authoring format, canonical form, fingerprints and bundles
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
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
//! Frames (core §7.7, 0.83; #981's K1): a plain stream's records of one
//! packed type (§7.6) under one envelope (§7.5) and one time.
//!
//! ```text
//! frame, a put's payload   CH · run · seq · body
//! body       time · count · record · … · record · pad
//! time       42 bits: record 0's measured time; all ones when unknown
//! count      the records in the frame, less 1, in ⌈log₂ records⌉ bits
//! record     full · when · offset · fields
//! full       1 bit: 1 for a full record, 0 for a delta record
//! when       1 bit, in record 1 and later only: 1 when the record is scheduled
//! offset     when `when` is 0: the record's time, in ms after record 0's, in ⌈log₂ span⌉ bits
//! fields     a full record: every field, as §7.6 writes it
//!            a delta record: one bit per field, then each field whose bit is 1
//! pad        zero bits up to the next whole byte, fewer than 8
//! ```
//!
//! - **The declaration** is a resource's `frame` table ([`FrameParams`],
//!   from authoring's [`FrameSpec`], E048 its findings), over a packed
//!   record type with no extension marker ([`Layout::of`]).
//! - **The tag** is §7.6's rule over the frame form ([`Layout::form`]): the
//!   table's `records`, `step_ms`, `jitter_ms` and deadbands, and the
//!   record's layout form. `keyframe_every` changes no bit, and is left out.
//! - **A body** is read with its layout ([`Frame::parse`], refusing for the
//!   first of §7.7's checks, [`ReadRefusal`]) and written back to its bytes
//!   ([`Frame::write`]). A record's fields are [`Slot`]s: omitted, included
//!   absent, or a code, which [`Frame::read_json`] spells as §7.6's JSON
//!   reading does.
//!
//! - **A key's frames** are read by a [`Reader`] (#981's K2): what each
//!   frame follows in the run it holds ([`Follows`]), and each record's
//!   seq, time ([`When`]) and field states ([`FieldState`]: `known`,
//!   `within`, `unknown` with why, `absent`), as §7.7's "The reader's
//!   fields" says; [`Applied::reading`] spells them as
//!   `spec/conformance/frame/apply.json` does, which observer §7.6's tools
//!   show.
//!
//! - **A key's writer** is a [`Framer`] (#981's K3): the records an owner
//!   measured ([`Measured`]) gathered into frames ([`Batch`]), each written
//!   under its run ([`Framer::encode`]) as §7.7's "The owner's frames"
//!   says, full where the run is new or `keyframe_every` asks, each delta
//!   field included where it changed or passed its deadband.

use std::collections::BTreeMap;
use std::fmt::Write as _;
use std::time::Duration;

use serde_json::{Map, Value, json};
use sha2::{Digest, Sha256};

use crate::authoring::{FrameSpec, Kind};
use crate::contract::{Body, Contract, Resource};
use crate::packed::{self, BitReader, BitWriter, Decimal, Field, FieldKind, Item};
use crate::wire::{Envelope, Run, Seq, Stamp, Tag};

/// The most records one frame holds.
pub const MAX_RECORDS: u64 = 256;
/// The greatest `step_ms`, 2^32 − 1.
pub const MAX_STEP_MS: u64 = u32::MAX as u64;
/// The greatest `keyframe_every`, and a deadband's greatest `max_records`.
pub const MAX_EVERY: u64 = 65_536;
/// The time code that says the owner's clock is unknown: all 42 bits set.
pub const TIME_UNKNOWN: u64 = (1 << 42) - 1;

/// A field's deadband (core §7.7): the field may be omitted while within
/// `delta` of the value last included, and is included at least once
/// every `max_records` records.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Deadband {
    /// Above 0, in the field's units. Compared as a value, so `"4.0"` and
    /// `"4"` are one delta.
    pub delta: Decimal,
    /// From 1 to 65,536.
    pub max_records: u32,
}

/// A frame resource's `frame` table, resolved (core §7.7): every key has
/// its value, a default's included.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FrameParams {
    /// From 1 to 256.
    pub records: u16,
    /// From 1 to 2^32 − 1, exactly when `records` is above 1.
    pub step_ms: Option<u32>,
    /// From 0 to `step_ms` / 2; set exactly when `step_ms` is.
    pub jitter_ms: Option<u32>,
    /// From 1 to 65,536.
    pub keyframe_every: u32,
    /// By field name.
    pub deadband: BTreeMap<String, Deadband>,
}

impl FrameParams {
    /// The table as written, resolved; or E048's findings, one sentence
    /// each, for every key and deadband member outside §7.7's table.
    ///
    /// # Errors
    /// Each value out of range, in words.
    pub fn resolve(spec: &FrameSpec) -> Result<Self, Vec<String>> {
        let mut why = Vec::new();
        let records = spec.records.unwrap_or(1);
        if !(1..=MAX_RECORDS).contains(&records) {
            why.push(format!("`records` = {records} is not from 1 to 256"));
        }
        match spec.step_ms {
            Some(s) if !(1..=MAX_STEP_MS).contains(&s) => {
                why.push(format!("`step_ms` = {s} is not from 1 to 2^32 - 1"));
            }
            Some(_) if records == 1 => {
                why.push("`step_ms` with `records` = 1: a one-record frame has no step".to_owned())
            }
            None if records > 1 => why.push(format!(
                "`records` = {records} and no `step_ms`: a frame of several records has a step"
            )),
            _ => {}
        }
        match (spec.jitter_ms, spec.step_ms) {
            (Some(_), None) => why.push("`jitter_ms` without `step_ms`".to_owned()),
            (Some(j), Some(s)) if j.saturating_mul(2) > s => {
                why.push(format!("`jitter_ms` = {j} is above `step_ms` / 2"));
            }
            _ => {}
        }
        let keyframe_every = spec.keyframe_every.unwrap_or(1);
        if !(1..=MAX_EVERY).contains(&keyframe_every) {
            why.push(format!(
                "`keyframe_every` = {keyframe_every} is not from 1 to 65,536"
            ));
        }
        let mut deadband = BTreeMap::new();
        for (name, d) in &spec.deadband {
            // Kept in its shortest form, so `"4.0"` and `"4"` are one value.
            let delta = Decimal::parse(&d.delta)
                .filter(|x| *x > Decimal::from(0))
                .and_then(|x| Decimal::parse(&x.to_string()));
            if delta.is_none() {
                why.push(format!(
                    "`deadband.{name}.delta` = {:?} is not a decimal string above 0",
                    d.delta
                ));
            }
            if !(1..=MAX_EVERY).contains(&d.max_records) {
                why.push(format!(
                    "`deadband.{name}.max_records` = {} is not from 1 to 65,536",
                    d.max_records
                ));
            }
            if let (Some(delta), Ok(max_records)) = (delta, u32::try_from(d.max_records)) {
                deadband.insert(name.clone(), Deadband { delta, max_records });
            }
        }
        if !why.is_empty() {
            return Err(why);
        }
        let step_ms = spec.step_ms.map(|s| u32::try_from(s).expect("checked"));
        Ok(Self {
            records: u16::try_from(records).expect("checked"),
            step_ms,
            jitter_ms: step_ms.map(|_| {
                u32::try_from(spec.jitter_ms.unwrap_or(0)).expect("checked below the step")
            }),
            keyframe_every: u32::try_from(keyframe_every).expect("checked"),
            deadband,
        })
    }

    /// The span, `records` × `step_ms`, where the frame has a step.
    #[must_use]
    pub fn span_ms(&self) -> Option<u64> {
        self.step_ms.map(|s| u64::from(self.records) * u64::from(s))
    }

    /// The frame resource's interval (core §7.7): its span where it has a
    /// step, else the `timing.period_ms` its annotations declare as
    /// `timing.v1` §2.2 reads them, else unstated.
    #[must_use]
    pub fn interval_ms(&self, annotations: &BTreeMap<String, Value>) -> Option<u64> {
        self.span_ms().or_else(
            || match crate::timing::declaration(Kind::Stream, annotations) {
                crate::timing::Declaration::Declared(d) => d.period.map(millis),
                _ => None,
            },
        )
    }

    /// Its canonical form's member (core §9.5): `records`, `step_ms` and
    /// `jitter_ms` (`null` without a step), `keyframe_every`, and
    /// `deadband` by field name, each `delta` in its shortest form.
    #[must_use]
    pub fn canonical(&self) -> Value {
        let deadband: Map<String, Value> = self
            .deadband
            .iter()
            .map(|(k, d)| {
                (
                    k.clone(),
                    json!({"delta": d.delta.to_string(), "max_records": d.max_records}),
                )
            })
            .collect();
        json!({
            "records": self.records,
            "step_ms": self.step_ms,
            "jitter_ms": self.jitter_ms,
            "keyframe_every": self.keyframe_every,
            "deadband": deadband,
        })
    }

    /// Its canonical form's member read back, as a bundle carries it.
    ///
    /// # Errors
    /// What is wrong with it, in words.
    pub fn from_canonical(v: &Value) -> Result<Self, String> {
        let m = v.as_object().ok_or("`frame` is not an object")?;
        let int = |k: &str| -> Result<Option<u64>, String> {
            match m.get(k) {
                None => Err(format!("`frame` has no `{k}`")),
                Some(Value::Null) => Ok(None),
                Some(x) => x
                    .as_u64()
                    .map(Some)
                    .ok_or_else(|| format!("`frame.{k}` is not an unsigned integer")),
            }
        };
        let mut deadband = BTreeMap::new();
        let db = m
            .get("deadband")
            .and_then(Value::as_object)
            .ok_or("`frame.deadband` is not an object")?;
        for (name, d) in db {
            let delta = d
                .get("delta")
                .and_then(Value::as_str)
                .ok_or_else(|| format!("`frame.deadband.{name}` has no `delta` string"))?;
            let max_records = d
                .get("max_records")
                .and_then(Value::as_u64)
                .ok_or_else(|| format!("`frame.deadband.{name}` has no `max_records`"))?;
            deadband.insert(
                name.clone(),
                crate::authoring::DeadbandSpec {
                    delta: delta.to_owned(),
                    max_records,
                },
            );
        }
        let step_ms = int("step_ms")?;
        let jitter_ms = int("jitter_ms")?;
        if step_ms.is_some() != jitter_ms.is_some() {
            return Err("`frame.jitter_ms` is null exactly when `step_ms` is".to_owned());
        }
        let spec = FrameSpec {
            records: Some(int("records")?.ok_or("`frame.records` is null")?),
            step_ms,
            jitter_ms,
            keyframe_every: Some(int("keyframe_every")?.ok_or("`frame.keyframe_every` is null")?),
            deadband,
        };
        Self::resolve(&spec).map_err(|why| why.join("; "))
    }
}

fn millis(d: Duration) -> u64 {
    u64::try_from(d.as_millis()).unwrap_or(u64::MAX)
}

/// ⌈log₂ n⌉, the fewest bits that hold n codes: 0 for one.
fn bits_for(n: u64) -> u32 {
    if n <= 1 {
        0
    } else {
        64 - (n - 1).leading_zeros()
    }
}

/// A frame resource's layout (core §7.7): its record type's fields, its
/// table, and the frame form its tag comes from.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Layout {
    pub params: FrameParams,
    fields: Vec<Field>,
    /// Each field's deadband, in field order.
    deadband: Vec<Option<Deadband>>,
    form: Value,
    sha256: [u8; 32],
}

impl Layout {
    /// The layout of a frame of `record`'s type under `params`.
    ///
    /// # Errors
    /// Why there is none: the record type has an extension marker (E047),
    /// or a deadband names no field of it, or one that is not an `int`,
    /// `fixed` or `counter` (E049).
    pub fn of(record: &packed::Layout, params: &FrameParams) -> Result<Self, String> {
        let mut fields = Vec::new();
        for it in &record.items {
            match it {
                Item::Marker => {
                    return Err(
                        "the record type has an extension marker, which a frame's type never has"
                            .to_owned(),
                    );
                }
                Item::Field(f) => fields.push(f.clone()),
            }
        }
        for name in params.deadband.keys() {
            match fields.iter().find(|f| &f.name == name) {
                None => return Err(format!("the deadband names {name:?}, no field of the type")),
                Some(f) if !takes_deadband(f) => {
                    return Err(format!(
                        "the deadband names {name:?}, a {} field, which takes none",
                        f.kind.as_str()
                    ));
                }
                Some(_) => {}
            }
        }
        let deadband: Vec<Option<Deadband>> = fields
            .iter()
            .map(|f| params.deadband.get(&f.name).cloned())
            .collect();
        let items: Vec<Value> = deadband
            .iter()
            .map(|d| match d {
                None => Value::Null,
                Some(d) => json!({"delta": d.delta.to_string(), "max_records": d.max_records}),
            })
            .collect();
        let form = json!({
            "frame": {
                "records": params.records,
                "step_ms": params.step_ms,
                "jitter_ms": params.jitter_ms,
                "deadband": items,
            },
            "record": record.form().clone(),
        });
        let sha256 = Sha256::digest(crate::schema::jcs(&form)).into();
        Ok(Self {
            params: params.clone(),
            fields,
            deadband,
            form,
            sha256,
        })
    }

    /// The layout of a contract's frame resource: `None` for a resource
    /// that is no frame resource.
    ///
    /// # Errors
    /// Why a frame resource has none: its type's layout (an artifact
    /// missing, or a declaration with a finding), or [`Layout::of`]'s.
    pub fn of_resource(c: &Contract, r: &Resource) -> Result<Option<Self>, String> {
        let Body::Data(d) = &r.body else {
            return Ok(None);
        };
        let Some(params) = &d.frame else {
            return Ok(None);
        };
        let record = c
            .packed_layout(&d.type_)?
            .ok_or_else(|| format!("{}: a frame resource's type is not packed", r.template))?;
        Self::of(&record, params).map(Some)
    }

    /// The frame form: the root of the tag (core §7.7).
    #[must_use]
    pub fn form(&self) -> &Value {
        &self.form
    }

    /// The sha256 of the frame form's JCS bytes, in lowercase hex.
    #[must_use]
    pub fn sha256_hex(&self) -> String {
        let mut s = String::with_capacity(64);
        for b in self.sha256 {
            let _ = write!(s, "{b:02x}");
        }
        s
    }

    /// The tag CH carries: the low four bits of the hash's last byte.
    #[must_use]
    pub fn tag(&self) -> Tag {
        Tag::new(self.sha256[31] & 0x0f).expect("four bits")
    }

    /// The count's width, ⌈log₂ records⌉: none for a one-record frame.
    #[must_use]
    pub fn count_bits(&self) -> u32 {
        bits_for(u64::from(self.params.records))
    }

    /// The offset's width, ⌈log₂ span⌉, where the frame has a step.
    #[must_use]
    pub fn offset_bits(&self) -> Option<u32> {
        self.params.span_ms().map(bits_for)
    }

    /// The span, where the frame has a step.
    #[must_use]
    pub fn span_ms(&self) -> Option<u64> {
        self.params.span_ms()
    }

    /// The record type's fields, in order.
    #[must_use]
    pub fn fields(&self) -> &[Field] {
        &self.fields
    }

    /// The deadband of the field at `index`, if it has one.
    #[must_use]
    pub fn deadband(&self, index: usize) -> Option<&Deadband> {
        self.deadband.get(index).and_then(Option::as_ref)
    }

    /// A record's time as a span of milliseconds after record 0: a
    /// scheduled record lies within `jitter_ms` of its instant.
    fn bounds(&self, i: usize, at: Option<At>) -> (u64, u64) {
        match at {
            None => (0, 0),
            Some(At::Offset(o)) => (o, o),
            Some(At::Scheduled) => {
                let step = u64::from(self.params.step_ms.unwrap_or(0));
                let jitter = u64::from(self.params.jitter_ms.unwrap_or(0));
                let t = step * i as u64;
                (t.saturating_sub(jitter), t + jitter)
            }
        }
    }
}

/// Whether a field takes a deadband: an `int`, `fixed` or `counter`.
#[must_use]
pub fn takes_deadband(f: &Field) -> bool {
    matches!(
        f.kind,
        FieldKind::Int { .. } | FieldKind::Fixed { .. } | FieldKind::Counter
    )
}

/// Record 0's time: seconds from the deployment epoch and milliseconds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Time {
    pub s: u32,
    /// From 0 to 999.
    pub ms: u16,
}

/// A later record's time, after record 0.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum At {
    /// `step_ms` × i after record 0, within `jitter_ms`: an instant and a
    /// bound, never a measurement.
    Scheduled,
    /// Measured: this many milliseconds after record 0.
    Offset(u64),
}

/// One field of a record, as the body carries it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Slot {
    /// A delta record's field whose bit is 0.
    Omitted,
    /// Included, with its presence bit 0.
    Absent,
    /// Included, with this code.
    Code(u64),
}

/// One record of a frame.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Record {
    /// A full record includes every field.
    pub full: bool,
    /// `None` for record 0, whose time is the frame's.
    pub at: Option<At>,
    /// One per field, in field order.
    pub slots: Vec<Slot>,
}

/// A frame's body, read: record 0's time, `None` when the owner's clock
/// was unknown, and its records.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Frame {
    pub time: Option<Time>,
    pub records: Vec<Record>,
}

/// Why a reader refused a body (core §7.7's order).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReadRefusal {
    /// CH's tag is not the frame's.
    Tag,
    /// The body ends before a bit its fields need, or holds a byte after
    /// its pad.
    Length,
    /// A code is unused: a millisecond code from 1000 up but the unknown
    /// time's, a count of `records` or more, an offset at or past the
    /// span, or a field's unused code.
    Code,
    /// A record lies before the record before it.
    Order,
    /// A bit of the pad is 1.
    Pad,
}

impl ReadRefusal {
    /// The token `spec/conformance/frame/bodies.json` pins.
    #[must_use]
    pub const fn token(self) -> &'static str {
        match self {
            Self::Tag => "tag",
            Self::Length => "length",
            Self::Code => "code",
            Self::Order => "order",
            Self::Pad => "pad",
        }
    }

    /// The refusal in words, its token first.
    #[must_use]
    pub const fn reason(self) -> &'static str {
        match self {
            Self::Tag => "tag: the envelope's tag is not the frame's",
            Self::Length => "length: the body is not as long as its fields read it",
            Self::Code => "code: a code is unused",
            Self::Order => "order: a record lies before the record before it",
            Self::Pad => "pad: a bit of the pad is 1",
        }
    }
}

impl std::fmt::Display for ReadRefusal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.reason())
    }
}

impl std::error::Error for ReadRefusal {}

impl From<packed::ReadRefusal> for ReadRefusal {
    fn from(r: packed::ReadRefusal) -> Self {
        match r {
            packed::ReadRefusal::Pad => Self::Pad,
            packed::ReadRefusal::Length => Self::Length,
            packed::ReadRefusal::Tag => Self::Tag,
            _ => Self::Code,
        }
    }
}

/// Why a writer refused a frame (core §7.7, the fixtures' tokens).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WriteRefusal {
    /// No record, or more than `records`.
    Records,
    /// A full record without a field that is not optional, or such a field
    /// written absent.
    Missing,
    /// A field the type does not have.
    Unknown,
    /// A value of the wrong JSON type, a name not declared, or a record
    /// whose time is not its place's (record 0 with one, a later one
    /// without).
    Value,
    /// A value with no code, an offset at or past the span, or a time
    /// outside its codes.
    Range,
    /// A record that lies before the record before it.
    Order,
}

impl WriteRefusal {
    /// The token `spec/conformance/frame/bodies.json` pins.
    #[must_use]
    pub const fn token(self) -> &'static str {
        match self {
            Self::Records => "records",
            Self::Missing => "missing",
            Self::Unknown => "unknown",
            Self::Value => "value",
            Self::Range => "range",
            Self::Order => "order",
        }
    }
}

impl std::fmt::Display for WriteRefusal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.token())
    }
}

impl std::error::Error for WriteRefusal {}

impl From<packed::WriteRefusal> for WriteRefusal {
    fn from(r: packed::WriteRefusal) -> Self {
        match r {
            packed::WriteRefusal::Missing => Self::Missing,
            packed::WriteRefusal::Unknown => Self::Unknown,
            packed::WriteRefusal::Value => Self::Value,
            packed::WriteRefusal::Range => Self::Range,
        }
    }
}

impl Frame {
    /// A body read under the tag its envelope's CH carries, refusing it for
    /// the first of core §7.7's checks that applies.
    ///
    /// # Errors
    /// The reader's refusal: [`ReadRefusal`].
    pub fn parse(tag: Tag, body: &[u8], layout: &Layout) -> Result<Frame, ReadRefusal> {
        if tag != layout.tag() {
            return Err(ReadRefusal::Tag);
        }
        let mut r = BitReader::new(body);
        let t = r.take(42)?;
        let time = if t == TIME_UNKNOWN {
            None
        } else {
            let ms = (t & 0x3ff) as u16;
            if ms >= 1000 {
                return Err(ReadRefusal::Code);
            }
            Some(Time {
                s: u32::try_from(t >> 10).expect("32 bits"),
                ms,
            })
        };
        let count = r.take(layout.count_bits())?;
        if count >= u64::from(layout.params.records) {
            return Err(ReadRefusal::Code);
        }
        let n = usize::try_from(count).expect("below 256") + 1;
        let mut records = Vec::with_capacity(n);
        let mut earliest = 0;
        for i in 0..n {
            let full = r.bit()?;
            let at = if i == 0 {
                None
            } else if r.bit()? {
                Some(At::Scheduled)
            } else {
                let o = r.take(layout.offset_bits().unwrap_or(0))?;
                if layout.span_ms().is_none_or(|span| o >= span) {
                    return Err(ReadRefusal::Code);
                }
                Some(At::Offset(o))
            };
            let (lo, hi) = layout.bounds(i, at);
            if hi < earliest {
                return Err(ReadRefusal::Order);
            }
            earliest = lo;
            let included: Vec<bool> = if full {
                vec![true; layout.fields.len()]
            } else {
                (0..layout.fields.len())
                    .map(|_| r.bit())
                    .collect::<Result<_, _>>()?
            };
            let mut slots = Vec::with_capacity(layout.fields.len());
            for (f, inc) in layout.fields.iter().zip(included) {
                if !inc {
                    slots.push(Slot::Omitted);
                } else if f.optional && !r.bit()? {
                    slots.push(Slot::Absent);
                } else {
                    let code = r.take(f.width)?;
                    f.check_code(code)?;
                    slots.push(Slot::Code(code));
                }
            }
            records.push(Record { full, at, slots });
        }
        r.end()?;
        Ok(Frame { time, records })
    }

    /// The body's bits, then the pad: what [`Frame::parse`] reads back.
    ///
    /// # Errors
    /// The writer's refusal: [`WriteRefusal`].
    pub fn write(&self, layout: &Layout) -> Result<packed::Record, WriteRefusal> {
        let n = self.records.len();
        if n == 0 || n > usize::from(layout.params.records) {
            return Err(WriteRefusal::Records);
        }
        let mut w = BitWriter::new();
        match self.time {
            None => w.push(TIME_UNKNOWN, 42),
            Some(t) if t.ms >= 1000 => return Err(WriteRefusal::Range),
            Some(t) => w.push((u64::from(t.s) << 10) | u64::from(t.ms), 42),
        }
        w.push(n as u64 - 1, layout.count_bits());
        let mut earliest = 0;
        for (i, rec) in self.records.iter().enumerate() {
            if rec.slots.len() != layout.fields.len() {
                return Err(WriteRefusal::Value);
            }
            w.bit(rec.full);
            match (i, rec.at) {
                (0, None) => {}
                (0, Some(_)) | (_, None) => return Err(WriteRefusal::Value),
                (_, Some(At::Scheduled)) => w.bit(true),
                (_, Some(At::Offset(o))) => {
                    if layout.span_ms().is_none_or(|span| o >= span) {
                        return Err(WriteRefusal::Range);
                    }
                    w.bit(false);
                    w.push(o, layout.offset_bits().unwrap_or(0));
                }
            }
            let (lo, hi) = layout.bounds(i, rec.at);
            if hi < earliest {
                return Err(WriteRefusal::Order);
            }
            earliest = lo;
            if !rec.full {
                for s in &rec.slots {
                    w.bit(!matches!(s, Slot::Omitted));
                }
            }
            for (f, s) in layout.fields.iter().zip(&rec.slots) {
                match s {
                    Slot::Omitted if rec.full => return Err(WriteRefusal::Missing),
                    Slot::Omitted => {}
                    Slot::Absent if !f.optional => return Err(WriteRefusal::Missing),
                    Slot::Absent => w.bit(false),
                    Slot::Code(c) => {
                        if (f.width < 64 && *c >> f.width != 0) || f.check_code(*c).is_err() {
                            return Err(WriteRefusal::Range);
                        }
                        if f.optional {
                            w.bit(true);
                        }
                        w.push(*c, f.width);
                    }
                }
            }
        }
        Ok(w.finish())
    }

    /// The put's payload: the frame's envelope, its tag the layout's, its
    /// stamp record 0's, then the body.
    ///
    /// # Errors
    /// [`Frame::write`]'s.
    pub fn envelope(&self, layout: &Layout, stamp: Stamp) -> Result<Vec<u8>, WriteRefusal> {
        let body = self.write(layout)?;
        Ok(Envelope::frame(layout.tag(), stamp, &body.bytes).write())
    }

    /// The frame as `spec/conformance/frame/bodies.json` spells a value:
    /// `time`, `{"s", "ms"}` or `null`, and `records`, each `full`, `at`
    /// (after record 0: `"scheduled"` or its offset), `values` (the fields
    /// included present, as §7.6's JSON reading spells them) and `absent`
    /// (those included absent, in field order).
    ///
    /// # Errors
    /// [`ReadRefusal::Code`] where a `time` field's instant lies past an
    /// `i64` of milliseconds under `epoch` ([`packed::ReadRefusal::Epoch`]),
    /// which no epoch within ±9.2 × 10^15 seconds gives.
    pub fn read_json(&self, layout: &Layout, epoch: Option<i64>) -> Result<Value, ReadRefusal> {
        let mut records = Vec::new();
        for rec in &self.records {
            let mut m = Map::new();
            m.insert("full".into(), rec.full.into());
            match rec.at {
                None => {}
                Some(At::Scheduled) => {
                    m.insert("at".into(), "scheduled".into());
                }
                Some(At::Offset(o)) => {
                    m.insert("at".into(), o.into());
                }
            }
            let mut values = Map::new();
            let mut absent = Vec::new();
            for (f, s) in layout.fields.iter().zip(&rec.slots) {
                match s {
                    Slot::Omitted => {}
                    Slot::Absent => absent.push(Value::from(f.name.clone())),
                    Slot::Code(c) => {
                        values.insert(f.name.clone(), f.value(*c, epoch)?);
                    }
                }
            }
            m.insert("values".into(), Value::Object(values));
            m.insert("absent".into(), Value::Array(absent));
            records.push(Value::Object(m));
        }
        let time = self
            .time
            .map_or(Value::Null, |t| json!({"s": t.s, "ms": t.ms}));
        Ok(json!({"time": time, "records": records}))
    }

    /// A frame from [`Frame::read_json`]'s spelling, each field's value
    /// coded as §7.6's writer codes it. A full record's optional field in
    /// neither `values` nor `absent` is absent.
    ///
    /// # Errors
    /// The writer's refusal: [`WriteRefusal`].
    pub fn from_json(
        v: &Value,
        layout: &Layout,
        epoch: Option<i64>,
    ) -> Result<Frame, WriteRefusal> {
        let time = match v.get("time") {
            Some(Value::Null) => None,
            Some(t) => {
                let s = t
                    .get("s")
                    .and_then(Value::as_u64)
                    .ok_or(WriteRefusal::Value)?;
                let ms = t
                    .get("ms")
                    .and_then(Value::as_u64)
                    .ok_or(WriteRefusal::Value)?;
                Some(Time {
                    s: u32::try_from(s).map_err(|_| WriteRefusal::Range)?,
                    ms: u16::try_from(ms)
                        .ok()
                        .filter(|ms| *ms < 1000)
                        .ok_or(WriteRefusal::Range)?,
                })
            }
            None => return Err(WriteRefusal::Value),
        };
        let recs = v
            .get("records")
            .and_then(Value::as_array)
            .ok_or(WriteRefusal::Value)?;
        if recs.is_empty() || recs.len() > usize::from(layout.params.records) {
            return Err(WriteRefusal::Records);
        }
        let mut records = Vec::with_capacity(recs.len());
        for r in recs {
            let full = r
                .get("full")
                .and_then(Value::as_bool)
                .ok_or(WriteRefusal::Value)?;
            let at = match r.get("at") {
                None => None,
                Some(Value::String(s)) if s == "scheduled" => Some(At::Scheduled),
                Some(x) => Some(At::Offset(x.as_u64().ok_or(WriteRefusal::Value)?)),
            };
            let empty = Map::new();
            let values = match r.get("values") {
                None => &empty,
                Some(x) => x.as_object().ok_or(WriteRefusal::Value)?,
            };
            let absent: Vec<&str> = match r.get("absent") {
                None => Vec::new(),
                Some(x) => x
                    .as_array()
                    .ok_or(WriteRefusal::Value)?
                    .iter()
                    .map(|n| n.as_str().ok_or(WriteRefusal::Value))
                    .collect::<Result<_, _>>()?,
            };
            let known = |n: &str| layout.fields.iter().any(|f| f.name == n);
            if values.keys().any(|k| !known(k)) || absent.iter().any(|k| !known(k)) {
                return Err(WriteRefusal::Unknown);
            }
            let mut slots = Vec::with_capacity(layout.fields.len());
            for f in &layout.fields {
                let slot = match values.get(&f.name) {
                    // A counter's count is written modulo 2^width (§7.6).
                    Some(x) => Slot::Code(f.code(x, epoch)? & packed::top_code(f.width)),
                    None if absent.contains(&f.name.as_str()) || (full && f.optional) => {
                        if !f.optional {
                            return Err(WriteRefusal::Missing);
                        }
                        Slot::Absent
                    }
                    None if full => return Err(WriteRefusal::Missing),
                    None => Slot::Omitted,
                };
                slots.push(slot);
            }
            records.push(Record { full, at, slots });
        }
        Ok(Frame { time, records })
    }
}

/// What a frame follows, in the run a reader holds (core §7.7, "The
/// frames it expects").
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Follows {
    /// The reader held no run: its first frame of the key.
    First,
    /// The seq after the last record it read, in the run it holds.
    Continues,
    /// A newer seq by core §4.3's serial order: `lost` records lost.
    Gap { lost: u32 },
    /// Any other seq of the run it holds, one exactly 2^23 ahead included:
    /// the key restarted under a run it had used, which §4.3 forbids.
    Rewind,
    /// A run other than the one it holds.
    Run,
}

impl Follows {
    /// `spec/conformance/frame/apply.json`'s token.
    #[must_use]
    pub const fn token(self) -> &'static str {
        match self {
            Self::First => "first",
            Self::Continues => "continues",
            Self::Gap { .. } => "gap",
            Self::Rewind => "rewind",
            Self::Run => "run",
        }
    }

    /// Whether the reader turned every field `unknown` at this frame.
    #[must_use]
    pub const fn resets(self) -> bool {
        !matches!(self, Self::Continues)
    }
}

/// Why a field is `unknown` (core §7.7's table).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Unknown {
    /// Not included since the reader's first frame of the key.
    First,
    /// Not included since a frame of a run the reader did not hold.
    Run,
    /// Not included since records were lost.
    Gap,
    /// Not included since a rewind.
    Rewind,
    /// `max_records` records after the record that included it.
    Deadband,
    /// Omitted under a deadband after an inclusion that was no value: the
    /// field absent, or a reserved code.
    NoValue,
}

impl Unknown {
    /// `spec/conformance/frame/apply.json`'s token.
    #[must_use]
    pub const fn token(self) -> &'static str {
        match self {
            Self::First => "first",
            Self::Run => "run",
            Self::Gap => "gap",
            Self::Rewind => "rewind",
            Self::Deadband => "deadband",
            Self::NoValue => "no_value",
        }
    }

    /// Why, in words, for a person (observer §7.6).
    #[must_use]
    pub const fn words(self) -> &'static str {
        match self {
            Self::First => "not included since the first frame read",
            Self::Run => "not included since a run not held",
            Self::Gap => "not included since records were lost",
            Self::Rewind => "not included since a rewind",
            Self::Deadband => "max_records passed since it was included",
            Self::NoValue => "omitted under a deadband after no value",
        }
    }

    fn of(follows: Follows) -> Self {
        match follows {
            Follows::First => Self::First,
            Follows::Run => Self::Run,
            Follows::Gap { .. } => Self::Gap,
            Follows::Rewind | Follows::Continues => Self::Rewind,
        }
    }
}

/// A field's state in one record (core §7.7, "The reader's fields").
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FieldState {
    /// Its value, at the record's time: included in this record, or
    /// omitted with no deadband and nothing lost since it was included.
    Known(u64),
    /// The last value included, give or take its deadband's `delta`:
    /// omitted under a deadband, fewer than `max_records` records after a
    /// record that included a value.
    Within(u64),
    /// Nothing, with why: never its last value, never 0.
    Unknown(Unknown),
    /// Nothing: the owner wrote it absent.
    Absent,
}

impl FieldState {
    /// `spec/conformance/frame/apply.json`'s token.
    #[must_use]
    pub const fn token(self) -> &'static str {
        match self {
            Self::Known(_) => "known",
            Self::Within(_) => "within",
            Self::Unknown(_) => "unknown",
            Self::Absent => "absent",
        }
    }

    /// The code it holds: a `known` field's value, or a `within` field's
    /// last value (give or take its delta). `None` for `unknown` and
    /// `absent`, which hold none.
    #[must_use]
    pub const fn code(self) -> Option<u64> {
        match self {
            Self::Known(c) | Self::Within(c) => Some(c),
            Self::Unknown(_) | Self::Absent => None,
        }
    }
}

/// A record's time, as a reader reads it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum When {
    /// Measured: record 0's on the owner's wall clock, a later record's as
    /// its offset on the owner's monotonic clock from record 0.
    Measured,
    /// `step_ms` × its index after record 0, within `jitter_ms`: an
    /// instant and a bound, never a measurement (observer §7.6).
    Scheduled { jitter_ms: u32 },
    /// Record 0 of a frame whose time is unknown: the owner's clock was.
    Unknown,
}

impl When {
    /// `spec/conformance/frame/apply.json`'s token.
    #[must_use]
    pub const fn token(self) -> &'static str {
        match self {
            Self::Measured => "measured",
            Self::Scheduled { .. } => "scheduled",
            Self::Unknown => "unknown",
        }
    }
}

/// One record of a frame, read.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecordRead {
    /// Record 0's seq plus its index, modulo 2^24.
    pub seq: Seq,
    pub when: When,
    /// After record 0: its offset, or `step_ms` × its index when
    /// scheduled. 0 for record 0.
    pub after_ms: u64,
    /// Its instant in milliseconds from the deployment epoch: record 0's
    /// time plus `after_ms`, `None` when that time is unknown.
    pub at_ms: Option<u64>,
    /// One per field, in field order.
    pub fields: Vec<FieldState>,
}

/// A frame as a [`Reader`] read it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Applied {
    /// The envelope's: record 0's seq.
    pub stamp: Stamp,
    pub follows: Follows,
    pub records: Vec<RecordRead>,
    /// Record 0's seq of each frame of the run it holds that its layout
    /// refused since the frame before this one ([`Reader::refuse`]), in
    /// arrival order. Their records are among a gap's `lost`: a refused
    /// frame's body is not read, so how many records it held is not known
    /// to the reader, and only the next frame's seq tells it.
    pub refused: Vec<Seq>,
}

impl Applied {
    /// The frame as `spec/conformance/frame/apply.json` spells a reading:
    /// `run`, `seq`, `follows` (and `lost` for a gap), then each record's
    /// `seq`, `time`, `after_ms` and `jitter_ms` where they apply, `at`
    /// (`{s, ms}`) where its instant is known, and `fields` by name, each
    /// `{state}` with its `value` (§7.6's JSON reading), a `within`
    /// field's `delta`, and an `unknown` field's `why`.
    ///
    /// # Errors
    /// [`Frame::read_json`]'s: a `time` field's instant past an `i64` of
    /// milliseconds under `epoch`.
    pub fn reading(&self, layout: &Layout, epoch: Option<i64>) -> Result<Value, ReadRefusal> {
        let mut records = Vec::with_capacity(self.records.len());
        for (index, rec) in self.records.iter().enumerate() {
            let mut m = Map::new();
            m.insert("seq".into(), rec.seq.get().into());
            m.insert("time".into(), rec.when.token().into());
            if index > 0 {
                m.insert("after_ms".into(), rec.after_ms.into());
            }
            if let When::Scheduled { jitter_ms } = rec.when {
                m.insert("jitter_ms".into(), jitter_ms.into());
            }
            if let Some(at) = rec.at_ms {
                m.insert("at".into(), json!({"s": at / 1000, "ms": at % 1000}));
            }
            let mut fields = Map::new();
            for (i, (f, s)) in layout.fields.iter().zip(&rec.fields).enumerate() {
                let mut v = Map::new();
                v.insert("state".into(), s.token().into());
                match s {
                    FieldState::Known(c) => {
                        v.insert("value".into(), f.value(*c, epoch)?);
                    }
                    FieldState::Within(c) => {
                        v.insert("value".into(), f.value(*c, epoch)?);
                        if let Some(d) = layout.deadband(i) {
                            v.insert("delta".into(), d.delta.to_string().into());
                        }
                    }
                    FieldState::Unknown(why) => {
                        v.insert("why".into(), why.token().into());
                    }
                    FieldState::Absent => {}
                }
                fields.insert(f.name.clone(), Value::Object(v));
            }
            m.insert("fields".into(), Value::Object(fields));
            records.push(Value::Object(m));
        }
        let mut out = Map::new();
        out.insert("run".into(), self.stamp.run.hex().into());
        out.insert("seq".into(), self.stamp.seq.get().into());
        out.insert("follows".into(), self.follows.token().into());
        if let Follows::Gap { lost } = self.follows {
            out.insert("lost".into(), lost.into());
        }
        out.insert("records".into(), Value::Array(records));
        Ok(Value::Object(out))
    }
}

/// What a field's last inclusion was, since the reader last reset.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Last {
    /// Not included since the reset.
    Never,
    Absent,
    /// A value's code.
    Value(u64),
    /// A reserved code: present, and no value.
    Reserved(u64),
}

#[derive(Debug, Clone, Copy)]
struct Track {
    last: Last,
    /// Records since its last inclusion.
    since: u32,
}

#[derive(Debug, Clone)]
struct Held {
    run: Run,
    /// The seq after the last record read.
    next: Seq,
    /// Why every field turned unknown at the last reset.
    reset: Unknown,
    /// The layout's hash: another layout is read as another run.
    layout: [u8; 32],
    tracks: Vec<Track>,
    /// Frames of this run its layout refused since the last one read.
    refused: Vec<Seq>,
}

/// One key's reader (core §7.7, "The reader's fields"): what it holds of
/// the key's frames, in the run it holds. A consumer or a tool keeps one
/// per frame key and hands it each frame in the order it arrived
/// ([`Reader::apply`]). A frame refused by its layout is never applied: its
/// body is not read, so neither its fields nor how many records it held
/// are known, and the next frame's seq reads its records as lost, a gap
/// (core §7.7, "The frames it expects": a seq newer than the one expected
/// follows records lost, whatever lost them; `spec/conformance/frame/
/// apply.json` reads a gap so). [`Reader::refuse`] notes it, so the next
/// frame says which of its lost records were refused ([`Applied::refused`]).
#[derive(Debug, Clone, Default)]
pub struct Reader {
    held: Option<Held>,
}

impl Reader {
    /// A reader that holds nothing: its first frame is [`Follows::First`].
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// The run it holds, and the seq it expects next.
    #[must_use]
    pub fn expects(&self) -> Option<Stamp> {
        self.held.as_ref().map(|h| Stamp {
            run: h.run,
            seq: h.next,
        })
    }

    /// Notes a frame at `stamp` whose layout refused it (core §7.7's
    /// refusals). It is never applied: the reader still expects the seq
    /// after the last record it read, so the next frame follows a gap
    /// counting the refused frame's records, and lists its seq in
    /// [`Applied::refused`]. A refused frame of a run it does not hold is
    /// forgotten: the next frame of that run resets every field anyway.
    pub fn refuse(&mut self, stamp: Stamp) {
        if let Some(h) = &mut self.held
            && h.run == stamp.run
            && h.refused.len() < usize::from(u16::MAX)
        {
            h.refused.push(stamp.seq);
        }
    }

    /// What a frame at `stamp` would follow, read under `layout`.
    #[must_use]
    pub fn follows(&self, layout: &Layout, stamp: Stamp) -> Follows {
        let Some(h) = &self.held else {
            return Follows::First;
        };
        if h.run != stamp.run || h.layout != layout.sha256 {
            return Follows::Run;
        }
        const SPACE: u32 = 1 << 24;
        const HALF: u32 = 1 << 23;
        let d = stamp.seq.get().wrapping_sub(h.next.get()) & (SPACE - 1);
        match d {
            0 => Follows::Continues,
            d if d < HALF => Follows::Gap { lost: d },
            _ => Follows::Rewind,
        }
    }

    /// Reads a frame of the key, `stamp` its envelope's, `frame` its body
    /// read under `layout` ([`Frame::parse`]): every field turns
    /// `unknown` at a gap, a rewind and a run not held (its first frame
    /// included), then each record is read in turn, a field `known` or
    /// `absent` where included, carried where omitted with no deadband,
    /// `within` under its deadband until `max_records` records after it
    /// was included with a value, and `unknown` past it.
    ///
    /// A reader holds one layout: a frame under another (another revision
    /// of the resource) is read as one of a run it does not hold.
    pub fn apply(&mut self, layout: &Layout, stamp: Stamp, frame: &Frame) -> Applied {
        let follows = self.follows(layout, stamp);
        let n = layout.fields.len();
        // A frame refused in the run held is among what this one follows.
        let refused = self
            .held
            .as_mut()
            .filter(|h| h.run == stamp.run)
            .map(|h| std::mem::take(&mut h.refused))
            .unwrap_or_default();
        let held = match (&mut self.held, follows.resets()) {
            (Some(h), false) => h,
            (slot, _) => slot.insert(Held {
                run: stamp.run,
                next: stamp.seq,
                reset: Unknown::of(follows),
                layout: layout.sha256,
                tracks: vec![
                    Track {
                        last: Last::Never,
                        since: 0,
                    };
                    n
                ],
                refused: Vec::new(),
            }),
        };
        let base = frame.time.map(|t| u64::from(t.s) * 1000 + u64::from(t.ms));
        let mut records = Vec::with_capacity(frame.records.len());
        for (i, rec) in frame.records.iter().enumerate() {
            let seq = Seq::new((stamp.seq.get() + i as u32) & Seq::MAX.get()).expect("24 bits");
            let (when, after_ms) = match rec.at {
                None if base.is_none() => (When::Unknown, 0),
                None => (When::Measured, 0),
                Some(At::Offset(o)) => (When::Measured, o),
                Some(At::Scheduled) => (
                    When::Scheduled {
                        jitter_ms: layout.params.jitter_ms.unwrap_or(0),
                    },
                    u64::from(layout.params.step_ms.unwrap_or(0)) * i as u64,
                ),
            };
            let mut fields = Vec::with_capacity(n);
            for (k, (f, slot)) in layout.fields.iter().zip(&rec.slots).enumerate() {
                let t = &mut held.tracks[k];
                let state = match slot {
                    Slot::Code(c) => {
                        *t = Track {
                            last: if f.reserved_name(*c).is_some() {
                                Last::Reserved(*c)
                            } else {
                                Last::Value(*c)
                            },
                            since: 0,
                        };
                        FieldState::Known(*c)
                    }
                    Slot::Absent => {
                        *t = Track {
                            last: Last::Absent,
                            since: 0,
                        };
                        FieldState::Absent
                    }
                    Slot::Omitted => {
                        t.since = t.since.saturating_add(1);
                        match (t.last, layout.deadband(k)) {
                            (Last::Never, _) => FieldState::Unknown(held.reset),
                            (Last::Absent, None) => FieldState::Absent,
                            (Last::Value(c) | Last::Reserved(c), None) => FieldState::Known(c),
                            (Last::Absent | Last::Reserved(_), Some(_)) => {
                                FieldState::Unknown(Unknown::NoValue)
                            }
                            (Last::Value(_), Some(d)) if t.since >= d.max_records => {
                                FieldState::Unknown(Unknown::Deadband)
                            }
                            (Last::Value(c), Some(_)) => FieldState::Within(c),
                        }
                    }
                };
                fields.push(state);
            }
            records.push(RecordRead {
                seq,
                when,
                after_ms,
                at_ms: base.map(|b| b + after_ms),
                fields,
            });
        }
        let count = u32::try_from(frame.records.len()).expect("at most 256");
        held.next = Seq::new((stamp.seq.get() + count) & Seq::MAX.get()).expect("24 bits");
        Applied {
            stamp,
            follows,
            records,
            refused,
        }
    }
}

// ── The owner's frames (core §7.7, "The owner's frames"; #981's K3) ─────

impl Time {
    /// The time of an instant `t`, in milliseconds since the Unix epoch,
    /// under the deployment epoch `epoch` in seconds: `None` for one before
    /// the epoch or 2^32 seconds after it, which the owner writes as
    /// unknown.
    #[must_use]
    pub fn from_unix_ms(t: i128, epoch: i64) -> Option<Time> {
        let delta = t - 1000 * i128::from(epoch);
        let (s, ms) = (delta.div_euclid(1000), delta.rem_euclid(1000));
        Some(Time {
            s: u32::try_from(s).ok()?,
            ms: u16::try_from(ms).expect("below 1000"),
        })
    }

    /// The instant in milliseconds since the Unix epoch, under `epoch`.
    #[must_use]
    pub fn unix_ms(self, epoch: i64) -> i128 {
        1000 * (i128::from(epoch) + i128::from(self.s)) + i128::from(self.ms)
    }
}

impl Layout {
    /// A §7.6 record of the frame's record type read into a full record's
    /// slots: each field absent or its code. The record's own tag is not
    /// asked: its bytes are the type's.
    ///
    /// # Errors
    /// [`ReadRefusal::Length`], [`ReadRefusal::Code`] or
    /// [`ReadRefusal::Pad`], as §7.6's reader refuses the record.
    pub fn slots_of(&self, record: &[u8]) -> Result<Vec<Slot>, ReadRefusal> {
        let mut r = BitReader::new(record);
        let mut slots = Vec::with_capacity(self.fields.len());
        for f in &self.fields {
            if f.optional && !r.bit()? {
                slots.push(Slot::Absent);
                continue;
            }
            let code = r.take(f.width)?;
            f.check_code(code)?;
            slots.push(Slot::Code(code));
        }
        r.end()?;
        Ok(slots)
    }

    /// A record in §7.6's JSON reading, a field's name to its value, read
    /// into a full record's slots: an optional field left out or `null` is
    /// absent. A `time` field counts from `epoch`.
    ///
    /// # Errors
    /// [`WriteRefusal`]: `value` for a value that is no object, `unknown`
    /// for a name the type does not have, `missing` for a field that is not
    /// optional left out, and §7.6's refusals of a value.
    pub fn slots_of_json(&self, v: &Value, epoch: Option<i64>) -> Result<Vec<Slot>, WriteRefusal> {
        let m = v.as_object().ok_or(WriteRefusal::Value)?;
        if m.keys().any(|k| !self.fields.iter().any(|f| &f.name == k)) {
            return Err(WriteRefusal::Unknown);
        }
        self.fields
            .iter()
            .map(|f| match m.get(&f.name) {
                None | Some(Value::Null) if f.optional => Ok(Slot::Absent),
                None => Err(WriteRefusal::Missing),
                Some(x) => Ok(Slot::Code(f.code(x, epoch)? & packed::top_code(f.width))),
            })
            .collect()
    }

    /// The bits of the largest frame an owner writes (link.v1 §2.8's
    /// `frame_over_batch`, 0.16, F-470): `records` records, every optional
    /// field present, and every record after record 0 measured, with its
    /// offset. As many of them as `keyframe_every` allows are delta records
    /// that include every field, each a bit per field longer than a full
    /// record (core §7.7): a full record comes at least once every
    /// `keyframe_every`, so `records` − ⌊`records` / `keyframe_every`⌋ of
    /// them, none at 1. The rest are full.
    #[must_use]
    pub fn largest_body_bits(&self) -> u64 {
        let mut fields = 0u64;
        let mut record = 1u64;
        for f in &self.fields {
            fields += 1;
            record += u64::from(f.width) + u64::from(f.optional);
        }
        let n = u64::from(self.params.records);
        let deltas = n - n / u64::from(self.params.keyframe_every.max(1));
        let later = n.saturating_sub(1) * (1 + u64::from(self.offset_bits().unwrap_or(0)));
        42 + u64::from(self.count_bits()) + n * record + deltas * fields + later
    }

    /// The payload of that largest frame, in bytes: its body after an
    /// envelope of a 32-bit run, 8 bytes (core §7.5).
    #[must_use]
    pub fn largest_payload_bytes(&self) -> u64 {
        8 + self.largest_body_bits().div_ceil(8)
    }

    /// The value of a field's code a deadband weighs: an `int`'s integer,
    /// a `fixed`'s decimal, a `counter`'s count. `None` for a reserved code,
    /// which holds no value, and for a field that takes no deadband.
    fn weighed(&self, index: usize, code: u64) -> Option<Decimal> {
        let f = &self.fields[index];
        if f.reserved_name(code).is_some() {
            return None;
        }
        match &f.kind {
            FieldKind::Int {
                minimum, maximum, ..
            } => packed::int_value(code, *minimum, *maximum)
                .ok()
                .map(Decimal::of_i128),
            FieldKind::Fixed {
                minimum,
                resolution,
                values,
                ..
            } => packed::fixed_value(code, minimum, resolution, *values).ok(),
            FieldKind::Counter => Some(Decimal::of_i128(i128::from(code))),
            _ => None,
        }
    }
}

/// One record an owner measured: each field absent or its code
/// ([`Slot::Absent`], [`Slot::Code`]), when it measured it on its
/// monotonic clock (`at`), and its wall clock's time then, `None` while
/// its clock is unknown. Only record 0's wall time is written: a later
/// record's time is its offset on the monotonic clock (core §7.7,
/// "Times").
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Measured {
    pub slots: Vec<Slot>,
    pub at: crate::timing::Mono,
    pub wall: Option<Time>,
}

/// The records of one frame, in the order they were measured: what a
/// [`Framer`] hands its owner to write ([`Framer::encode`]).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Batch(Vec<Measured>);

impl Batch {
    /// How many records it holds, and so how many seqs its frame takes
    /// (core §4.3).
    #[must_use]
    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> u32 {
        u32::try_from(self.0.len()).expect("at most 256")
    }

    /// Its records.
    #[must_use]
    pub fn records(&self) -> &[Measured] {
        &self.0
    }
}

/// What the key's readers hold after the last frame an owner sent, in the
/// run it was sent under: what the next delta record is written against.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Sent {
    run: Run,
    /// The last record sent, every field absent or a code.
    prev: Vec<Slot>,
    /// Each field as the last record that included it held it.
    included: Vec<Slot>,
    /// Records since each field was last included.
    since: Vec<u32>,
    /// Records since the last full record.
    since_full: u32,
}

/// A batch written as a frame under a run ([`Framer::encode`]), and what
/// the key's readers hold once it is sent: [`Framer::sent`] keeps that.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Encoded {
    pub frame: Frame,
    next: Sent,
}

/// One frame key's writer (core §7.7, "The owner's frames"): a pure state
/// machine an owner hands each record it measures, in the order it
/// measured them ([`Framer::push`]), which says when a frame is complete
/// and writes it ([`Framer::encode`]). The owner takes the frame's seqs,
/// one per record (§4.3), puts it, and tells the framer it was sent
/// ([`Framer::sent`]). A frame never sent leaves nothing behind: the
/// next is written against the last one sent.
///
/// - **Frames.** A frame holds up to `records` records. A record that
///   would lie at or past the open frame's span ends it, and opens the
///   next as its record 0. [`Framer::flush`] ends the open frame early.
/// - **Full records.** Record 0 of the first frame written under a run is
///   full, and so is a record `keyframe_every` records after the last full
///   one. A delta record that would include every field is written full.
/// - **Delta records** include each field with no deadband whose code or
///   presence differs from the record before. A field with a deadband is
///   omitted while it holds a value within `delta` of the value last
///   included, as that inclusion did, and fewer than `max_records`
///   records passed since; else it is included.
/// - **Times.** Record 0 carries its wall time. A later record is
///   scheduled when its offset from record 0, on the monotonic clock, lies
///   within `jitter_ms` of `step_ms` × its index, and carries its offset,
///   in whole milliseconds, otherwise.
#[derive(Debug, Clone)]
pub struct Framer {
    layout: Layout,
    open: Vec<Measured>,
    last_at: Option<crate::timing::Mono>,
    sent: Option<Sent>,
}

impl Framer {
    /// A framer of `layout`'s frames that has sent none.
    #[must_use]
    pub fn new(layout: Layout) -> Self {
        Self {
            layout,
            open: Vec::new(),
            last_at: None,
            sent: None,
        }
    }

    /// Its layout.
    #[must_use]
    pub fn layout(&self) -> &Layout {
        &self.layout
    }

    /// How many records the open frame holds.
    #[must_use]
    pub fn open(&self) -> usize {
        self.open.len()
    }

    /// The run the last frame sent was written under.
    #[must_use]
    pub fn run(&self) -> Option<Run> {
        self.sent.as_ref().map(|s| s.run)
    }

    /// Takes a measured record, refused before anything is kept: a slot
    /// omitted, one per field missing, a field that is not optional
    /// written absent, a code its field does not use, or a record measured
    /// before the last one.
    ///
    /// Returns the frame now complete, if one is: the open frame, when
    /// this record lies at or past its span (this record then opens the
    /// next frame), or the open frame with this record, when that holds
    /// `records` records. A one-record frame is complete at once.
    ///
    /// # Errors
    /// [`WriteRefusal`]: `value` for a slot omitted or the wrong count,
    /// `missing`, `range`, `order`.
    pub fn push(&mut self, m: Measured) -> Result<Option<Batch>, WriteRefusal> {
        if m.slots.len() != self.layout.fields.len() {
            return Err(WriteRefusal::Value);
        }
        for (f, s) in self.layout.fields.iter().zip(&m.slots) {
            match s {
                Slot::Omitted => return Err(WriteRefusal::Value),
                Slot::Absent if !f.optional => return Err(WriteRefusal::Missing),
                Slot::Absent => {}
                Slot::Code(c) => {
                    if (f.width < 64 && *c >> f.width != 0) || f.check_code(*c).is_err() {
                        return Err(WriteRefusal::Range);
                    }
                }
            }
        }
        if self.last_at.is_some_and(|l| m.at < l) {
            return Err(WriteRefusal::Order);
        }
        self.last_at = Some(m.at);
        let past_span = match (self.open.first(), self.layout.params.span_ms()) {
            (Some(first), Some(span)) => {
                m.at.since_origin().saturating_sub(first.at.since_origin())
                    >= Duration::from_millis(span)
            }
            _ => false,
        };
        let ended = if past_span {
            Some(Batch(std::mem::take(&mut self.open)))
        } else {
            None
        };
        self.open.push(m);
        if ended.is_some() {
            return Ok(ended);
        }
        if self.open.len() >= usize::from(self.layout.params.records) {
            return Ok(Some(Batch(std::mem::take(&mut self.open))));
        }
        Ok(None)
    }

    /// Ends the open frame now, if it holds a record: an owner that puts
    /// what it measured so far, or stops.
    pub fn flush(&mut self) -> Option<Batch> {
        (!self.open.is_empty()).then(|| Batch(std::mem::take(&mut self.open)))
    }

    /// `batch` written as a frame under `run`: record 0 full when the last
    /// frame sent was another run's, or none was; each later record's time
    /// and fields as [`Framer`] says.
    #[must_use]
    pub fn encode(&self, batch: &Batch, run: Run) -> Encoded {
        let l = &self.layout;
        let n = l.fields.len();
        let mut state = self.sent.clone().filter(|s| s.run == run);
        let origin = batch.0[0].at.since_origin();
        let mut records = Vec::with_capacity(batch.0.len());
        for (i, m) in batch.0.iter().enumerate() {
            let at = (i > 0).then(|| {
                let off = m.at.since_origin().saturating_sub(origin);
                let scheduled = l.params.step_ms.is_some_and(|step| {
                    let instant = Duration::from_millis(u64::from(step) * i as u64);
                    let jitter = Duration::from_millis(u64::from(l.params.jitter_ms.unwrap_or(0)));
                    off.abs_diff(instant) <= jitter
                });
                if scheduled {
                    At::Scheduled
                } else {
                    At::Offset(u64::try_from(off.as_millis()).unwrap_or(u64::MAX))
                }
            });
            let (full, slots, next) = match state {
                None => (true, m.slots.clone(), None),
                Some(ref s) => {
                    let include: Vec<bool> =
                        (0..n).map(|k| self.includes(s, k, m.slots[k])).collect();
                    let full =
                        s.since_full + 1 >= l.params.keyframe_every || include.iter().all(|x| *x);
                    let slots = if full {
                        m.slots.clone()
                    } else {
                        m.slots
                            .iter()
                            .zip(&include)
                            .map(|(slot, inc)| if *inc { *slot } else { Slot::Omitted })
                            .collect()
                    };
                    (full, slots, Some(s))
                }
            };
            let mut s = next.cloned().unwrap_or(Sent {
                run,
                prev: m.slots.clone(),
                included: m.slots.clone(),
                since: vec![0; n],
                since_full: 0,
            });
            for (k, slot) in slots.iter().enumerate() {
                if matches!(slot, Slot::Omitted) {
                    s.since[k] = s.since[k].saturating_add(1);
                } else {
                    s.included[k] = *slot;
                    s.since[k] = 0;
                }
            }
            s.since_full = if full { 0 } else { s.since_full + 1 };
            s.prev = m.slots.clone();
            state = Some(s);
            records.push(Record { full, at, slots });
        }
        Encoded {
            frame: Frame {
                time: batch.0[0].wall,
                records,
            },
            next: state.expect("a record at least"),
        }
    }

    /// Whether a delta record after `s` includes field `k`, now `slot`.
    fn includes(&self, s: &Sent, k: usize, slot: Slot) -> bool {
        let Some(d) = self.layout.deadband(k) else {
            return slot != s.prev[k];
        };
        let (Slot::Code(now), Slot::Code(then)) = (slot, s.included[k]) else {
            return true;
        };
        if s.since[k] + 1 >= d.max_records {
            return true;
        }
        match (self.layout.weighed(k, now), self.layout.weighed(k, then)) {
            (Some(a), Some(b)) => a.distance(&b) > d.delta,
            _ => true,
        }
    }

    /// `e` was sent: the next frame is written against it.
    pub fn sent(&mut self, e: Encoded) {
        self.sent = Some(e.next);
    }
}

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

    fn link() -> packed::Layout {
        let doc = json!({"types": {"Link": {"fields": [
            {"name": "dropped", "type": "counter", "width": 32, "optional": true},
            {"name": "queue_depth", "type": "int", "minimum": 0, "maximum": 1022,
             "reserved": ["out_of_range"], "optional": true},
            {"name": "duty_available", "type": "int", "minimum": 0, "maximum": 1, "optional": true},
            {"name": "duty_wait_ms", "type": "fixed", "minimum": "0", "maximum": "3600000",
             "resolution": "100", "reserved": ["out_of_range"], "optional": true}
        ]}}});
        packed::layout_in(&doc, "Link").expect("a layout")
    }

    fn one() -> FrameParams {
        FrameParams::resolve(&FrameSpec::default()).expect("the defaults")
    }

    /// zenoh-modem's four link readings, one record a frame (#981's §3):
    /// a 63-bit record, so a keyframe's body is 14 bytes and its payload 20.
    #[test]
    fn zenoh_modems_link_frame() {
        let l = Layout::of(&link(), &one()).expect("a frame layout");
        assert_eq!(
            (l.count_bits(), l.offset_bits(), l.span_ms()),
            (0, None, None)
        );
        let all = json!({"time": {"s": 3600, "ms": 250}, "records": [{"full": true,
            "values": {"dropped": 7, "queue_depth": 12, "duty_available": 1, "duty_wait_ms": "1500"},
            "absent": []}]});
        let f = Frame::from_json(&all, &l, None).expect("a frame");
        let body = f.write(&l).expect("written");
        assert_eq!((body.bits, body.bytes.len()), (106, 14));
        let stamp = Stamp {
            run: crate::wire::Run::U16(1),
            seq: crate::wire::Seq::new(12).unwrap(),
        };
        let put = f.envelope(&l, stamp).expect("an envelope");
        assert_eq!(put.len(), 20);
        let e = Envelope::parse(&put).expect("parsed");
        assert!(e.is_frame());
        let back = Frame::parse(e.tag, e.record, &l).expect("read back");
        assert_eq!(back, f);
        assert_eq!(back.read_json(&l, None).unwrap(), all);
        // A delta record with the queue and the wait changed: 10 bytes.
        let d = json!({"time": {"s": 3660, "ms": 0}, "records": [{"full": false,
            "values": {"queue_depth": 30, "duty_wait_ms": "2500"}, "absent": []}]});
        let body = Frame::from_json(&d, &l, None).unwrap().write(&l).unwrap();
        assert_eq!((body.bits, body.bytes.len()), (75, 10));
        // Nothing changed: 6 bytes.
        let d = json!({"time": {"s": 3720, "ms": 0}, "records": [{"full": false}]});
        let body = Frame::from_json(&d, &l, None).unwrap().write(&l).unwrap();
        assert_eq!((body.bits, body.bytes.len()), (47, 6));
    }

    #[test]
    fn the_table_resolves_or_names_each_finding() {
        let p = one();
        assert_eq!(
            (p.records, p.step_ms, p.jitter_ms, p.keyframe_every),
            (1, None, None, 1)
        );
        let p = FrameParams::resolve(&FrameSpec {
            records: Some(3),
            step_ms: Some(10_000),
            ..FrameSpec::default()
        })
        .unwrap();
        assert_eq!((p.jitter_ms, p.span_ms()), (Some(0), Some(30_000)));
        let bad = FrameParams::resolve(&FrameSpec {
            records: Some(0),
            jitter_ms: Some(1),
            keyframe_every: Some(0),
            ..FrameSpec::default()
        })
        .unwrap_err();
        assert_eq!(bad.len(), 3, "{bad:?}");
        assert_eq!(FrameParams::from_canonical(&p.canonical()), Ok(p));
    }

    /// A frame its layout refuses is never applied (core §7.7): the next
    /// frame follows a gap that counts its records, and names it.
    #[test]
    fn a_refused_frame_reads_as_lost_and_is_named() {
        let l = Layout::of(&link(), &one()).unwrap();
        let stamp = |seq| Stamp {
            run: crate::wire::Run::U16(1),
            seq: Seq::new(seq).unwrap(),
        };
        let full = Frame::from_json(
            &json!({"time": {"s": 1, "ms": 0}, "records": [{"full": true, "values": {"dropped": 1}}]}),
            &l,
            None,
        )
        .unwrap();
        let mut r = Reader::new();
        assert!(r.apply(&l, stamp(10), &full).refused.is_empty());
        r.refuse(stamp(11));
        let next = r.apply(&l, stamp(12), &full);
        assert_eq!(next.follows, Follows::Gap { lost: 1 });
        assert_eq!(next.refused, [Seq::new(11).unwrap()]);
        // Noted once: the frame after reads none.
        assert!(r.apply(&l, stamp(13), &full).refused.is_empty());
        // Another run's refusal is forgotten.
        r.refuse(Stamp {
            run: crate::wire::Run::U16(2),
            seq: Seq::new(0).unwrap(),
        });
        assert!(r.apply(&l, stamp(14), &full).refused.is_empty());
    }

    fn sample() -> packed::Layout {
        let doc = json!({"types": {"Sample": {"fields": [
            {"name": "temperature", "type": "fixed", "minimum": "-40", "maximum": "85",
             "resolution": "0.5"},
            {"name": "rssi", "type": "int", "minimum": -140, "maximum": -20,
             "reserved": ["not_available"]},
            {"name": "link_up", "type": "bool"},
            {"name": "mode", "type": "enum", "values": ["idle", "rx", "tx"], "escape": true,
             "optional": true}
        ]}}});
        packed::layout_in(&doc, "Sample").expect("a layout")
    }

    fn params(spec: FrameSpec) -> FrameParams {
        FrameParams::resolve(&spec).expect("a table")
    }

    fn measured(l: &Layout, v: Value, ms: u64) -> Measured {
        Measured {
            slots: l.slots_of_json(&v, None).expect("slots"),
            at: crate::timing::Mono::after(Duration::from_millis(ms)),
            wall: Some(Time { s: 100, ms: 0 }),
        }
    }

    fn base(queue: u64) -> Value {
        json!({"dropped": 7, "queue_depth": queue, "duty_available": 1, "duty_wait_ms": "1500"})
    }

    /// Writes each batch under `run` from seq `next`, as an owner would,
    /// and reads it back through a reader.
    fn send(f: &mut Framer, r: &mut Reader, batch: &Batch, run: Run, next: &mut u32) -> Applied {
        let e = f.encode(batch, run);
        let l = f.layout().clone();
        let stamp = Stamp {
            run,
            seq: Seq::new(*next).unwrap(),
        };
        let put = e.frame.envelope(&l, stamp).expect("written");
        let env = Envelope::parse(&put).unwrap();
        let back = Frame::parse(env.tag, env.record, &l).expect("read back");
        assert_eq!(back, e.frame);
        *next += batch.len();
        f.sent(e);
        r.apply(&l, stamp, &back)
    }

    /// The owner's rules (core §7.7): its first frame in a run full, a full
    /// record every `keyframe_every`, a delta record including what
    /// changed, a deadband omitted within `delta` and included every
    /// `max_records`, and a new run full again; a reader of its frames
    /// reads every field known or within.
    #[test]
    fn a_framer_writes_the_owners_frames() {
        let p = params(FrameSpec {
            keyframe_every: Some(4),
            deadband: [(
                "queue_depth".to_owned(),
                crate::authoring::DeadbandSpec {
                    delta: "4".to_owned(),
                    max_records: 3,
                },
            )]
            .into(),
            ..FrameSpec::default()
        });
        let l = Layout::of(&link(), &p).unwrap();
        let mut f = Framer::new(l.clone());
        let mut r = Reader::new();
        let (run, mut next) = (Run::U16(1), 0);
        let one = |f: &mut Framer, r: &mut Reader, v: Value, ms: u64, run: Run, next: &mut u32| {
            let b = f
                .push(measured(&l, v, ms))
                .unwrap()
                .expect("one record a frame");
            send(f, r, &b, run, next)
        };
        let a = one(&mut f, &mut r, base(12), 0, run, &mut next);
        assert!(
            a.records[0]
                .fields
                .iter()
                .all(|s| matches!(s, FieldState::Known(_)))
        );
        // Within 4 of 12, the rest unchanged: nothing included.
        let a = one(&mut f, &mut r, base(15), 1000, run, &mut next);
        assert_eq!(a.follows, Follows::Continues);
        assert_eq!(a.records[0].fields[1], FieldState::Within(12));
        let a = one(&mut f, &mut r, base(9), 2000, run, &mut next);
        assert_eq!(a.records[0].fields[1], FieldState::Within(12));
        // Three records after its inclusion: included, 13.
        let a = one(&mut f, &mut r, base(13), 3000, run, &mut next);
        assert_eq!(a.records[0].fields[1], FieldState::Known(13));
        // Four records after the full one: full again.
        let mut v = base(13);
        v["duty_wait_ms"] = json!("2500");
        let e = f.encode(
            &f.clone()
                .push(measured(&l, v.clone(), 4000))
                .unwrap()
                .unwrap(),
            run,
        );
        assert!(e.frame.records[0].full);
        let a = one(&mut f, &mut r, v, 4000, run, &mut next);
        assert_eq!(a.records[0].fields[3], FieldState::Known(25));
        // A delta: the wait changed alone.
        let mut v = base(13);
        v["duty_wait_ms"] = json!("1500");
        let e = f.encode(
            &f.clone()
                .push(measured(&l, v.clone(), 5000))
                .unwrap()
                .unwrap(),
            run,
        );
        assert!(!e.frame.records[0].full);
        assert_eq!(
            e.frame.records[0].slots,
            vec![Slot::Omitted, Slot::Omitted, Slot::Omitted, Slot::Code(15)]
        );
        // A field turned absent is included, its presence bit 0.
        let a = one(&mut f, &mut r, v, 5000, run, &mut next);
        let mut v = base(13);
        v.as_object_mut().unwrap().remove("dropped");
        let a2 = one(&mut f, &mut r, v, 6000, run, &mut next);
        assert_eq!(a.records[0].fields[0], FieldState::Known(7));
        assert_eq!(a2.records[0].fields[0], FieldState::Absent);
        // A new run: full, whatever changed.
        let b = f.push(measured(&l, base(13), 7000)).unwrap().unwrap();
        let e = f.encode(&b, Run::U16(2));
        assert!(e.frame.records[0].full);
        // A frame never sent leaves nothing: the next is written against
        // the last one sent, `dropped` present again, and the queue three
        // records after its inclusion.
        let e = f.encode(&b, run);
        assert_eq!(
            e.frame.records[0].slots,
            vec![Slot::Code(7), Slot::Code(13), Slot::Omitted, Slot::Omitted]
        );
    }

    /// Times (core §7.7): a record within the jitter of its instant is
    /// scheduled, another measured; a record at or past the span ends the
    /// frame and opens the next; a record measured before the last is
    /// refused.
    #[test]
    fn a_framer_schedules_and_ends_frames() {
        let p = params(FrameSpec {
            records: Some(3),
            step_ms: Some(10_000),
            jitter_ms: Some(500),
            keyframe_every: Some(3),
            ..FrameSpec::default()
        });
        let l = Layout::of(&sample(), &p).unwrap();
        let mut f = Framer::new(l.clone());
        let v = |t: &str, rssi: i64, mode: &str| json!({"temperature": t, "rssi": rssi, "link_up": true, "mode": mode});
        assert_eq!(f.push(measured(&l, v("21.5", -80, "rx"), 0)).unwrap(), None);
        assert_eq!(
            f.push(measured(&l, v("21.5", -82, "rx"), 10_200)).unwrap(),
            None
        );
        let b = f
            .push(measured(&l, v("22", -82, "tx"), 20_700))
            .unwrap()
            .expect("three records");
        let e = f.encode(&b, Run::U16(1));
        let at: Vec<_> = e.frame.records.iter().map(|r| r.at).collect();
        assert_eq!(
            at,
            vec![None, Some(At::Scheduled), Some(At::Offset(20_700))]
        );
        assert_eq!(e.frame.records[1].slots[1], Slot::Code(58));
        assert!(matches!(e.frame.records[1].slots[0], Slot::Omitted));
        f.sent(e);
        assert_eq!(
            f.push(measured(&l, v("22", -82, "tx"), 30_100)).unwrap(),
            None
        );
        assert_eq!(
            f.push(measured(&l, v("22", -82, "tx"), 39_700)).unwrap(),
            None
        );
        let b = f
            .push(measured(&l, v("22", -82, "tx"), 65_100))
            .unwrap()
            .expect("past the span");
        assert_eq!(b.len(), 2);
        let e = f.encode(&b, Run::U16(1));
        assert!(e.frame.records[0].full, "three records after the last full");
        assert_eq!(e.frame.records[1].at, Some(At::Scheduled));
        assert_eq!(f.open(), 1);
        assert_eq!(
            f.push(measured(&l, v("22", -82, "tx"), 65_000)),
            Err(WriteRefusal::Order)
        );
        assert_eq!(f.flush().map(|b| b.len()), Some(1));
        assert_eq!(f.flush(), None);
        // A slot omitted, or a code its field does not use, is refused.
        let mut m = measured(&l, v("22", -82, "tx"), 70_000);
        m.slots[0] = Slot::Omitted;
        assert_eq!(f.push(m), Err(WriteRefusal::Value));
        let mut m = measured(&l, v("22", -82, "tx"), 70_000);
        m.slots[0] = Slot::Code(251);
        assert_eq!(f.push(m), Err(WriteRefusal::Range));
    }

    /// The largest frame (link.v1 §2.8): `records` full records with every
    /// optional field and every offset.
    #[test]
    fn the_largest_frame() {
        let l = Layout::of(&link(), &one()).unwrap();
        assert_eq!(
            (l.largest_body_bits(), l.largest_payload_bytes()),
            (106, 22)
        );
        let p = params(FrameSpec {
            records: Some(3),
            step_ms: Some(10_000),
            ..FrameSpec::default()
        });
        let l = Layout::of(&sample(), &p).unwrap();
        // 42 + 2 + 3 × (1 + 19) + 2 × (1 + 15).
        assert_eq!(l.largest_body_bits(), 136);
        let all = json!({"temperature": "85", "rssi": -20, "link_up": true, "mode": "tx"});
        let slots = l.slots_of_json(&all, None).unwrap();
        let frame = Frame {
            time: Some(Time { s: 1, ms: 2 }),
            records: (0..3)
                .map(|i| Record {
                    full: true,
                    at: (i > 0).then_some(At::Offset(29_999)),
                    slots: slots.clone(),
                })
                .collect(),
        };
        assert_eq!(frame.write(&l).unwrap().bits, l.largest_body_bits());
        // keyframe_every 3 (F-470): two of the three records may be delta
        // records that include every field, a bit per field longer.
        let p = params(FrameSpec {
            records: Some(3),
            step_ms: Some(10_000),
            keyframe_every: Some(3),
            ..FrameSpec::default()
        });
        let k3 = Layout::of(&sample(), &p).unwrap();
        assert_eq!(k3.largest_body_bits(), 136 + 2 * 4);
        let deltas = Frame {
            records: frame
                .records
                .iter()
                .enumerate()
                .map(|(i, r)| Record {
                    full: i == 0,
                    ..r.clone()
                })
                .collect(),
            ..frame.clone()
        };
        assert_eq!(deltas.write(&k3).unwrap().bits, k3.largest_body_bits());
        // One record a frame, at keyframe_every 15: that record may be a
        // delta, 4 bits longer than a keyframe's.
        let p = params(FrameSpec {
            keyframe_every: Some(15),
            ..FrameSpec::default()
        });
        let k15 = Layout::of(&link(), &p).unwrap();
        assert_eq!(
            (k15.largest_body_bits(), k15.largest_payload_bytes()),
            (110, 22)
        );
        // A record's bytes read into its slots as its JSON does.
        let record = sample().write(&all, None).unwrap();
        assert_eq!(l.slots_of(&record.bytes).unwrap(), slots);
        assert_eq!(Time::from_unix_ms(1_500, 1), Some(Time { s: 0, ms: 500 }));
        assert_eq!(Time::from_unix_ms(500, 1), None);
        assert_eq!(Time { s: 0, ms: 500 }.unix_ms(1), 1_500);
    }

    #[test]
    fn widths() {
        assert_eq!(
            (
                bits_for(1),
                bits_for(2),
                bits_for(3),
                bits_for(256),
                bits_for(257)
            ),
            (0, 1, 2, 8, 9)
        );
        assert_eq!(bits_for(256 * MAX_STEP_MS), 40);
    }
}