folderbase-core 0.7.1

Local-first Folderbase protocol and filesystem primitives for humans and agents.
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
//! Canonical, provider-neutral full state for one Folderbase boundary.
//!
//! A Folderbase Version is a bounded data artifact. This module deliberately
//! does not capture filesystem state, resolve Cloud authority, or persist a
//! Local Head.

use std::{
    collections::{BTreeMap, BTreeSet},
    fmt::{self, Write as _},
    io::{Read, Write},
    marker::PhantomData,
};

use chrono::{DateTime, SecondsFormat, Utc};
use serde::{
    Deserialize, Serialize,
    de::{IgnoredAny, SeqAccess, Visitor},
};
use sha2::{Digest, Sha256};
use unicode_casefold::UnicodeCaseFold;
use unicode_normalization::UnicodeNormalization;
use uuid::Uuid;

pub const VERSION_FORMAT_V1: &str = "folderbase-version-v1";
pub const VERSION_PROTOCOL_V04: &str = "0.4";
pub const VERSION_PROTOCOL_V05: &str = "0.5";
pub const PATH_POLICY_FORMAT_V1: &str = "folderbase-portable-path-v1";
pub const MAX_ENCODED_VERSION_BYTES: u64 = 64 * 1024 * 1024;
pub const MAX_VERSION_ENTRIES: usize = 16_384;
pub const MAX_OBJECT_BYTES: u64 = 1024 * 1024 * 1024 * 1024;
pub const MAX_ROOT_MANIFEST_BYTES: u64 = 16 * 1024 * 1024;
pub const MAX_PATH_BYTES: usize = 4_096;
pub const MAX_PATH_COMPONENT_BYTES: usize = 255;
pub const MAX_PATH_DEPTH: usize = 128;

#[derive(Debug)]
pub struct FolderbaseVersion {
    format: String,
    protocol_version: String,
    folderbase_id: String,
    version_id: String,
    parents: Vec<String>,
    created_at: String,
    path_policy: PathPolicy,
    root_manifest: RootManifest,
    bindings: Vec<PathBinding>,
    tombstones: Vec<Tombstone>,
    exclusions: Vec<Exclusion>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct FolderbaseVersionWire {
    format: String,
    protocol_version: String,
    folderbase_id: String,
    version_id: String,
    parents: Vec<String>,
    created_at: String,
    path_policy: PathPolicy,
    root_manifest: RootManifest,
    #[serde(deserialize_with = "deserialize_bounded_bindings")]
    bindings: Vec<PathBinding>,
    #[serde(deserialize_with = "deserialize_bounded_tombstones")]
    tombstones: Vec<Tombstone>,
    #[serde(deserialize_with = "deserialize_bounded_exclusions")]
    exclusions: Vec<Exclusion>,
}

/// Crate-private construction boundary for future verified producers.
///
/// Later capture/seal code may prepare these parts only after it has verified
/// their referenced bytes. Construction still performs the complete protocol
/// validation before returning a `FolderbaseVersion`.
pub(crate) struct FolderbaseVersionParts {
    format: String,
    protocol_version: String,
    folderbase_id: String,
    version_id: String,
    parents: Vec<String>,
    created_at: String,
    path_policy: PathPolicy,
    root_manifest: RootManifest,
    bindings: Vec<PathBinding>,
    tombstones: Vec<Tombstone>,
    exclusions: Vec<Exclusion>,
}

pub(crate) struct FolderbaseVersionEntries {
    bindings: Vec<PathBinding>,
    tombstones: Vec<Tombstone>,
    exclusions: Vec<Exclusion>,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct PathPolicy {
    format: String,
    normalization: String,
    normalization_unicode_version: String,
    case_folding: String,
    case_folding_unicode_version: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct RootManifest {
    path: String,
    object_version_id: String,
    content_sha256: String,
    bytes: ExactU64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PathBindingKind {
    Directory,
    RegularFile,
    Symlink,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum PathBinding {
    Directory(DirectoryBinding),
    RegularFile(RegularFileBinding),
    Symlink(SymlinkBinding),
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct DirectoryBinding {
    path: String,
    object_id: String,
    lifecycle: LiveLifecycle,
    kind: DirectoryKind,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct RegularFileBinding {
    path: String,
    object_id: String,
    lifecycle: LiveLifecycle,
    kind: RegularFileKind,
    object_version_id: String,
    content_sha256: String,
    bytes: ExactU64,
    executable: bool,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct SymlinkBinding {
    path: String,
    object_id: String,
    lifecycle: LiveLifecycle,
    kind: SymlinkKind,
    object_version_id: String,
    target: String,
    target_safety: SymlinkTargetSafety,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
enum DirectoryKind {
    Directory,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
enum RegularFileKind {
    RegularFile,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
enum SymlinkKind {
    Symlink,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
enum LiveLifecycle {
    Live,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
enum SymlinkTargetSafety {
    RelativeWithinFolderbase,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Tombstone {
    path: String,
    object_id: String,
    lifecycle: DeletedLifecycle,
    deleted_kind: DeletedKind,
    last_object_version_id: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
enum DeletedLifecycle {
    Deleted,
}

#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum DeletedKind {
    Directory,
    RegularFile,
    Symlink,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Exclusion {
    path: String,
    kind: ExclusionKind,
    reason: ExclusionReason,
}

#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ExclusionKind {
    NestedFolderbase,
    HardLink,
    Fifo,
    Socket,
    BlockDevice,
    CharacterDevice,
    OtherSpecial,
}

#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum ExclusionReason {
    NestedFolderbaseBoundary,
    UnsupportedV1,
}

// This phase deliberately exposes the producer seam before the later sealing
// transaction consumes it.
#[allow(dead_code)]
impl FolderbaseVersionParts {
    /// Assemble the closed v1 shape from producer-verified references.
    ///
    /// `FolderbaseVersion::from_verified_parts` still enforces every protocol
    /// invariant before a value can exist.
    pub(crate) fn portable_v1_from_verified_producer(
        folderbase_id: impl Into<String>,
        version_id: impl Into<String>,
        parents: Vec<String>,
        created_at: impl Into<String>,
        root_manifest: RootManifest,
        entries: FolderbaseVersionEntries,
    ) -> Self {
        Self::portable_v1_for_protocol_from_verified_producer(
            VERSION_PROTOCOL_V04,
            folderbase_id,
            version_id,
            parents,
            created_at,
            root_manifest,
            entries,
        )
    }

    pub(crate) fn portable_v1_for_protocol_from_verified_producer(
        protocol_version: impl Into<String>,
        folderbase_id: impl Into<String>,
        version_id: impl Into<String>,
        parents: Vec<String>,
        created_at: impl Into<String>,
        root_manifest: RootManifest,
        entries: FolderbaseVersionEntries,
    ) -> Self {
        Self {
            format: VERSION_FORMAT_V1.to_owned(),
            protocol_version: protocol_version.into(),
            folderbase_id: folderbase_id.into(),
            version_id: version_id.into(),
            parents,
            created_at: created_at.into(),
            path_policy: PathPolicy::portable_v1(),
            root_manifest,
            bindings: entries.bindings,
            tombstones: entries.tombstones,
            exclusions: entries.exclusions,
        }
    }
}

#[allow(dead_code)]
impl FolderbaseVersionEntries {
    pub(crate) fn from_verified_producer(
        bindings: Vec<PathBinding>,
        tombstones: Vec<Tombstone>,
        exclusions: Vec<Exclusion>,
    ) -> Self {
        Self {
            bindings,
            tombstones,
            exclusions,
        }
    }
}

#[allow(dead_code)]
impl PathPolicy {
    fn portable_v1() -> Self {
        Self {
            format: PATH_POLICY_FORMAT_V1.to_owned(),
            normalization: "NFC".to_owned(),
            normalization_unicode_version: "17.0.0".to_owned(),
            case_folding: "full-default".to_owned(),
            case_folding_unicode_version: "9.0.0".to_owned(),
        }
    }
}

#[allow(dead_code)]
impl RootManifest {
    pub(crate) fn from_verified_producer(
        object_version_id: impl Into<String>,
        content_sha256: impl Into<String>,
        bytes: u64,
    ) -> Self {
        Self {
            path: ".folderbase/manifest.json".to_owned(),
            object_version_id: object_version_id.into(),
            content_sha256: content_sha256.into(),
            bytes: ExactU64(bytes),
        }
    }
}

#[allow(dead_code)]
impl PathBinding {
    pub(crate) fn directory_from_verified_producer(
        path: impl Into<String>,
        object_id: impl Into<String>,
    ) -> Self {
        Self::Directory(DirectoryBinding {
            path: path.into(),
            object_id: object_id.into(),
            lifecycle: LiveLifecycle::Live,
            kind: DirectoryKind::Directory,
        })
    }

    pub(crate) fn regular_file_from_verified_producer(
        path: impl Into<String>,
        object_id: impl Into<String>,
        object_version_id: impl Into<String>,
        content_sha256: impl Into<String>,
        bytes: u64,
        executable: bool,
    ) -> Self {
        Self::RegularFile(RegularFileBinding {
            path: path.into(),
            object_id: object_id.into(),
            lifecycle: LiveLifecycle::Live,
            kind: RegularFileKind::RegularFile,
            object_version_id: object_version_id.into(),
            content_sha256: content_sha256.into(),
            bytes: ExactU64(bytes),
            executable,
        })
    }

    pub(crate) fn symlink_from_verified_producer(
        path: impl Into<String>,
        object_id: impl Into<String>,
        object_version_id: impl Into<String>,
        target: impl Into<String>,
    ) -> Self {
        Self::Symlink(SymlinkBinding {
            path: path.into(),
            object_id: object_id.into(),
            lifecycle: LiveLifecycle::Live,
            kind: SymlinkKind::Symlink,
            object_version_id: object_version_id.into(),
            target: target.into(),
            target_safety: SymlinkTargetSafety::RelativeWithinFolderbase,
        })
    }
}

#[allow(dead_code)]
impl Tombstone {
    pub(crate) fn from_verified_producer(
        path: impl Into<String>,
        object_id: impl Into<String>,
        deleted_kind: DeletedKind,
        last_object_version_id: Option<String>,
    ) -> Self {
        Self {
            path: path.into(),
            object_id: object_id.into(),
            lifecycle: DeletedLifecycle::Deleted,
            deleted_kind,
            last_object_version_id,
        }
    }
}

#[allow(dead_code)]
impl Exclusion {
    pub(crate) fn from_verified_producer(
        path: impl Into<String>,
        kind: ExclusionKind,
        reason: ExclusionReason,
    ) -> Self {
        Self {
            path: path.into(),
            kind,
            reason,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FolderbaseVersionDiff {
    changes: Vec<FolderbaseVersionChange>,
}

impl FolderbaseVersionDiff {
    pub fn changes(&self) -> &[FolderbaseVersionChange] {
        &self.changes
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FolderbaseVersionChange {
    RootManifestUpdated {
        previous_object_version_id: String,
        object_version_id: String,
        previous_content_sha256: String,
        content_sha256: String,
    },
    Added {
        path: String,
        object_id: String,
    },
    Deleted {
        path: String,
        object_id: String,
        tombstone_present: bool,
    },
    Moved {
        object_id: String,
        from_path: String,
        to_path: String,
    },
    Recreated {
        path: String,
        previous_object_id: String,
        object_id: String,
    },
    Updated {
        path: String,
        object_id: String,
        previous_object_version_id: Option<String>,
        object_version_id: Option<String>,
    },
    TombstoneAdded {
        path: String,
        object_id: String,
    },
    TombstoneRemoved {
        path: String,
        object_id: String,
    },
    ExclusionAdded {
        path: String,
        kind: ExclusionKind,
    },
    ExclusionRemoved {
        path: String,
        kind: ExclusionKind,
    },
}

#[derive(Debug, thiserror::Error)]
pub enum FolderbaseVersionError {
    #[error("encoded Folderbase Version exceeds {maximum_bytes} bytes")]
    EncodedVersionTooLarge { maximum_bytes: u64 },

    #[error("Folderbase Version is not valid JSON: {0}")]
    InvalidJson(#[from] serde_json::Error),

    #[error("could not write encoded Folderbase Version: {0}")]
    EncodingIo(#[source] std::io::Error),

    #[error("Folderbase Version violates the protocol: {0}")]
    InvalidVersion(String),
}

impl FolderbaseVersion {
    pub fn decode_bounded(mut reader: impl Read) -> Result<Self, FolderbaseVersionError> {
        let mut encoded = Vec::new();
        reader
            .by_ref()
            .take(MAX_ENCODED_VERSION_BYTES + 1)
            .read_to_end(&mut encoded)
            .map_err(serde_json::Error::io)?;
        if encoded.len() as u64 > MAX_ENCODED_VERSION_BYTES {
            return Err(FolderbaseVersionError::EncodedVersionTooLarge {
                maximum_bytes: MAX_ENCODED_VERSION_BYTES,
            });
        }
        Self::decode_verified_slice(&encoded)
    }

    /// Encode one already-validated Folderbase Version as bounded JSON.
    ///
    /// The public value intentionally does not implement `Serialize`; this
    /// method is the only supported JSON encoding path.
    pub fn encode_bounded(&self, mut writer: impl Write) -> Result<(), FolderbaseVersionError> {
        self.validate()?;
        let mut encoded = BoundedJsonBuffer::new(MAX_ENCODED_VERSION_BYTES as usize);
        let result = serde_json::to_writer(&mut encoded, &FolderbaseVersionWireRef::from(self));
        if encoded.exceeded {
            return Err(FolderbaseVersionError::EncodedVersionTooLarge {
                maximum_bytes: MAX_ENCODED_VERSION_BYTES,
            });
        }
        result?;
        writer
            .write_all(&encoded.bytes)
            .map_err(FolderbaseVersionError::EncodingIo)
    }

    /// Construct a validated value from a bounded in-memory representation.
    ///
    /// This crate-private seam is reserved for later producer transactions;
    /// external callers continue to use `decode_bounded`.
    pub(crate) fn decode_verified_slice(encoded: &[u8]) -> Result<Self, FolderbaseVersionError> {
        if encoded.len() as u64 > MAX_ENCODED_VERSION_BYTES {
            return Err(FolderbaseVersionError::EncodedVersionTooLarge {
                maximum_bytes: MAX_ENCODED_VERSION_BYTES,
            });
        }
        let counts: EntryCountProbe = serde_json::from_slice(encoded)?;
        if counts.total() > MAX_VERSION_ENTRIES {
            return invalid("Folderbase Version entry count exceeds the v1 limit");
        }
        let parts =
            FolderbaseVersionParts::from(serde_json::from_slice::<FolderbaseVersionWire>(encoded)?);
        Self::from_verified_parts(parts)
    }

    /// Construct one value from producer-verified parts, then independently
    /// enforce every Folderbase Version invariant.
    pub(crate) fn from_verified_parts(
        parts: FolderbaseVersionParts,
    ) -> Result<Self, FolderbaseVersionError> {
        let version = Self {
            format: parts.format,
            protocol_version: parts.protocol_version,
            folderbase_id: parts.folderbase_id,
            version_id: parts.version_id,
            parents: parts.parents,
            created_at: parts.created_at,
            path_policy: parts.path_policy,
            root_manifest: parts.root_manifest,
            bindings: parts.bindings,
            tombstones: parts.tombstones,
            exclusions: parts.exclusions,
        };
        version.validate()?;
        Ok(version)
    }

    pub fn validate(&self) -> Result<(), FolderbaseVersionError> {
        if self.format != VERSION_FORMAT_V1
            || !matches!(
                self.protocol_version.as_str(),
                VERSION_PROTOCOL_V04 | VERSION_PROTOCOL_V05
            )
        {
            return invalid("unsupported Folderbase Version format or protocol");
        }
        validate_prefixed_uuid(&self.folderbase_id, "folderbase_")?;
        validate_prefixed_uuid(&self.version_id, "fbversion_")?;
        if self.parents.len() > 2 {
            return invalid("a Folderbase Version has at most two parents");
        }
        for parent in &self.parents {
            validate_prefixed_uuid(parent, "fbversion_")?;
            if parent == &self.version_id {
                return invalid("a Folderbase Version cannot be its own parent");
            }
        }
        if self.parents.len() == 2 && self.parents[0] == self.parents[1] {
            return invalid("Folderbase Version parents must be unique");
        }
        let parsed = DateTime::parse_from_rfc3339(&self.created_at)
            .map_err(|_| invalid_error("created_at must be canonical UTC RFC 3339 seconds"))?;
        if parsed.offset().local_minus_utc() != 0
            || parsed
                .with_timezone(&Utc)
                .to_rfc3339_opts(SecondsFormat::Secs, true)
                != self.created_at
        {
            return invalid("created_at must be canonical UTC RFC 3339 seconds");
        }
        if self.path_policy.format != PATH_POLICY_FORMAT_V1
            || self.path_policy.normalization != "NFC"
            || self.path_policy.normalization_unicode_version != "17.0.0"
            || self.path_policy.case_folding != "full-default"
            || self.path_policy.case_folding_unicode_version != "9.0.0"
        {
            return invalid("unsupported portable path policy");
        }
        if unicode_normalization::UNICODE_VERSION != (17, 0, 0)
            || unicode_casefold::UNICODE_VERSION != (9, 0, 0)
        {
            return invalid("Core build does not provide the declared Unicode path tables");
        }
        if self.root_manifest.path != ".folderbase/manifest.json" {
            return invalid("root_manifest must name the exact reserved manifest path");
        }
        validate_prefixed_uuid(&self.root_manifest.object_version_id, "version_")?;
        validate_sha256(&self.root_manifest.content_sha256)?;
        if self.root_manifest.bytes.0 == 0 || self.root_manifest.bytes.0 > MAX_ROOT_MANIFEST_BYTES {
            return invalid("root_manifest byte length is outside the v1 limit");
        }
        let total = self
            .bindings
            .len()
            .saturating_add(self.tombstones.len())
            .saturating_add(self.exclusions.len());
        if total > MAX_VERSION_ENTRIES {
            return invalid("Folderbase Version entry count exceeds the v1 limit");
        }

        validate_strict_path_order(self.bindings.iter().map(PathBinding::path), "bindings")?;
        validate_strict_path_order(
            self.tombstones
                .iter()
                .map(|tombstone| tombstone.path.as_str()),
            "tombstones",
        )?;
        validate_strict_path_order(
            self.exclusions
                .iter()
                .map(|exclusion| exclusion.path.as_str()),
            "exclusions",
        )?;

        let mut current_paths = CollisionIndex::default();
        let mut live_object_ids = BTreeSet::new();
        let mut object_version_owners = BTreeMap::new();
        object_version_owners.insert(
            self.root_manifest.object_version_id.as_str(),
            "__folderbase_root_manifest__",
        );
        for binding in &self.bindings {
            let keys = validate_portable_path(binding.path())?;
            current_paths.insert(binding.path(), keys, Some(binding.object_id()))?;
            validate_prefixed_uuid(binding.object_id(), "obj_")?;
            if !live_object_ids.insert(binding.object_id()) {
                return invalid("one live Object ID is bound to more than one path");
            }
            match binding {
                PathBinding::Directory(binding) => {
                    let _ = (&binding.lifecycle, &binding.kind);
                }
                PathBinding::RegularFile(binding) => {
                    let _ = (&binding.lifecycle, &binding.kind);
                    validate_prefixed_uuid(&binding.object_version_id, "version_")?;
                    bind_object_version(
                        &mut object_version_owners,
                        &binding.object_version_id,
                        &binding.object_id,
                    )?;
                    validate_sha256(&binding.content_sha256)?;
                    if binding.bytes.0 > MAX_OBJECT_BYTES {
                        return invalid("regular file exceeds the v1 object-size limit");
                    }
                }
                PathBinding::Symlink(binding) => {
                    let _ = (&binding.lifecycle, &binding.kind, &binding.target_safety);
                    validate_prefixed_uuid(&binding.object_version_id, "version_")?;
                    bind_object_version(
                        &mut object_version_owners,
                        &binding.object_version_id,
                        &binding.object_id,
                    )?;
                }
            }
        }
        if self.protocol_version == VERSION_PROTOCOL_V04 {
            for required_path in [".folderbaseignore", "FOLDERBASE.md"] {
                if !self.bindings.iter().any(|binding| {
                    binding.path() == required_path
                        && matches!(binding, PathBinding::RegularFile(_))
                }) {
                    return invalid(format!(
                        "{required_path} must be a live regular-file Path Binding"
                    ));
                }
            }
        }

        for exclusion in &self.exclusions {
            let keys = validate_portable_path(&exclusion.path)?;
            current_paths.insert(&exclusion.path, keys, None)?;
            match (exclusion.kind, exclusion.reason) {
                (ExclusionKind::NestedFolderbase, ExclusionReason::NestedFolderbaseBoundary)
                | (
                    ExclusionKind::HardLink
                    | ExclusionKind::Fifo
                    | ExclusionKind::Socket
                    | ExclusionKind::BlockDevice
                    | ExclusionKind::CharacterDevice
                    | ExclusionKind::OtherSpecial,
                    ExclusionReason::UnsupportedV1,
                ) => {}
                _ => return invalid("exclusion kind and reason do not match"),
            }
        }

        let mut tombstone_paths = CollisionIndex::default();
        for tombstone in &self.tombstones {
            let _ = &tombstone.lifecycle;
            let keys = validate_portable_path(&tombstone.path)?;
            tombstone_paths.insert(&tombstone.path, keys.clone(), Some(&tombstone.object_id))?;
            if let Some(current) = current_paths.exact.get(tombstone.path.as_str()) {
                match current.object_id {
                    Some(current_object_id) if current_object_id != tombstone.object_id => {}
                    Some(_) => {
                        return invalid("same-path recreation must use a new stable Object ID");
                    }
                    None => {
                        return invalid("a tombstone cannot occupy an excluded current path");
                    }
                }
            } else {
                current_paths.reject_alias(&tombstone.path, &keys)?;
            }
            validate_prefixed_uuid(&tombstone.object_id, "obj_")?;
            if let Some(version_id) = &tombstone.last_object_version_id {
                validate_prefixed_uuid(version_id, "version_")?;
                bind_object_version(&mut object_version_owners, version_id, &tombstone.object_id)?;
            }
            match (
                tombstone.deleted_kind,
                tombstone.last_object_version_id.is_some(),
            ) {
                (DeletedKind::Directory, false)
                | (DeletedKind::RegularFile | DeletedKind::Symlink, true) => {}
                _ => {
                    return invalid(
                        "directory tombstones omit Object Version; content tombstones require it",
                    );
                }
            }
        }

        let nested_boundaries = NestedBoundaryIndex::from_paths(
            self.exclusions
                .iter()
                .filter(|exclusion| exclusion.kind == ExclusionKind::NestedFolderbase)
                .map(|exclusion| exclusion.path.as_str()),
        );
        for boundary in nested_boundaries.iter() {
            if nested_boundaries.contains_strict_ancestor(boundary) {
                return invalid(format!(
                    "nested Folderbase boundary overlaps its ancestor: {boundary}"
                ));
            }
        }
        for binding in &self.bindings {
            reject_nested_descendant(binding.path(), &nested_boundaries)?;
        }
        for tombstone in &self.tombstones {
            reject_nested_descendant(&tombstone.path, &nested_boundaries)?;
        }
        for exclusion in &self.exclusions {
            if exclusion.kind != ExclusionKind::NestedFolderbase {
                reject_nested_descendant(&exclusion.path, &nested_boundaries)?;
            }
        }
        for binding in &self.bindings {
            if let PathBinding::Symlink(symlink) = binding {
                validate_symlink_target(&symlink.path, &symlink.target, &nested_boundaries)?;
            }
        }
        Ok(())
    }

    pub fn version_id(&self) -> &str {
        &self.version_id
    }

    pub fn folderbase_id(&self) -> &str {
        &self.folderbase_id
    }

    pub fn protocol_version(&self) -> &str {
        &self.protocol_version
    }

    pub fn parents(&self) -> &[String] {
        &self.parents
    }

    pub fn created_at(&self) -> &str {
        &self.created_at
    }

    pub fn root_manifest(&self) -> &RootManifest {
        &self.root_manifest
    }

    pub fn bindings(&self) -> &[PathBinding] {
        &self.bindings
    }

    pub fn tombstones(&self) -> &[Tombstone] {
        &self.tombstones
    }

    pub fn exclusions(&self) -> &[Exclusion] {
        &self.exclusions
    }

    pub fn binding_count(&self) -> usize {
        self.bindings.len()
    }

    pub fn tombstone_count(&self) -> usize {
        self.tombstones.len()
    }

    pub fn exclusion_count(&self) -> usize {
        self.exclusions.len()
    }

    pub fn lookup_binding(&self, path: &str) -> Option<&PathBinding> {
        self.bindings
            .binary_search_by(|binding| binding.path().as_bytes().cmp(path.as_bytes()))
            .ok()
            .map(|index| &self.bindings[index])
    }

    pub fn canonical_digest(&self) -> Result<String, FolderbaseVersionError> {
        self.validate()?;

        let mut digest = Sha256::new();
        digest.update(b"folderbase-version-v1\0");
        update_identifier(&mut digest, &self.protocol_version);
        update_identifier(&mut digest, &self.folderbase_id);
        update_identifier(&mut digest, &self.version_id);
        digest.update([self.parents.len() as u8]);
        for parent in &self.parents {
            update_identifier(&mut digest, parent);
        }
        update_identifier(&mut digest, &self.created_at);
        update_identifier(&mut digest, &self.path_policy.format);
        update_identifier(&mut digest, &self.path_policy.normalization);
        update_identifier(&mut digest, &self.path_policy.normalization_unicode_version);
        update_identifier(&mut digest, &self.path_policy.case_folding);
        update_identifier(&mut digest, &self.path_policy.case_folding_unicode_version);
        update_identifier(&mut digest, &self.root_manifest.path);
        update_identifier(&mut digest, &self.root_manifest.object_version_id);
        digest.update(decode_sha256(&self.root_manifest.content_sha256));
        digest.update(self.root_manifest.bytes.0.to_be_bytes());

        digest.update((self.bindings.len() as u32).to_be_bytes());
        for binding in &self.bindings {
            update_identifier(&mut digest, binding.path());
            update_identifier(&mut digest, binding.object_id());
            update_identifier(&mut digest, "live");
            match binding {
                PathBinding::Directory(_) => digest.update([0]),
                PathBinding::RegularFile(binding) => {
                    digest.update([1]);
                    update_identifier(&mut digest, &binding.object_version_id);
                    digest.update(decode_sha256(&binding.content_sha256));
                    digest.update(binding.bytes.0.to_be_bytes());
                    digest.update([u8::from(binding.executable)]);
                }
                PathBinding::Symlink(binding) => {
                    digest.update([2]);
                    update_identifier(&mut digest, &binding.object_version_id);
                    update_identifier(&mut digest, &binding.target);
                    update_identifier(&mut digest, "relative-within-folderbase");
                }
            }
        }

        digest.update((self.tombstones.len() as u32).to_be_bytes());
        for tombstone in &self.tombstones {
            update_identifier(&mut digest, &tombstone.path);
            update_identifier(&mut digest, &tombstone.object_id);
            update_identifier(&mut digest, "deleted");
            digest.update([match tombstone.deleted_kind {
                DeletedKind::Directory => 0,
                DeletedKind::RegularFile => 1,
                DeletedKind::Symlink => 2,
            }]);
            match &tombstone.last_object_version_id {
                Some(version_id) => {
                    digest.update([1]);
                    update_identifier(&mut digest, version_id);
                }
                None => digest.update([0]),
            }
        }

        digest.update((self.exclusions.len() as u32).to_be_bytes());
        for exclusion in &self.exclusions {
            update_identifier(&mut digest, &exclusion.path);
            digest.update([match exclusion.kind {
                ExclusionKind::NestedFolderbase => 0,
                ExclusionKind::HardLink => 1,
                ExclusionKind::Fifo => 2,
                ExclusionKind::Socket => 3,
                ExclusionKind::BlockDevice => 4,
                ExclusionKind::CharacterDevice => 5,
                ExclusionKind::OtherSpecial => 6,
            }]);
            update_identifier(
                &mut digest,
                match exclusion.reason {
                    ExclusionReason::NestedFolderbaseBoundary => "nested-folderbase-boundary",
                    ExclusionReason::UnsupportedV1 => "unsupported-v1",
                },
            );
        }

        Ok(digest_hex(&digest.finalize()))
    }

    pub fn diff(&self, newer: &Self) -> Result<FolderbaseVersionDiff, FolderbaseVersionError> {
        self.validate()?;
        newer.validate()?;
        if self.folderbase_id != newer.folderbase_id {
            return invalid("cannot diff Folderbase Versions from different Folderbases");
        }

        let mut changes = Vec::new();
        if self.root_manifest != newer.root_manifest {
            changes.push(FolderbaseVersionChange::RootManifestUpdated {
                previous_object_version_id: self.root_manifest.object_version_id.clone(),
                object_version_id: newer.root_manifest.object_version_id.clone(),
                previous_content_sha256: self.root_manifest.content_sha256.clone(),
                content_sha256: newer.root_manifest.content_sha256.clone(),
            });
        }

        let previous_by_object = self
            .bindings
            .iter()
            .map(|binding| (binding.object_id(), binding))
            .collect::<BTreeMap<_, _>>();
        let current_by_object = newer
            .bindings
            .iter()
            .map(|binding| (binding.object_id(), binding))
            .collect::<BTreeMap<_, _>>();
        for (object_id, previous) in &previous_by_object {
            if let Some(current) = current_by_object.get(object_id)
                && previous.path() != current.path()
            {
                changes.push(FolderbaseVersionChange::Moved {
                    object_id: (*object_id).to_owned(),
                    from_path: previous.path().to_owned(),
                    to_path: current.path().to_owned(),
                });
                if !previous.state_eq_ignoring_path(current) {
                    changes.push(FolderbaseVersionChange::Updated {
                        path: current.path().to_owned(),
                        object_id: (*object_id).to_owned(),
                        previous_object_version_id: previous.object_version_id().map(str::to_owned),
                        object_version_id: current.object_version_id().map(str::to_owned),
                    });
                }
            }
        }

        let previous_by_path = self
            .bindings
            .iter()
            .map(|binding| (binding.path(), binding))
            .collect::<BTreeMap<_, _>>();
        let current_by_path = newer
            .bindings
            .iter()
            .map(|binding| (binding.path(), binding))
            .collect::<BTreeMap<_, _>>();
        let all_paths = previous_by_path
            .keys()
            .chain(current_by_path.keys())
            .copied()
            .collect::<BTreeSet<_>>();
        for path in all_paths {
            match (previous_by_path.get(path), current_by_path.get(path)) {
                (None, Some(current)) if !previous_by_object.contains_key(current.object_id()) => {
                    changes.push(FolderbaseVersionChange::Added {
                        path: path.to_owned(),
                        object_id: current.object_id().to_owned(),
                    });
                }
                (Some(previous), None) if !current_by_object.contains_key(previous.object_id()) => {
                    let tombstone_present = newer.tombstones.iter().any(|tombstone| {
                        tombstone.path == path && tombstone.object_id == previous.object_id()
                    });
                    changes.push(FolderbaseVersionChange::Deleted {
                        path: path.to_owned(),
                        object_id: previous.object_id().to_owned(),
                        tombstone_present,
                    });
                }
                (Some(previous), Some(current)) if previous.object_id() != current.object_id() => {
                    changes.push(FolderbaseVersionChange::Recreated {
                        path: path.to_owned(),
                        previous_object_id: previous.object_id().to_owned(),
                        object_id: current.object_id().to_owned(),
                    });
                }
                (Some(previous), Some(current)) if *previous != *current => {
                    changes.push(FolderbaseVersionChange::Updated {
                        path: path.to_owned(),
                        object_id: current.object_id().to_owned(),
                        previous_object_version_id: previous.object_version_id().map(str::to_owned),
                        object_version_id: current.object_version_id().map(str::to_owned),
                    });
                }
                _ => {}
            }
        }

        let previous_tombstones = self
            .tombstones
            .iter()
            .map(|tombstone| {
                (
                    (tombstone.path.as_str(), tombstone.object_id.as_str()),
                    tombstone,
                )
            })
            .collect::<BTreeMap<_, _>>();
        let current_tombstones = newer
            .tombstones
            .iter()
            .map(|tombstone| {
                (
                    (tombstone.path.as_str(), tombstone.object_id.as_str()),
                    tombstone,
                )
            })
            .collect::<BTreeMap<_, _>>();
        let tombstone_keys = previous_tombstones
            .keys()
            .chain(current_tombstones.keys())
            .copied()
            .collect::<BTreeSet<_>>();
        for (path, object_id) in tombstone_keys {
            match (
                previous_tombstones.get(&(path, object_id)),
                current_tombstones.get(&(path, object_id)),
            ) {
                (None, Some(_)) => {
                    changes.push(FolderbaseVersionChange::TombstoneAdded {
                        path: path.to_owned(),
                        object_id: object_id.to_owned(),
                    });
                }
                (Some(_), None) => {
                    changes.push(FolderbaseVersionChange::TombstoneRemoved {
                        path: path.to_owned(),
                        object_id: object_id.to_owned(),
                    });
                }
                (Some(previous), Some(current)) if *previous != *current => {
                    changes.push(FolderbaseVersionChange::TombstoneRemoved {
                        path: path.to_owned(),
                        object_id: object_id.to_owned(),
                    });
                    changes.push(FolderbaseVersionChange::TombstoneAdded {
                        path: path.to_owned(),
                        object_id: object_id.to_owned(),
                    });
                }
                _ => {}
            }
        }

        let previous_exclusions = self
            .exclusions
            .iter()
            .map(|exclusion| (exclusion.path.as_str(), exclusion))
            .collect::<BTreeMap<_, _>>();
        let current_exclusions = newer
            .exclusions
            .iter()
            .map(|exclusion| (exclusion.path.as_str(), exclusion))
            .collect::<BTreeMap<_, _>>();
        let exclusion_paths = previous_exclusions
            .keys()
            .chain(current_exclusions.keys())
            .copied()
            .collect::<BTreeSet<_>>();
        for path in exclusion_paths {
            match (previous_exclusions.get(path), current_exclusions.get(path)) {
                (None, Some(current)) => {
                    changes.push(FolderbaseVersionChange::ExclusionAdded {
                        path: path.to_owned(),
                        kind: current.kind,
                    });
                }
                (Some(previous), None) => {
                    changes.push(FolderbaseVersionChange::ExclusionRemoved {
                        path: path.to_owned(),
                        kind: previous.kind,
                    });
                }
                (Some(previous), Some(current)) if *previous != *current => {
                    changes.push(FolderbaseVersionChange::ExclusionRemoved {
                        path: path.to_owned(),
                        kind: previous.kind,
                    });
                    changes.push(FolderbaseVersionChange::ExclusionAdded {
                        path: path.to_owned(),
                        kind: current.kind,
                    });
                }
                _ => {}
            }
        }

        changes.sort_by_cached_key(change_sort_key);
        Ok(FolderbaseVersionDiff { changes })
    }
}

struct BoundedJsonBuffer {
    bytes: Vec<u8>,
    maximum: usize,
    exceeded: bool,
}

impl BoundedJsonBuffer {
    fn new(maximum: usize) -> Self {
        Self {
            bytes: Vec::with_capacity(4096),
            maximum,
            exceeded: false,
        }
    }
}

impl Write for BoundedJsonBuffer {
    fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
        let Some(length) = self.bytes.len().checked_add(bytes.len()) else {
            self.exceeded = true;
            return Err(std::io::Error::other(
                "Folderbase Version encoded length overflowed",
            ));
        };
        if length > self.maximum {
            self.exceeded = true;
            return Err(std::io::Error::other(
                "Folderbase Version encoded length exceeded its limit",
            ));
        }
        self.bytes.extend_from_slice(bytes);
        Ok(bytes.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

#[derive(Serialize)]
struct FolderbaseVersionWireRef<'a> {
    format: &'a str,
    protocol_version: &'a str,
    folderbase_id: &'a str,
    version_id: &'a str,
    parents: &'a [String],
    created_at: &'a str,
    path_policy: &'a PathPolicy,
    root_manifest: &'a RootManifest,
    bindings: &'a [PathBinding],
    tombstones: &'a [Tombstone],
    exclusions: &'a [Exclusion],
}

impl<'a> From<&'a FolderbaseVersion> for FolderbaseVersionWireRef<'a> {
    fn from(version: &'a FolderbaseVersion) -> Self {
        Self {
            format: &version.format,
            protocol_version: &version.protocol_version,
            folderbase_id: &version.folderbase_id,
            version_id: &version.version_id,
            parents: &version.parents,
            created_at: &version.created_at,
            path_policy: &version.path_policy,
            root_manifest: &version.root_manifest,
            bindings: &version.bindings,
            tombstones: &version.tombstones,
            exclusions: &version.exclusions,
        }
    }
}

impl From<FolderbaseVersionWire> for FolderbaseVersionParts {
    fn from(wire: FolderbaseVersionWire) -> Self {
        Self {
            format: wire.format,
            protocol_version: wire.protocol_version,
            folderbase_id: wire.folderbase_id,
            version_id: wire.version_id,
            parents: wire.parents,
            created_at: wire.created_at,
            path_policy: wire.path_policy,
            root_manifest: wire.root_manifest,
            bindings: wire.bindings,
            tombstones: wire.tombstones,
            exclusions: wire.exclusions,
        }
    }
}

impl PathBinding {
    pub fn path(&self) -> &str {
        match self {
            Self::Directory(binding) => &binding.path,
            Self::RegularFile(binding) => &binding.path,
            Self::Symlink(binding) => &binding.path,
        }
    }

    pub fn object_id(&self) -> &str {
        match self {
            Self::Directory(binding) => &binding.object_id,
            Self::RegularFile(binding) => &binding.object_id,
            Self::Symlink(binding) => &binding.object_id,
        }
    }

    pub fn kind(&self) -> PathBindingKind {
        match self {
            Self::Directory(_) => PathBindingKind::Directory,
            Self::RegularFile(_) => PathBindingKind::RegularFile,
            Self::Symlink(_) => PathBindingKind::Symlink,
        }
    }

    pub fn object_version_id(&self) -> Option<&str> {
        match self {
            Self::Directory(_) => None,
            Self::RegularFile(binding) => Some(&binding.object_version_id),
            Self::Symlink(binding) => Some(&binding.object_version_id),
        }
    }

    pub fn bytes(&self) -> Option<u64> {
        match self {
            Self::RegularFile(binding) => Some(binding.bytes.0),
            _ => None,
        }
    }

    pub fn executable(&self) -> Option<bool> {
        match self {
            Self::RegularFile(binding) => Some(binding.executable),
            _ => None,
        }
    }

    pub fn symlink_target(&self) -> Option<&str> {
        match self {
            Self::Symlink(binding) => Some(&binding.target),
            _ => None,
        }
    }

    pub fn content_sha256(&self) -> Option<&str> {
        match self {
            Self::RegularFile(binding) => Some(&binding.content_sha256),
            _ => None,
        }
    }

    fn state_eq_ignoring_path(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Directory(previous), Self::Directory(current)) => {
                previous.object_id == current.object_id
                    && previous.lifecycle == current.lifecycle
                    && previous.kind == current.kind
            }
            (Self::RegularFile(previous), Self::RegularFile(current)) => {
                previous.object_id == current.object_id
                    && previous.lifecycle == current.lifecycle
                    && previous.kind == current.kind
                    && previous.object_version_id == current.object_version_id
                    && previous.content_sha256 == current.content_sha256
                    && previous.bytes == current.bytes
                    && previous.executable == current.executable
            }
            (Self::Symlink(previous), Self::Symlink(current)) => {
                previous.object_id == current.object_id
                    && previous.lifecycle == current.lifecycle
                    && previous.kind == current.kind
                    && previous.object_version_id == current.object_version_id
                    && previous.target == current.target
                    && previous.target_safety == current.target_safety
            }
            _ => false,
        }
    }
}

impl RootManifest {
    pub fn path(&self) -> &str {
        &self.path
    }

    pub fn object_version_id(&self) -> &str {
        &self.object_version_id
    }

    pub fn content_sha256(&self) -> &str {
        &self.content_sha256
    }

    pub fn bytes(&self) -> u64 {
        self.bytes.0
    }
}

impl Tombstone {
    pub fn path(&self) -> &str {
        &self.path
    }

    pub fn object_id(&self) -> &str {
        &self.object_id
    }

    pub fn deleted_kind(&self) -> DeletedKind {
        self.deleted_kind
    }

    pub fn last_object_version_id(&self) -> Option<&str> {
        self.last_object_version_id.as_deref()
    }
}

impl Exclusion {
    pub fn path(&self) -> &str {
        &self.path
    }

    pub fn kind(&self) -> ExclusionKind {
        self.kind
    }

    pub fn reason(&self) -> ExclusionReason {
        self.reason
    }
}

fn validate_prefixed_uuid(value: &str, prefix: &str) -> Result<(), FolderbaseVersionError> {
    let Some(uuid) = value.strip_prefix(prefix) else {
        return invalid("identifier uses the wrong namespace");
    };
    let parsed = Uuid::parse_str(uuid).map_err(|_| invalid_error("identifier is not a UUID"))?;
    if parsed.hyphenated().to_string() != uuid {
        return invalid("identifier UUID is not canonical lowercase hyphenated text");
    }
    if !(1..=8).contains(&(parsed.as_bytes()[6] >> 4))
        || parsed.as_bytes()[8] & 0b1100_0000 != 0b1000_0000
    {
        return invalid("identifier UUID has an unsupported version or variant");
    }
    Ok(())
}

fn validate_sha256(value: &str) -> Result<(), FolderbaseVersionError> {
    if value.len() != 64
        || !value
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    {
        return invalid("content digest is not lowercase hexadecimal SHA-256");
    }
    Ok(())
}

fn update_identifier(digest: &mut Sha256, value: &str) {
    digest.update((value.len() as u32).to_be_bytes());
    digest.update(value.as_bytes());
}

fn decode_sha256(value: &str) -> [u8; 32] {
    let mut decoded = [0_u8; 32];
    for (destination, encoded) in decoded.iter_mut().zip(value.as_bytes().chunks_exact(2)) {
        *destination = (hex_nibble(encoded[0]) << 4) | hex_nibble(encoded[1]);
    }
    decoded
}

fn hex_nibble(byte: u8) -> u8 {
    match byte {
        b'0'..=b'9' => byte - b'0',
        b'a'..=b'f' => byte - b'a' + 10,
        _ => unreachable!("digest syntax is validated before canonical encoding"),
    }
}

fn digest_hex(bytes: &[u8]) -> String {
    let mut encoded = String::with_capacity(bytes.len() * 2);
    for byte in bytes {
        write!(&mut encoded, "{byte:02x}").expect("writing to String cannot fail");
    }
    encoded
}

fn change_sort_key(change: &FolderbaseVersionChange) -> (String, u8, String) {
    match change {
        FolderbaseVersionChange::RootManifestUpdated { .. } => (String::new(), 0, String::new()),
        FolderbaseVersionChange::Moved {
            object_id,
            from_path,
            ..
        } => (from_path.clone(), 1, object_id.clone()),
        FolderbaseVersionChange::Recreated {
            path, object_id, ..
        } => (path.clone(), 2, object_id.clone()),
        FolderbaseVersionChange::Added { path, object_id } => (path.clone(), 3, object_id.clone()),
        FolderbaseVersionChange::Updated {
            path, object_id, ..
        } => (path.clone(), 4, object_id.clone()),
        FolderbaseVersionChange::Deleted {
            path, object_id, ..
        } => (path.clone(), 5, object_id.clone()),
        FolderbaseVersionChange::TombstoneAdded { path, object_id } => {
            (path.clone(), 6, object_id.clone())
        }
        FolderbaseVersionChange::TombstoneRemoved { path, object_id } => {
            (path.clone(), 7, object_id.clone())
        }
        FolderbaseVersionChange::ExclusionAdded { path, .. } => (path.clone(), 8, String::new()),
        FolderbaseVersionChange::ExclusionRemoved { path, .. } => (path.clone(), 9, String::new()),
    }
}

#[derive(Debug, Clone)]
struct PathKeys {
    nfc: String,
    folded: String,
}

#[derive(Default)]
struct CollisionIndex<'a> {
    exact: BTreeMap<&'a str, PathOwner<'a>>,
    nfc: BTreeMap<String, &'a str>,
    folded: BTreeMap<String, &'a str>,
}

#[derive(Clone, Copy)]
struct PathOwner<'a> {
    object_id: Option<&'a str>,
}

impl<'a> CollisionIndex<'a> {
    fn insert(
        &mut self,
        path: &'a str,
        keys: PathKeys,
        object_id: Option<&'a str>,
    ) -> Result<(), FolderbaseVersionError> {
        if self.exact.insert(path, PathOwner { object_id }).is_some() {
            return invalid("exact portable paths collide");
        }
        if self.nfc.insert(keys.nfc, path).is_some() {
            return invalid("portable paths collide after NFC normalization");
        }
        if self.folded.insert(keys.folded, path).is_some() {
            return invalid("portable paths collide after full default case folding");
        }
        Ok(())
    }

    fn reject_alias(&self, path: &str, keys: &PathKeys) -> Result<(), FolderbaseVersionError> {
        if self
            .nfc
            .get(&keys.nfc)
            .is_some_and(|existing| *existing != path)
        {
            return invalid("portable paths collide after NFC normalization");
        }
        if self
            .folded
            .get(&keys.folded)
            .is_some_and(|existing| *existing != path)
        {
            return invalid("portable paths collide after full default case folding");
        }
        Ok(())
    }
}

fn validate_strict_path_order<'a>(
    paths: impl Iterator<Item = &'a str>,
    label: &str,
) -> Result<(), FolderbaseVersionError> {
    let mut previous: Option<&[u8]> = None;
    for path in paths {
        if previous.is_some_and(|previous| previous >= path.as_bytes()) {
            return invalid(format!(
                "{label} must be strictly sorted by exact UTF-8 path bytes"
            ));
        }
        previous = Some(path.as_bytes());
    }
    Ok(())
}

pub(crate) fn validate_capture_path(path: &str) -> Result<(), FolderbaseVersionError> {
    validate_portable_path(path).map(|_| ())
}

pub(crate) fn validate_capture_version_id(value: &str) -> Result<(), FolderbaseVersionError> {
    validate_prefixed_uuid(value, "fbversion_")
}

pub(crate) fn validate_capture_sha256(value: &str) -> Result<(), FolderbaseVersionError> {
    validate_sha256(value)
}

pub(crate) fn validate_capture_symlink_targets<'a>(
    targets: impl IntoIterator<Item = (&'a str, &'a str)>,
    nested_boundaries: &[String],
) -> Result<(), &'a str> {
    let nested_boundaries =
        NestedBoundaryIndex::from_paths(nested_boundaries.iter().map(String::as_str));
    for (link_path, target) in targets {
        if validate_symlink_target(link_path, target, &nested_boundaries).is_err() {
            return Err(link_path);
        }
    }
    Ok(())
}

fn validate_portable_path(path: &str) -> Result<PathKeys, FolderbaseVersionError> {
    let bytes = path.as_bytes();
    let drive_prefix = bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':';
    if bytes.is_empty()
        || bytes.len() > MAX_PATH_BYTES
        || path.starts_with('/')
        || path.ends_with('/')
        || path.contains('\\')
        || path.contains('\0')
        || drive_prefix
    {
        return invalid(format!("unsafe portable path: {path}"));
    }
    let components = path.split('/').collect::<Vec<_>>();
    if components.len() > MAX_PATH_DEPTH {
        return invalid("portable path exceeds the v1 depth limit");
    }
    for component in components {
        validate_portable_component(component)?;
    }
    Ok(PathKeys {
        nfc: path.nfc().collect(),
        folded: path
            .nfc()
            .collect::<String>()
            .case_fold()
            .collect::<String>()
            .nfc()
            .collect(),
    })
}

fn validate_portable_component(component: &str) -> Result<(), FolderbaseVersionError> {
    if component.is_empty()
        || matches!(component, "." | "..")
        || component.len() > MAX_PATH_COMPONENT_BYTES
        || component.ends_with(['.', ' '])
        || component.eq_ignore_ascii_case(".folderbase")
        || component
            .chars()
            .any(|character| character <= '\u{1f}' || r#"<>:"|?*"#.contains(character))
    {
        return invalid(format!("unsafe portable path component: {component}"));
    }
    let stem = component.split('.').next().unwrap_or(component);
    let uppercase_stem = stem.to_ascii_uppercase();
    let reserved = matches!(
        uppercase_stem.as_str(),
        "CON"
            | "PRN"
            | "AUX"
            | "NUL"
            | "COM1"
            | "COM2"
            | "COM3"
            | "COM4"
            | "COM5"
            | "COM6"
            | "COM7"
            | "COM8"
            | "COM9"
            | "LPT1"
            | "LPT2"
            | "LPT3"
            | "LPT4"
            | "LPT5"
            | "LPT6"
            | "LPT7"
            | "LPT8"
            | "LPT9"
            | "COM¹"
            | "COM²"
            | "COM³"
            | "LPT¹"
            | "LPT²"
            | "LPT³"
    );
    if reserved {
        return invalid(format!(
            "Windows-reserved portable path component: {component}"
        ));
    }
    Ok(())
}

fn bind_object_version<'a>(
    owners: &mut BTreeMap<&'a str, &'a str>,
    version_id: &'a str,
    object_id: &'a str,
) -> Result<(), FolderbaseVersionError> {
    if owners
        .insert(version_id, object_id)
        .is_some_and(|existing| existing != object_id)
    {
        return invalid("one Object Version is referenced by different Object IDs");
    }
    Ok(())
}

fn reject_nested_descendant(
    path: &str,
    boundaries: &NestedBoundaryIndex,
) -> Result<(), FolderbaseVersionError> {
    let path = portable_folded_key(path);
    if boundaries.contains_strict_ancestor(&path) {
        return invalid(format!(
            "path enters an excluded nested Folderbase boundary: {path}"
        ));
    }
    Ok(())
}

fn validate_symlink_target(
    link_path: &str,
    target: &str,
    nested_boundaries: &NestedBoundaryIndex,
) -> Result<(), FolderbaseVersionError> {
    let bytes = target.as_bytes();
    let drive_prefix = bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':';
    if bytes.is_empty()
        || bytes.len() > MAX_PATH_BYTES
        || target.starts_with('/')
        || target.ends_with('/')
        || target.contains('\\')
        || target.contains('\0')
        || target.contains("//")
        || drive_prefix
    {
        return invalid("symlink target is not a portable relative target");
    }

    let mut resolved = link_path.split('/').rev().skip(1).collect::<Vec<_>>();
    resolved.reverse();
    for component in target.split('/') {
        match component {
            "." => {}
            ".." => {
                if resolved.pop().is_none() {
                    return invalid("symlink target escapes the Folderbase root");
                }
            }
            component => {
                validate_portable_component(component)?;
                resolved.push(component);
                if resolved.len() > MAX_PATH_DEPTH {
                    return invalid("symlink target exceeds the v1 path depth limit");
                }
            }
        }
    }
    let resolved = resolved.join("/");
    if !resolved.is_empty() {
        validate_portable_path(&resolved)?;
        if resolved.eq_ignore_ascii_case(".folderbase")
            || resolved
                .split('/')
                .next()
                .is_some_and(|component| component.eq_ignore_ascii_case(".folderbase"))
        {
            return invalid("symlink target enters Folderbase protocol state");
        }
        let folded_resolved = portable_folded_key(&resolved);
        if nested_boundaries.contains_or_ancestor(&folded_resolved) {
            return invalid("symlink target enters a nested Folderbase boundary");
        }
    }
    Ok(())
}

struct NestedBoundaryIndex {
    folded: BTreeSet<String>,
}

impl NestedBoundaryIndex {
    fn from_paths<'a>(paths: impl IntoIterator<Item = &'a str>) -> Self {
        Self {
            folded: paths.into_iter().map(portable_folded_key).collect(),
        }
    }

    fn iter(&self) -> impl Iterator<Item = &str> {
        self.folded.iter().map(String::as_str)
    }

    fn contains_or_ancestor(&self, folded_path: &str) -> bool {
        self.contains_ancestor_with(folded_path, true, |_| {})
    }

    fn contains_strict_ancestor(&self, folded_path: &str) -> bool {
        self.contains_ancestor_with(folded_path, false, |_| {})
    }

    fn contains_ancestor_with(
        &self,
        folded_path: &str,
        include_exact: bool,
        mut inspect: impl FnMut(&str),
    ) -> bool {
        for (separator, _) in folded_path.match_indices('/') {
            let ancestor = &folded_path[..separator];
            inspect(ancestor);
            if self.folded.contains(ancestor) {
                return true;
            }
        }
        if include_exact {
            inspect(folded_path);
            if self.folded.contains(folded_path) {
                return true;
            }
        }
        false
    }

    #[cfg(test)]
    fn len(&self) -> usize {
        self.folded.len()
    }

    #[cfg(test)]
    fn probe_count(&self, folded_path: &str, include_exact: bool) -> usize {
        let mut probes = 0;
        let _ = self.contains_ancestor_with(folded_path, include_exact, |_| probes += 1);
        probes
    }
}

fn portable_folded_key(path: &str) -> String {
    path.nfc()
        .collect::<String>()
        .case_fold()
        .collect::<String>()
        .nfc()
        .collect()
}

fn invalid<T>(message: impl Into<String>) -> Result<T, FolderbaseVersionError> {
    Err(invalid_error(message))
}

fn invalid_error(message: impl Into<String>) -> FolderbaseVersionError {
    FolderbaseVersionError::InvalidVersion(message.into())
}

#[derive(Deserialize)]
struct EntryCountProbe {
    bindings: EntryCount,
    tombstones: EntryCount,
    exclusions: EntryCount,
}

impl EntryCountProbe {
    fn total(&self) -> usize {
        self.bindings
            .0
            .saturating_add(self.tombstones.0)
            .saturating_add(self.exclusions.0)
    }
}

struct EntryCount(usize);

impl<'de> Deserialize<'de> for EntryCount {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_seq(EntryCountVisitor)
    }
}

struct EntryCountVisitor;

impl<'de> Visitor<'de> for EntryCountVisitor {
    type Value = EntryCount;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a bounded Folderbase Version entry array")
    }

    fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        let mut count = 0_usize;
        while sequence.next_element::<IgnoredAny>()?.is_some() {
            count = count.saturating_add(1);
        }
        Ok(EntryCount(count))
    }
}

fn deserialize_bounded_bindings<'de, D>(deserializer: D) -> Result<Vec<PathBinding>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    deserialize_bounded_entries(deserializer, "bindings")
}

fn deserialize_bounded_tombstones<'de, D>(deserializer: D) -> Result<Vec<Tombstone>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    deserialize_bounded_entries(deserializer, "tombstones")
}

fn deserialize_bounded_exclusions<'de, D>(deserializer: D) -> Result<Vec<Exclusion>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    deserialize_bounded_entries(deserializer, "exclusions")
}

fn deserialize_bounded_entries<'de, D, T>(
    deserializer: D,
    label: &'static str,
) -> Result<Vec<T>, D::Error>
where
    D: serde::Deserializer<'de>,
    T: Deserialize<'de>,
{
    deserializer.deserialize_seq(BoundedEntriesVisitor {
        label,
        marker: PhantomData,
    })
}

struct BoundedEntriesVisitor<T> {
    label: &'static str,
    marker: PhantomData<T>,
}

impl<'de, T> Visitor<'de> for BoundedEntriesVisitor<T>
where
    T: Deserialize<'de>,
{
    type Value = Vec<T>;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "a bounded {} array", self.label)
    }

    fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        let capacity = sequence.size_hint().unwrap_or(0).min(MAX_VERSION_ENTRIES);
        let mut entries = Vec::with_capacity(capacity);
        while entries.len() < MAX_VERSION_ENTRIES {
            match sequence.next_element()? {
                Some(entry) => entries.push(entry),
                None => return Ok(entries),
            }
        }
        if sequence.next_element::<IgnoredAny>()?.is_some() {
            return Err(serde::de::Error::custom(format!(
                "{} exceeds the v1 entry limit",
                self.label
            )));
        }
        Ok(entries)
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
struct ExactU64(u64);

impl<'de> Deserialize<'de> for ExactU64 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let number = serde_json::Number::deserialize(deserializer)?;
        parse_exact_unsigned(&number.to_string())
            .filter(|value| *value <= MAX_OBJECT_BYTES)
            .map(Self)
            .ok_or_else(|| {
                serde::de::Error::custom(format!(
                    "byte length must be an exact integer from 0 through {MAX_OBJECT_BYTES}"
                ))
            })
    }
}

fn parse_exact_unsigned(encoded: &str) -> Option<u64> {
    let (negative, unsigned) = match encoded.strip_prefix('-') {
        Some(unsigned) => (true, unsigned),
        None => (false, encoded),
    };
    let (coefficient, exponent) = unsigned
        .split_once(['e', 'E'])
        .map_or((unsigned, "0"), |parts| parts);
    let exponent = exponent.parse::<i64>().ok()?;
    let (integer, fraction) = coefficient
        .split_once('.')
        .map_or((coefficient, ""), |parts| parts);
    let digits = format!("{integer}{fraction}");
    let significant = digits.trim_start_matches('0');
    if significant.is_empty() {
        return Some(0);
    }
    if negative {
        return None;
    }
    let scale = exponent.checked_sub(fraction.len() as i64)?;
    let mut normalized = significant.to_owned();
    if scale >= 0 {
        let zero_count = usize::try_from(scale).ok()?;
        if normalized.len().checked_add(zero_count)? > 20 {
            return None;
        }
        normalized.extend(std::iter::repeat_n('0', zero_count));
    } else {
        let removed = usize::try_from(scale.unsigned_abs()).ok()?;
        if removed > normalized.len() {
            return None;
        }
        let retained = normalized.len() - removed;
        if !normalized.as_bytes()[retained..]
            .iter()
            .all(|byte| *byte == b'0')
        {
            return None;
        }
        normalized.truncate(retained);
        if normalized.is_empty() {
            return Some(0);
        }
    }
    normalized.parse().ok()
}

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

    #[test]
    fn maximum_boundary_set_uses_at_most_path_depth_lookups_per_target() {
        let boundaries = (0..MAX_VERSION_ENTRIES / 2)
            .map(|index| format!("boundary-{index:04}"))
            .collect::<Vec<_>>();
        let index = NestedBoundaryIndex::from_paths(boundaries.iter().map(String::as_str));
        let resolved = (0..MAX_PATH_DEPTH)
            .map(|index| format!("ordinary-{index}"))
            .collect::<Vec<_>>()
            .join("/");
        let folded_resolved = portable_folded_key(&resolved);

        assert_eq!(index.len(), MAX_VERSION_ENTRIES / 2);
        assert_eq!(index.probe_count(&folded_resolved, true), MAX_PATH_DEPTH);
        assert!(!index.contains_or_ancestor(&folded_resolved));
    }
}