zond-engine 0.14.0

The core networking library powering Zond. Raw packet injection, protocol parsing, service and OS fingerprinting, and asynchronous network scanning.
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
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
// Copyright (c) 2026 Erik Lening (hollowpointer) and Contributors
//
// This file is part of Zond Engine, licensed under the GNU Affero General
// Public License, version 3 or later. See the LICENSE file for details, or
// <https://www.gnu.org/licenses/agpl-3.0.html>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! # The model, as data that can be written down and read back
//!
//! [`model`](crate::model) holds the domain: types with invariants, private
//! fields, and no opinion about serialization. This module is the same
//! information in a shape that survives a file.
//!
//! ## Why the model does not do this itself
//!
//! Deriving `serde` on [`Host`] and its neighbours would be two lines a type and
//! would be wrong in four ways. It welds the on-disk format to the struct layout,
//! so renaming a field breaks every file ever written. It bypasses the invariants
//! the constructors maintain, so a rebuilt host can have an `open_port_count`
//! that disagrees with its ports. It makes `Serialize` public API that cannot be
//! withdrawn. And it does not compile:
//! [`RttSample`](crate::model::host::telemetry::RttSample) carries an
//! [`Instant`](std::time::Instant), which `serde` declines to serialize since a
//! monotonic instant means nothing outside the process that read it.
//!
//! So the conversion lives here instead. Reading uses the model's getters and
//! rebuilding uses its constructors, which means a rebuilt value passed through
//! the same checks a scanned one did.
//!
//! ## What does not survive
//!
//! - **Round-trip time samples.** Their timestamps are monotonic
//!   [`Instant`](std::time::Instant)s, comparable only within one process. A
//!   rebuilt host keeps its summary statistics through the samples' durations and
//!   starts its history fresh.
//! - **Nothing else.** Every other field round-trips, and
//!   `a_fully_populated_host_survives_a_round_trip` keeps that true as the model
//!   grows.
//!
//! ## These are not `#[non_exhaustive]`
//!
//! Almost everything else public in this crate is, and it stops here because of
//! what these types are for. A record is interchange: something builds one to
//! hand over, and a struct literal naming every field is how that is written
//! down. Sealing them would trade a compile error for a caller who adds a field
//! against a builder per type for a caller who wants to state one.
//!
//! The cost is accepted. A field added here is a breaking change, and five of the
//! ones below arrived that way. What buys it back is that the serialized shape is
//! versioned where it matters, by
//! [`JOURNAL_VERSION`](crate::journal::JOURNAL_VERSION) for a journal and
//! [`SCHEMA_VERSION`](crate::format::SCHEMA_VERSION) for a report, so a reader is
//! told when a document has moved.
//!
//! ## Who uses it
//!
//! [`journal`](crate::journal) writes findings as a scan produces them and reads
//! them back to continue one. A differ over two scans wants the same reader, and
//! so would an importer richer than [`import::json`](crate::import), which is
//! narrow on purpose. Sitting beside the model rather than inside any one of them
//! is what lets all three share it.

pub mod wire;

use std::net::IpAddr;
use std::time::{Duration, SystemTime};

use serde::{Deserialize, Serialize};

use crate::config::DetectionEnvelope;
use crate::config::{IdleScan, RetryConfig, TimeoutScale};
use crate::model::capture::CaptureCounts;
use crate::model::confidence::Confidence;
use crate::model::finding::{
    DetectionClass, DetectionId, Excerpt, Finding, Reference, Severity, Version,
};
use crate::model::host::os::OsFingerprint;
use crate::model::host::path::Hop;
use crate::model::host::telemetry::HostTelemetry;
use crate::model::host::{HardwareInfo, Host, IpProtocolState, StatusProtocol, StatusReason};
use crate::model::host::{OsEvidence, OsSource};
use crate::model::ip::range::{IpRange, Ipv4Range, Ipv6Range};
use crate::model::ip::scoped::Zone;
use crate::model::ip::set::IpSet;
use crate::model::mac::MacAddr;
use crate::model::port::discovery::{Discovery, ScanResponse};
use crate::model::port::security::{CertificateInfo, Security};
use crate::model::port::{Port, PortSet, PortState, Protocol, Service};
use crate::model::target::{TargetMap, TargetSet};
use crate::model::tls::{CipherSuite, TlsSupport, TlsVersion, VersionSupport};
use crate::report::ScannerKind;
use crate::report::WindowSummary;
use crate::report::{
    Attachment, AttachmentSource, EvasionRecord, PhaseOrigin, PhaseParts, PortScope, ProbeStats,
    ProbeStatsParts, Refusal, ScanKind, ScanPhase, ScanSettings, ScannerFailure, ScopeParts,
    StopReason, TargetScope,
};
use crate::system::privilege::Privilege;
use std::num::{NonZeroU8, NonZeroU32};

/// One host, as a file holds it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct HostRecord {
    /// The address this host is reported under.
    pub primary_ip: IpAddr,
    /// Every address it is known to answer at, the primary included.
    #[serde(default)]
    pub ips: Vec<IpAddr>,
    /// Its resolved name, if one was found.
    #[serde(default)]
    pub hostname: Option<String>,
    /// Whether it answered, by wire name.
    pub status: String,
    /// What the status rests on.
    #[serde(default)]
    pub reasons: Vec<StatusReasonRecord>,
    /// What it was identified as running.
    #[serde(default)]
    pub os: Option<OsRecord>,
    /// What each source concluded about the operating system, kept so that a
    /// later source can corroborate an earlier one.
    #[serde(default)]
    pub os_evidence: Vec<OsEvidenceRecord>,
    /// Its hardware address and vendor, where the link layer reached it.
    #[serde(default)]
    pub hardware: Option<HardwareRecord>,
    /// The interface a link-local address is valid on.
    #[serde(default)]
    pub zone: Option<ZoneRecord>,
    /// Round-trip summary and hop counter.
    #[serde(default)]
    pub telemetry: TelemetryRecord,
    /// The routers between the scanning host and this one.
    #[serde(default)]
    pub path: Vec<HopRecord>,
    /// Roles inferred from where it sits or what it runs, by wire name.
    #[serde(default)]
    pub roles: Vec<String>,
    /// What the filter in front of the host was shown to be doing, by wire name.
    #[serde(default)]
    pub filtering: Vec<String>,
    /// What a scan concluded about each IP protocol it asked this host about,
    /// ascending by number. Empty for a scan that did not ask.
    ///
    /// A list rather than a map keyed by number, so that every key in this
    /// document is a field name. A map's keys would be data wearing the shape of
    /// field names, which is a difference nothing reading the file generically
    /// can see; the report writes the same list, one field wider.
    #[serde(default)]
    pub ip_protocols: Vec<IpProtocolRecord>,
    /// When it was first seen.
    pub first_seen: SystemTime,
    /// When it was last seen.
    pub last_seen: SystemTime,
    /// Its ports, in the order the model holds them.
    #[serde(default)]
    pub ports: Vec<PortRecord>,
    /// What a detection concluded was wrong with the host as a whole, in claim
    /// order.
    #[serde(default)]
    pub findings: Vec<FindingRecord>,
}

impl From<&Host> for HostRecord {
    fn from(host: &Host) -> Self {
        Self {
            primary_ip: host.primary_ip(),
            ips: host.ips().iter().copied().collect(),
            hostname: host.hostname().map(str::to_owned),
            status: wire::host_status_name(host.status()).to_owned(),
            // Sorted, because the model holds these in a set: two runs that
            // found the same things must write the same file, or a journal is
            // not comparable with itself.
            reasons: {
                let mut reasons: Vec<_> = host
                    .reasons()
                    .iter()
                    .map(StatusReasonRecord::from)
                    .collect();
                reasons.sort();
                reasons
            },
            os: host.os().map(OsRecord::from),
            os_evidence: host.os_evidence().map(OsEvidenceRecord::from).collect(),
            hardware: host.hardware().map(HardwareRecord::from),
            zone: host.zone().map(ZoneRecord::from),
            telemetry: TelemetryRecord::from(host.telemetry()),
            path: host.path().hops().iter().map(HopRecord::from).collect(),
            roles: {
                let mut roles: Vec<_> = host
                    .network_roles()
                    .iter()
                    .map(|role| wire::network_role_name(*role).to_owned())
                    .collect();
                roles.sort();
                roles
            },
            filtering: {
                let mut filtering: Vec<_> = host
                    .filtering()
                    .iter()
                    .map(|f| wire::filtering_name(*f).to_owned())
                    .collect();
                filtering.sort();
                filtering
            },
            ip_protocols: host
                .ip_protocols()
                .iter()
                .map(|(number, state)| IpProtocolRecord {
                    protocol: *number,
                    state: wire::ip_protocol_state_name(*state).to_owned(),
                })
                .collect(),
            first_seen: host.first_seen(),
            last_seen: host.last_seen(),
            ports: host.ports().map(PortRecord::from).collect(),
            // Already claim-ordered by the model's map, so no sort is needed for
            // two runs to write the same file.
            findings: host.findings().map(FindingRecord::from).collect(),
        }
    }
}

impl From<&HostRecord> for Host {
    fn from(record: &HostRecord) -> Self {
        let mut host = Host::new(record.primary_ip);

        host.extend_ips(record.ips.iter().copied());
        if let Some(hostname) = &record.hostname {
            host.set_hostname(Some(hostname.clone()));
        }
        // An unrecognised name leaves the status where `Host::new` put it,
        // which is `Unknown`, the reading that claims least.
        if let Some(status) = wire::host_status(&record.status) {
            host.set_status(status);
        }
        for reason in &record.reasons {
            host.add_reason(reason.into());
        }
        if let Some(os) = &record.os {
            host.set_os(os.into());
        }
        for evidence in &record.os_evidence {
            host.record_os_evidence(evidence.into());
        }
        if let Some(hardware) = record.hardware.as_ref().and_then(HardwareRecord::rebuild) {
            host.set_hardware(hardware);
        }
        if let Some(zone) = &record.zone {
            host.set_zone(zone.into());
        }

        record.telemetry.restore(&mut host);
        for hop in &record.path {
            host.record_hop(hop.into());
        }
        for role in record.roles.iter().filter_map(|r| wire::network_role(r)) {
            host.add_network_role(role);
        }
        for filtering in record.filtering.iter().filter_map(|f| wire::filtering(f)) {
            host.add_filtering(filtering);
        }
        // A state name this build cannot read is one a later build wrote, and
        // `Unasked` is the state that claims nothing, which is the same reading
        // `PortRecord` gives an unrecognised port state.
        for entry in &record.ip_protocols {
            let state = wire::ip_protocol_state(&entry.state).unwrap_or(IpProtocolState::Unasked);
            host.record_ip_protocol(entry.protocol, state);
        }
        for port in &record.ports {
            host.add_port(port.into());
        }
        for finding in record.findings.iter().filter_map(FindingRecord::rebuild) {
            host.add_finding(finding);
        }

        // Last, because everything above moves `last_seen` forward as it goes.
        host.restore_seen(record.first_seen, record.last_seen);
        host
    }
}

/// One piece of evidence for a host's reachability.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct StatusReasonRecord {
    /// The protocol the finding came from, by wire name.
    pub protocol: String,
    /// The address that answered, where one did.
    #[serde(default)]
    pub source: Option<IpAddr>,
    /// What was observed, in words.
    #[serde(default)]
    pub details: Option<String>,
}

impl From<&StatusReason> for StatusReasonRecord {
    fn from(reason: &StatusReason) -> Self {
        Self {
            protocol: wire::status_protocol_name(&reason.protocol).into_owned(),
            source: reason.source,
            details: reason.details.as_ref().map(|d| d.to_string()),
        }
    }
}

impl From<&StatusReasonRecord> for StatusReason {
    fn from(record: &StatusReasonRecord) -> Self {
        let protocol = wire::status_protocol(&record.protocol)
            .unwrap_or_else(|| StatusProtocol::Custom(record.protocol.as_str().into()));

        let mut reason = StatusReason::new(protocol, "");
        reason.details = record.details.as_deref().map(Into::into);
        reason.source = record.source;
        reason
    }
}

/// What a host was identified as running.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OsRecord {
    /// The name to show.
    pub name: String,
    /// How sure the identification is, out of a hundred.
    pub accuracy: u8,
    /// What kind of box it is, such as `Printer`.
    #[serde(default)]
    pub device: Option<String>,
    /// The broad family, such as `Linux`.
    #[serde(default)]
    pub family: Option<String>,
    /// The release within the family.
    #[serde(default)]
    pub generation: Option<String>,
    /// Who makes it.
    #[serde(default)]
    pub vendor: Option<String>,
    /// The kernel, where one was named separately.
    #[serde(default)]
    pub kernel: Option<String>,
    /// The instruction set, such as `x86_64`.
    #[serde(default)]
    pub arch: Option<String>,
    /// How sure the detail beyond the family is.
    #[serde(default)]
    pub detail_accuracy: Option<u8>,
    /// What the identification rests on.
    #[serde(default)]
    pub evidence: Option<String>,
    /// Platform identifiers.
    #[serde(default)]
    pub cpes: Vec<String>,
}

impl From<&OsFingerprint> for OsRecord {
    fn from(os: &OsFingerprint) -> Self {
        Self {
            name: os.name().to_owned(),
            accuracy: os.accuracy(),
            family: os.family().map(str::to_owned),
            device: os.device().map(str::to_owned),
            generation: os.generation().map(str::to_owned),
            vendor: os.vendor().map(str::to_owned),
            kernel: os.kernel().map(str::to_owned),
            arch: os.arch().map(str::to_owned),
            detail_accuracy: os.detail_accuracy(),
            evidence: os.evidence().map(str::to_owned),
            cpes: os.cpes().iter().map(|cpe| cpe.to_string()).collect(),
        }
    }
}

impl From<&OsRecord> for OsFingerprint {
    fn from(record: &OsRecord) -> Self {
        let mut os = OsFingerprint::new(record.name.clone(), record.accuracy);
        if let Some(family) = &record.family {
            os = os.with_family(family.clone());
        }
        if let Some(device) = &record.device {
            os = os.with_device(device.clone());
        }
        if let Some(generation) = &record.generation {
            os = os.with_generation(generation.clone());
        }
        if let Some(vendor) = &record.vendor {
            os = os.with_vendor(vendor.clone());
        }
        if let Some(kernel) = &record.kernel {
            os = os.with_kernel(kernel.clone());
        }
        if let Some(arch) = &record.arch {
            os = os.with_arch(arch.clone());
        }
        if let Some(accuracy) = record.detail_accuracy {
            os = os.with_detail_accuracy(accuracy);
        }
        if let Some(evidence) = &record.evidence {
            os = os.with_evidence(evidence.clone());
        }
        for cpe in &record.cpes {
            os.add_cpe(cpe.clone());
        }
        os
    }
}

/// What one source concluded about a host's operating system.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OsEvidenceRecord {
    /// Which source said so, by wire name.
    pub source: String,
    /// The family it named, where it could name one.
    #[serde(default)]
    pub family: Option<String>,
    /// The device class it named, where it named one.
    #[serde(default)]
    pub device: Option<String>,
    /// The vendor, where it named one.
    #[serde(default)]
    pub vendor: Option<String>,
    /// The product, where it named one.
    #[serde(default)]
    pub product: Option<String>,
    /// The version, where it named one.
    #[serde(default)]
    pub version: Option<String>,
    /// The kernel, where it named one.
    #[serde(default)]
    pub kernel: Option<String>,
    /// The instruction set, where it named one.
    #[serde(default)]
    pub arch: Option<String>,
    /// A platform identifier, where it carried one.
    #[serde(default)]
    pub cpe: Option<String>,
    /// How sure it was, from zero to one.
    pub confidence: f32,
    /// What it read to conclude this.
    pub evidence: String,
}

impl From<&OsEvidence> for OsEvidenceRecord {
    fn from(evidence: &OsEvidence) -> Self {
        Self {
            source: wire::os_source_name(evidence.source).to_owned(),
            family: evidence.family.clone(),
            device: evidence.device.clone(),
            vendor: evidence.vendor.clone(),
            product: evidence.product.clone(),
            version: evidence.version.clone(),
            kernel: evidence.kernel.clone(),
            arch: evidence.arch.clone(),
            cpe: evidence.cpe.clone(),
            confidence: evidence.confidence,
            evidence: evidence.evidence.clone(),
        }
    }
}

impl From<&OsEvidenceRecord> for OsEvidence {
    fn from(record: &OsEvidenceRecord) -> Self {
        OsEvidence {
            // An unrecognised source reads as the weakest one, which is what a
            // reading this build cannot place is worth.
            source: wire::os_source(&record.source).unwrap_or(OsSource::Hostname),
            family: record.family.clone(),
            device: record.device.clone(),
            vendor: record.vendor.clone(),
            product: record.product.clone(),
            version: record.version.clone(),
            kernel: record.kernel.clone(),
            arch: record.arch.clone(),
            cpe: record.cpe.clone(),
            confidence: record.confidence,
            evidence: record.evidence.clone(),
        }
    }
}

/// Hardware addresses and the vendor they attribute to.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HardwareRecord {
    /// Each address and when it was last seen.
    pub macs: Vec<(String, SystemTime)>,
    /// The vendor, where something named it rather than it being read from an
    /// address block. A record rebuilt from addresses alone resolves its own.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub vendor: Option<String>,
    /// The model a service named.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub product: Option<String>,
    /// The line that model belongs to.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub family: Option<String>,
    /// The hardware's platform identifier.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cpe23: Option<String>,
    /// A finer designation than the product.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// The hardware revision, which is not the operating system's version.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    /// The unit's own serial number.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub serial_number: Option<String>,
}

impl From<&HardwareInfo> for HardwareRecord {
    fn from(hardware: &HardwareInfo) -> Self {
        let mut macs: Vec<(String, SystemTime)> = hardware
            .macs()
            .iter()
            .map(|(mac, at)| (mac.to_string(), *at))
            .collect();

        // The vendor comes from whichever address arrived first and is never
        // revised, but the model keeps its addresses sorted rather than in
        // arrival order, so a rebuild reading them back in order would resolve
        // the vendor from a different address and reach a different answer.
        //
        // Recording the one that produced it first is what makes the rebuild
        // agree, and it keeps a vendor out of the file: it stays derived from
        // the OUI table, so a record cannot assert one the table does not
        // support.
        if let Some(vendor) = hardware.vendor() {
            let source = macs.iter().position(|(mac, _)| {
                mac.parse::<MacAddr>()
                    .ok()
                    .and_then(|mac| crate::model::mac::vendor(&mac))
                    .is_some_and(|found| found == vendor)
            });
            if let Some(source) = source {
                macs.swap(0, source);
            }
        }

        // A vendor an address block produced stays out of the file, for the
        // reason above. One a service *stated* cannot be re-derived from
        // anything, so it is recorded: without this a NETGEAR ReadyNAS named by
        // its FTP banner comes back from a journal as an unnamed box.
        let derivable = hardware.vendor().is_some_and(|vendor| {
            macs.iter().any(|(mac, _)| {
                mac.parse::<MacAddr>()
                    .ok()
                    .and_then(|mac| crate::model::mac::vendor(&mac))
                    .is_some_and(|found| found == vendor)
            })
        });

        Self {
            macs,
            vendor: (!derivable)
                .then(|| hardware.vendor().map(str::to_string))
                .flatten(),
            product: hardware.product().map(str::to_string),
            family: hardware.family().map(str::to_string),
            cpe23: hardware.cpe23().map(str::to_string),
            model: hardware.model().map(str::to_string),
            version: hardware.hardware_version().map(str::to_string),
            serial_number: hardware.serial_number().map(str::to_string),
        }
    }
}

impl HardwareRecord {
    /// Rebuilds the hardware, or `None` where the record names no address this
    /// build can read.
    ///
    /// The vendor is not carried. It is derived from the address's OUI, so a
    /// rebuild resolves it from the same table the scan used, and a file cannot
    /// assert a vendor the OUI does not support.
    pub fn rebuild(&self) -> Option<HardwareInfo> {
        let mut macs = self
            .macs
            .iter()
            .filter_map(|(mac, at)| mac.parse::<MacAddr>().ok().map(|mac| (mac, *at)));

        // A record may hold no address at all: a host reached through a gateway
        // has no MAC to read, and a banner naming `Merit LILIN PDR M800`
        // describes the box without one.
        let described = HardwareInfo::described(crate::model::host::HardwareDescription {
            vendor: self.vendor.as_deref(),
            product: self.product.as_deref(),
            family: self.family.as_deref(),
            cpe23: self.cpe23.as_deref(),
            model: self.model.as_deref(),
            version: self.version.as_deref(),
            serial_number: self.serial_number.as_deref(),
        });

        let Some((first, first_at)) = macs.next() else {
            return described;
        };

        let mut hardware = HardwareInfo::new(first);
        hardware.record_mac_seen_at(first, first_at);
        for (mac, at) in macs {
            hardware.record_mac_seen_at(mac, at);
        }
        if let Some(described) = described {
            hardware.merge(described);
        }
        Some(hardware)
    }
}

/// The interface a link-local address is valid on.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ZoneRecord {
    /// The scope id, where the host resolved one.
    #[serde(default)]
    pub index: Option<u32>,
    /// The interface's name.
    pub name: String,
}

impl From<&Zone> for ZoneRecord {
    fn from(zone: &Zone) -> Self {
        Self {
            index: zone.index(),
            name: zone.name().to_owned(),
        }
    }
}

impl From<&ZoneRecord> for Zone {
    fn from(record: &ZoneRecord) -> Self {
        match record.index {
            Some(index) => Zone::new(index, record.name.clone()),
            None => Zone::unresolved(record.name.clone()),
        }
    }
}

/// A host's round-trip summary.
///
/// The samples themselves are not carried. Each is stamped with a monotonic
/// [`Instant`](std::time::Instant), which orders a history within one process
/// and means nothing outside it, so the durations are replayed and the history
/// starts fresh.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TelemetryRecord {
    /// The measured round trips, oldest first.
    #[serde(default)]
    pub rtts: Vec<Duration>,
    /// The hop counter the most recent reply arrived with.
    #[serde(default)]
    pub hop_counter: Option<u8>,
    /// Which probe the round trips were measured from, where they agree on one.
    ///
    /// One name for the list rather than one per sample. The figures are only
    /// worth a name when they share it, and a record that carried a protocol per
    /// duration would be paying per sample for a word the reader sees once.
    ///
    /// Absent in a record written before the field existed, which reads back as
    /// a set of figures that do not say what measured them. That is what they
    /// were.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rtt_protocol: Option<String>,
}

impl From<&HostTelemetry> for TelemetryRecord {
    fn from(telemetry: &HostTelemetry) -> Self {
        Self {
            rtts: telemetry
                .history()
                .iter()
                .map(|sample| sample.rtt)
                .collect(),
            hop_counter: telemetry.hop_counter(),
            rtt_protocol: telemetry
                .rtt_protocol()
                .map(|protocol| wire::status_protocol_name(&protocol).into_owned()),
        }
    }
}

impl TelemetryRecord {
    /// Replays the measurements onto `host`.
    fn restore(&self, host: &mut Host) {
        match self.rtt_protocol.as_deref().and_then(wire::status_protocol) {
            Some(protocol) => {
                for rtt in &self.rtts {
                    host.add_rtt_from(*rtt, protocol.clone());
                }
            }
            None => host.add_rtts(self.rtts.iter().copied()),
        }
        if let Some(arrived) = self.hop_counter {
            host.record_hop_counter(arrived);
        }
    }
}

/// One router between the scanning host and the target.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HopRecord {
    /// How many routers out it sits.
    pub distance: u8,
    /// Its address, where it answered.
    #[serde(default)]
    pub address: Option<IpAddr>,
    /// The measured round trip to it.
    #[serde(default)]
    pub rtt: Option<Duration>,
    /// Whether it was spliced from another host's path rather than measured.
    #[serde(default)]
    pub inferred: bool,
}

impl From<&Hop> for HopRecord {
    fn from(hop: &Hop) -> Self {
        Self {
            distance: hop.distance(),
            address: hop.address(),
            rtt: hop.rtt(),
            inferred: hop.inferred(),
        }
    }
}

impl From<&HopRecord> for Hop {
    fn from(record: &HopRecord) -> Self {
        let hop = match record.address {
            Some(address) => Hop::answered(record.distance, address, record.rtt),
            None => Hop::silent(record.distance),
        };

        if record.inferred {
            hop.as_inferred()
        } else {
            hop
        }
    }
}

/// One port and what was established about it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PortRecord {
    /// The port number.
    pub port: u16,
    /// The transport it was reached over, by wire name.
    pub protocol: String,
    /// What a probe established, by wire name.
    pub state: String,
    /// What is listening, where it was identified.
    #[serde(default)]
    pub service: Option<ServiceRecord>,
    /// What a TLS handshake negotiated.
    #[serde(default)]
    pub security: Option<SecurityRecord>,
    /// The packet that settled the state.
    #[serde(default)]
    pub discovery: Option<DiscoveryRecord>,
    /// What a detection concluded was wrong with what is listening here, in
    /// claim order.
    #[serde(default)]
    pub findings: Vec<FindingRecord>,
}

impl From<&Port> for PortRecord {
    fn from(port: &Port) -> Self {
        Self {
            port: port.number(),
            protocol: wire::protocol_name(port.protocol()).to_owned(),
            state: wire::port_state_name(port.state()).to_owned(),
            service: port.service().map(ServiceRecord::from),
            security: port.security().map(SecurityRecord::from),
            discovery: port.discovery().map(DiscoveryRecord::from),
            // Already claim-ordered by the model's map.
            findings: port.findings().map(FindingRecord::from).collect(),
        }
    }
}

impl From<&PortRecord> for Port {
    fn from(record: &PortRecord) -> Self {
        // An unrecognised transport or state reads as the least this engine
        // could have established, never as more. A state name this build cannot
        // read is one a later build wrote, and `Unasked` is the state that holds
        // no verdict at all.
        let protocol = wire::protocol(&record.protocol).unwrap_or(Protocol::Tcp);
        let state = wire::port_state(&record.state).unwrap_or(PortState::Unasked);

        let mut port = Port::new(record.port, protocol, state);
        if let Some(service) = &record.service {
            port = port.with_service(service.into());
        }
        if let Some(security) = &record.security {
            port = port.with_security(security.into());
        }
        if let Some(discovery) = &record.discovery {
            port = port.with_discovery(discovery.into());
        }
        for finding in record.findings.iter().filter_map(FindingRecord::rebuild) {
            port.add_finding(finding);
        }
        port
    }
}

/// What a scan concluded about one IP protocol on one host, as a file holds it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IpProtocolRecord {
    /// The IP protocol number.
    pub protocol: u8,
    /// What the scan established, by wire name.
    pub state: String,
}

/// A finding, as a file holds it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FindingRecord {
    /// Which detection produced it, to which version, from which bytes.
    pub detection: DetectionIdRecord,
    /// Its one-line title.
    pub title: String,
    /// How bad it is if true, by wire name.
    pub severity: String,
    /// How sure it is true, by wire name.
    pub confidence: String,
    /// The intrusiveness the detection ran under, by wire name.
    pub class: String,
    /// The bytes that justify it, if any.
    #[serde(default)]
    pub excerpt: Option<String>,
    /// Its external references, in sorted order.
    #[serde(default)]
    pub references: Vec<ReferenceRecord>,
    /// Remediation advice, if any.
    ///
    /// Omitted when absent, as the report's own field is. The two documents
    /// describe the same finding and `export::conformance` holds them to
    /// spelling it the same way; a journal that wrote `null` where the report
    /// wrote nothing was a difference nobody had noticed because every entry in
    /// the shipped catalogue used to carry advice. A converted feed carries
    /// none, and the asymmetry surfaced the moment one was shipped.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub remediation: Option<String>,
}

impl From<&Finding> for FindingRecord {
    fn from(finding: &Finding) -> Self {
        Self {
            detection: DetectionIdRecord::from(finding.detection()),
            title: finding.title().to_owned(),
            severity: wire::severity_name(finding.severity()).to_owned(),
            confidence: wire::confidence_name(finding.confidence()).to_owned(),
            class: wire::detection_class_name(finding.class()).to_owned(),
            excerpt: (!finding.excerpt().is_empty()).then(|| finding.excerpt().as_str().to_owned()),
            references: finding.references().map(ReferenceRecord::from).collect(),
            remediation: finding.remediation().map(str::to_owned),
        }
    }
}

impl FindingRecord {
    /// Rebuilds the finding, or [`None`] if it names no detection or has no
    /// title, the two things a finding cannot be without.
    ///
    /// Everything softer reads downward rather than failing. An unknown severity,
    /// confidence or class reads as the least it could claim, and a reference
    /// that will not rebuild is dropped while the finding is kept, since
    /// provenance that does not parse is no reason to discard a real finding.
    pub fn rebuild(&self) -> Option<Finding> {
        let severity = wire::severity(&self.severity).unwrap_or(Severity::Info);
        let confidence = wire::confidence(&self.confidence).unwrap_or(Confidence::Heuristic);
        let class = wire::detection_class(&self.class).unwrap_or(DetectionClass::Passive);

        let mut finding = Finding::new(
            self.detection.rebuild()?,
            self.title.clone(),
            severity,
            confidence,
            class,
        )
        .ok()?;

        if let Some(excerpt) = &self.excerpt {
            finding = finding.with_excerpt(Excerpt::new(excerpt.clone()));
        }
        if let Some(remediation) = &self.remediation {
            finding = finding.with_remediation(remediation.clone());
        }
        for reference in self.references.iter().filter_map(ReferenceRecord::rebuild) {
            finding = finding.with_reference(reference);
        }
        Some(finding)
    }
}

/// A detection identity, as a file holds it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DetectionIdRecord {
    /// The author-chosen identifier.
    pub id: String,
    /// Its version, as `major.minor.patch`.
    pub version: String,
    /// The content hash of the detection body.
    pub content_hash: String,
}

impl From<&DetectionId> for DetectionIdRecord {
    fn from(detection: &DetectionId) -> Self {
        Self {
            id: detection.id().to_owned(),
            version: detection.version().to_string(),
            content_hash: detection.content_hash().to_owned(),
        }
    }
}

impl DetectionIdRecord {
    /// Rebuilds the identity, or [`None`] if it names nothing. A version that
    /// will not parse reads as `0.0.0`, the earliest and least trusted, rather
    /// than discarding the finding it identifies.
    pub fn rebuild(&self) -> Option<DetectionId> {
        let version = self.version.parse().unwrap_or(Version::new(0, 0, 0));
        DetectionId::new(self.id.clone(), version, self.content_hash.clone()).ok()
    }
}

/// An external reference, as a file holds it: a kind and the value it carries.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReferenceRecord {
    /// Which kind of reference, by wire name.
    pub kind: String,
    /// The value it carries: a CVE id, a CWE number, or a URL.
    pub value: String,
}

impl From<&Reference> for ReferenceRecord {
    fn from(reference: &Reference) -> Self {
        Self {
            kind: wire::reference_kind_name(reference).to_owned(),
            value: match reference {
                Reference::Cve(id) | Reference::Url(id) => id.clone(),
                Reference::Cwe(number) => number.to_string(),
            },
        }
    }
}

impl ReferenceRecord {
    /// Rebuilds the reference, or [`None`] for an unknown kind or a malformed
    /// value.
    pub fn rebuild(&self) -> Option<Reference> {
        wire::reference(&self.kind, &self.value)
    }
}

/// What is listening on a port.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ServiceRecord {
    /// What it is, such as `http`.
    pub name: String,
    /// How sure the identification is, out of a hundred.
    pub confidence: u8,
    /// The product behind it.
    #[serde(default)]
    pub product: Option<String>,
    /// Who makes it.
    #[serde(default)]
    pub vendor: Option<String>,
    /// Its version.
    #[serde(default)]
    pub version: Option<String>,
    /// Detail that is not the product itself.
    #[serde(default)]
    pub extrainfo: Option<String>,
    /// Platform identifiers.
    #[serde(default)]
    pub cpes: Vec<String>,
}

impl From<&Service> for ServiceRecord {
    fn from(service: &Service) -> Self {
        Self {
            name: service.name().to_owned(),
            confidence: service.confidence(),
            product: service.product().map(str::to_owned),
            vendor: service.vendor().map(str::to_owned),
            version: service.version().map(str::to_owned),
            extrainfo: service.extrainfo().map(str::to_owned),
            cpes: service.cpes().iter().map(|cpe| cpe.to_string()).collect(),
        }
    }
}

impl From<&ServiceRecord> for Service {
    fn from(record: &ServiceRecord) -> Self {
        let mut service = Service::new(record.name.clone(), record.confidence);
        if let Some(product) = &record.product {
            service = service.with_product(product.clone());
        }
        if let Some(vendor) = &record.vendor {
            service = service.with_vendor(vendor.clone());
        }
        if let Some(version) = &record.version {
            service = service.with_version(version.clone());
        }
        if let Some(extrainfo) = &record.extrainfo {
            service = service.with_extrainfo(extrainfo.clone());
        }
        for cpe in &record.cpes {
            service.add_cpe(cpe.clone());
        }
        service
    }
}

/// What a TLS handshake negotiated.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SecurityRecord {
    /// The protocol version agreed.
    #[serde(default)]
    pub tls_version: Option<String>,
    /// The cipher suite agreed.
    #[serde(default)]
    pub cipher_suite: Option<String>,
    /// The protocols offered over ALPN.
    #[serde(default)]
    pub alpn: Vec<String>,
    /// The certificate presented.
    #[serde(default)]
    pub certificate: Option<CertificateRecord>,
    /// What the endpoint accepts, one entry per version, where a scan asked.
    ///
    /// Skipped when empty, which is every sitting that did not enumerate, and
    /// defaulted on the way in so a record written before the pass existed
    /// reads back as one that did not.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub accepts: Vec<AcceptedVersionRecord>,
}

/// One version an endpoint accepted, and the suites chosen under it.
///
/// Suites are written as their wire numbers rather than their names. A number is
/// what the server actually sent, it is half the size, and a build that has since
/// added or renamed a suite reads an old record correctly instead of matching on
/// a string it no longer uses. A number this build does not carry reads back into
/// `unrecognised`, which is where it belonged in the first place.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AcceptedVersionRecord {
    /// The version, by the name it prints.
    pub version: String,
    /// The suites accepted, by wire number, in the order the server chose them.
    #[serde(default)]
    pub suites: Vec<u16>,
    /// Suites the scanning build could not name, by wire number.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub unrecognised: Vec<u16>,
}

impl From<&Security> for SecurityRecord {
    fn from(security: &Security) -> Self {
        Self {
            tls_version: security.tls_version().map(str::to_owned),
            cipher_suite: security.cipher_suite().map(str::to_owned),
            alpn: security.alpn().iter().map(|p| p.to_string()).collect(),
            certificate: security.certificate().map(CertificateRecord::from),
            accepts: security
                .support()
                .versions()
                .iter()
                .map(|held| AcceptedVersionRecord {
                    version: held.version().name().to_owned(),
                    suites: held.suites().iter().map(|suite| suite.code()).collect(),
                    unrecognised: held.unrecognised().to_vec(),
                })
                .collect(),
        }
    }
}

impl From<&SecurityRecord> for Security {
    fn from(record: &SecurityRecord) -> Self {
        let mut security = Security::new();
        if let Some(version) = &record.tls_version {
            security = security.with_tls_version(version.clone());
        }
        if let Some(cipher) = &record.cipher_suite {
            security = security.with_cipher_suite(cipher.clone());
        }
        for protocol in &record.alpn {
            security.add_alpn(protocol.clone());
        }
        if let Some(certificate) = &record.certificate {
            security = security.with_certificate(certificate.into());
        }

        let mut support = TlsSupport::new();
        for held in &record.accepts {
            // A version this build cannot name is dropped rather than guessed
            // at: there is nothing to file its suites under.
            let Ok(version) = held.version.parse::<TlsVersion>() else {
                continue;
            };
            let mut suites = Vec::with_capacity(held.suites.len());
            let mut unrecognised = held.unrecognised.clone();
            for code in &held.suites {
                match CipherSuite::from_code(*code) {
                    Some(suite) => suites.push(suite),
                    // Written by a build that carried it and read by one that
                    // does not. Kept as a number, which is what it always was.
                    None => unrecognised.push(*code),
                }
            }
            support.record(VersionSupport::new(version, suites, unrecognised));
        }
        security.set_support(support);

        security
    }
}

/// The certificate a TLS endpoint presented.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CertificateRecord {
    /// Who it is for.
    pub common_name: String,
    /// The other names it covers.
    #[serde(default)]
    pub sans: Vec<String>,
    /// Who signed it.
    pub issuer: String,
    /// When it becomes valid.
    pub validity_start: SystemTime,
    /// When it stops being valid.
    pub validity_end: SystemTime,
    /// The SHA-256 of the certificate.
    pub fingerprint_sha256: String,
    /// The public key's algorithm, or `unknown` where it was not read.
    #[serde(default)]
    pub pubkey_type: String,
    /// The public key's size in bits, or zero where it was not read.
    #[serde(default)]
    pub pubkey_bits: u32,
}

impl From<&CertificateInfo> for CertificateRecord {
    fn from(certificate: &CertificateInfo) -> Self {
        Self {
            common_name: certificate.common_name().to_owned(),
            sans: certificate.sans().iter().map(|s| s.to_string()).collect(),
            issuer: certificate.issuer().to_owned(),
            validity_start: certificate.validity_start(),
            validity_end: certificate.validity_end(),
            fingerprint_sha256: certificate.fingerprint_sha256().to_owned(),
            pubkey_type: certificate.pubkey_type().to_owned(),
            pubkey_bits: certificate.pubkey_bits(),
        }
    }
}

impl From<&CertificateRecord> for CertificateInfo {
    fn from(record: &CertificateRecord) -> Self {
        CertificateInfo::new(
            record.common_name.clone(),
            record.issuer.clone(),
            record.validity_start,
            record.validity_end,
            record.fingerprint_sha256.clone(),
        )
        .with_sans(record.sans.iter().map(|s| s.as_str().into()))
        .with_public_key(record.pubkey_type.clone(), record.pubkey_bits)
    }
}

/// The packet that settled a port's state.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DiscoveryRecord {
    /// What came back, by wire name.
    pub reason: String,
    /// When it arrived.
    pub timestamp: SystemTime,
    /// The measured round trip.
    #[serde(default)]
    pub rtt: Option<Duration>,
    /// The hop counter it carried.
    #[serde(default)]
    pub ttl: Option<u8>,
    /// Where it came from.
    #[serde(default)]
    pub source_ip: Option<IpAddr>,
}

impl From<&Discovery> for DiscoveryRecord {
    fn from(discovery: &Discovery) -> Self {
        Self {
            reason: wire::scan_response_name(discovery.reason()).into_owned(),
            timestamp: discovery.timestamp(),
            rtt: discovery.rtt(),
            ttl: discovery.ttl(),
            source_ip: discovery.source_ip(),
        }
    }
}

impl From<&DiscoveryRecord> for Discovery {
    fn from(record: &DiscoveryRecord) -> Self {
        let reason = wire::scan_response(&record.reason)
            .unwrap_or_else(|| ScanResponse::Custom(record.reason.clone()));

        let mut discovery = Discovery::new(reason).seen_at(record.timestamp);
        if let Some(rtt) = record.rtt {
            discovery = discovery.with_rtt(rtt);
        }
        if let Some(ttl) = record.ttl {
            discovery = discovery.with_ttl(ttl);
        }
        if let Some(source) = record.source_ip {
            discovery = discovery.with_source_ip(source);
        }
        discovery
    }
}

/// One sitting of a scan, as a file holds it.
///
/// Mirrors [`PhaseParts`], which mirrors the
/// phase. A field added to any of the three has to be added to all of them, and
/// the compiler says so.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PhaseRecord {
    /// Which entry point this sitting recorded, by wire name.
    pub kind: String,
    /// When it began.
    pub started_at: SystemTime,
    /// How long it ran.
    pub elapsed: Duration,
    /// Whether it held the privileges its raw strategies need, or absent where
    /// this engine did not measure the phase and cannot say.
    pub privileged: Option<bool>,
    /// What it covered, and what it was forbidden.
    pub targets: ScopeRecord,
    /// The settings it ran under.
    pub settings: SettingsRecord,
    /// The strategies that could not do their job.
    #[serde(default)]
    pub failures: Vec<FailureRecord>,
    /// Ground the sitting declined before sending anything.
    ///
    /// Skipped when empty, which is most sittings, and defaulted on the way in,
    /// so a record written before this field existed reads back as a sitting
    /// that refused nothing. That is the honest reading: a refusal was written
    /// down as a failure then, and it stays a failure rather than being invented
    /// as a refusal now.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub refusals: Vec<RefusalRecord>,
    /// Addresses the scanning host had no route to.
    #[serde(default)]
    pub unroutable: Vec<IpAddr>,
    /// Addresses the sitting left before it had finished with them, because
    /// their own budget ran out.
    ///
    /// Skipped when empty, which is every sitting that set no per-host budget,
    /// and defaulted on the way in.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub timed_out: Vec<IpAddr>,
    /// What each strategy recorded about its own run.
    #[serde(default)]
    pub probe_stats: Vec<ProbeStatsRecord>,
    /// Which document this sitting was folded in from, for a merged report.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub origin: Option<PhaseOriginRecord>,
    /// Which switch ports the machine running this sitting was plugged into.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub attachments: Vec<AttachmentRecord>,
}

/// Where the machine running a sitting was plugged in, as a file holds it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttachmentRecord {
    /// Which of the scanning machine's interfaces the announcement arrived on.
    pub link: String,
    /// The interface index, where the reading host knew it. Absent rather than
    /// zero, since zero is what "no interface" means to a kernel.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub link_index: Option<u32>,
    /// Which protocol the announcement was read from.
    pub source: String,
    /// The hardware address the device identified its chassis with.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub device_mac: Option<String>,
    /// What the device calls itself.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub device_name: Option<String>,
    /// What the device calls the port this machine is plugged into.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub port: Option<String>,
    /// The VLAN untagged traffic on this port lands in.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub native_vlan: Option<u16>,
    /// An address the device is managed at.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub management_address: Option<IpAddr>,
    /// When the announcement arrived.
    pub observed_at: SystemTime,
}

impl From<&Attachment> for AttachmentRecord {
    fn from(attachment: &Attachment) -> Self {
        Self {
            link: attachment.link().name().to_owned(),
            link_index: attachment.link().index(),
            source: wire::attachment_source_name(attachment.source()).to_owned(),
            device_mac: attachment.device_mac().map(|mac| mac.to_string()),
            device_name: attachment.device_name().map(str::to_owned),
            port: attachment.port().map(str::to_owned),
            native_vlan: attachment.native_vlan(),
            management_address: attachment.management_address(),
            observed_at: attachment.observed_at(),
        }
    }
}

impl From<&AttachmentRecord> for Attachment {
    fn from(record: &AttachmentRecord) -> Self {
        let link = match record.link_index {
            Some(index) => Zone::new(index, record.link.as_str()),
            None => Zone::unresolved(record.link.as_str()),
        };

        // A source this build cannot place is read as LLDP: it is the standard
        // one, and the field says which protocol carried the fields rather than
        // deciding what any of them mean.
        let source = wire::attachment_source(&record.source).unwrap_or(AttachmentSource::Lldp);

        let mut attachment = Attachment::new(link, source, record.observed_at);
        if let Some(mac) = record
            .device_mac
            .as_deref()
            .and_then(|mac| mac.parse().ok())
        {
            attachment = attachment.with_device_mac(mac);
        }
        if let Some(name) = record.device_name.as_deref() {
            attachment = attachment.with_device_name(name);
        }
        if let Some(port) = record.port.as_deref() {
            attachment = attachment.with_port(port);
        }
        if let Some(vlan) = record.native_vlan {
            attachment = attachment.with_native_vlan(vlan);
        }
        if let Some(address) = record.management_address {
            attachment = attachment.with_management_address(address);
        }
        attachment
    }
}

/// Which document a sitting came from, as a file holds it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PhaseOriginRecord {
    /// What the caller called the document it was read from.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// What produced it, as that scanner attributed itself.
    pub engine_version: String,
}

impl From<&PhaseOrigin> for PhaseOriginRecord {
    fn from(origin: &PhaseOrigin) -> Self {
        Self {
            label: origin.label().map(str::to_owned),
            engine_version: origin.engine_version().to_owned(),
        }
    }
}

impl From<&PhaseOriginRecord> for PhaseOrigin {
    fn from(record: &PhaseOriginRecord) -> Self {
        let origin = PhaseOrigin::new(record.engine_version.as_str());
        match &record.label {
            Some(label) => origin.with_label(label.as_str()),
            None => origin,
        }
    }
}

impl From<&ScanPhase> for PhaseRecord {
    fn from(phase: &ScanPhase) -> Self {
        Self {
            kind: wire::scan_kind_name(phase.kind()).to_owned(),
            started_at: phase.started_at(),
            elapsed: phase.elapsed(),
            privileged: phase.privilege().map(Privilege::is_raw),
            targets: ScopeRecord::from(phase.targets()),
            settings: SettingsRecord::from(phase.settings()),
            failures: phase.failures().iter().map(FailureRecord::from).collect(),
            refusals: phase.refusals().iter().map(RefusalRecord::from).collect(),
            unroutable: phase.unroutable().to_vec(),
            timed_out: phase.timed_out().to_vec(),
            probe_stats: phase
                .probe_stats()
                .iter()
                .map(ProbeStatsRecord::from)
                .collect(),
            origin: phase.origin().map(PhaseOriginRecord::from),
            attachments: phase
                .attachments()
                .iter()
                .map(AttachmentRecord::from)
                .collect(),
        }
    }
}

impl From<&PhaseRecord> for ScanPhase {
    fn from(record: &PhaseRecord) -> Self {
        ScanPhase::from_parts(PhaseParts {
            // A sitting whose kind this build cannot place is read as a
            // discovery sweep. Of the kinds that exist it claims the least
            // while still claiming the addresses were walked: a reader is not
            // told ports were scanned, and is not told the sitting covered
            // nothing when its scope says it covered a range.
            kind: wire::scan_kind(&record.kind).unwrap_or(ScanKind::Discovery),
            started_at: record.started_at,
            elapsed: record.elapsed,
            privilege: record.privileged.map(Privilege::from_raw),
            targets: TargetScope::from(&record.targets),
            settings: ScanSettings::from(&record.settings),
            failures: record.failures.iter().map(ScannerFailure::from).collect(),
            refusals: record.refusals.iter().map(Refusal::from).collect(),
            unroutable: record.unroutable.clone(),
            timed_out: record.timed_out.clone(),
            probes: record.probe_stats.iter().map(ProbeStats::from).collect(),
            origin: record.origin.as_ref().map(PhaseOrigin::from),
            attachments: record.attachments.iter().map(Attachment::from).collect(),
        })
    }
}

/// What a sitting was asked to cover, and what it was forbidden.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScopeRecord {
    /// The ranges walked, after exclusions, as `start-end`.
    #[serde(default)]
    pub ranges: Vec<RangeRecord>,
    /// The links swept whole, by the interface each is on. Empty for a phase that
    /// swept no segment, and for one recorded before this field existed. The two
    /// read back the same way, since neither claims a link was covered.
    #[serde(default)]
    pub links: Vec<ZoneRecord>,
    /// The links whose traffic was read without anything being probed on them.
    /// Never coverage; see `TargetScope::listened`. Empty for a phase that
    /// sent probes, and for one recorded before this field existed.
    #[serde(default)]
    pub listened: Vec<ZoneRecord>,
    /// How many distinct addresses those hold.
    pub addresses: u128,
    /// How many probes the scope implies, where ports were known.
    #[serde(default)]
    pub probes: Option<u128>,
    /// Which ports were walked, and whether uniformly. Absent for a phase that
    /// walked no ports, and for one recorded before this field existed. The two
    /// read back the same way, since neither can say which ports were probed.
    #[serde(default)]
    pub ports: Option<PortsRecord>,
    /// The transports covered, by wire name.
    #[serde(default)]
    pub protocols: Vec<String>,
    /// The ranges the policy withheld.
    #[serde(default)]
    pub excluded: Vec<RangeRecord>,
    /// How many addresses that withheld.
    pub withheld: u128,
}

impl From<&TargetScope> for ScopeRecord {
    fn from(scope: &TargetScope) -> Self {
        Self {
            ranges: scope.ranges().iter().map(RangeRecord::from).collect(),
            links: scope.links().iter().map(ZoneRecord::from).collect(),
            listened: scope.listened().iter().map(ZoneRecord::from).collect(),
            addresses: scope.addresses(),
            probes: scope.probes(),
            ports: PortsRecord::of(scope.ports()),
            protocols: scope
                .protocols()
                .iter()
                .map(|p| wire::protocol_name(*p).to_owned())
                .collect(),
            excluded: scope.excluded().iter().map(RangeRecord::from).collect(),
            withheld: scope.withheld(),
        }
    }
}

impl From<&ScopeRecord> for TargetScope {
    fn from(record: &ScopeRecord) -> Self {
        TargetScope::from_parts(ScopeParts {
            ranges: record
                .ranges
                .iter()
                .filter_map(RangeRecord::rebuild)
                .collect(),
            links: record.links.iter().map(Zone::from).collect(),
            listened: record.listened.iter().map(Zone::from).collect(),
            addresses: record.addresses,
            probes: record.probes,
            ports: record
                .ports
                .as_ref()
                .map_or(PortScope::Unstated, PortsRecord::rebuild),
            protocols: record
                .protocols
                .iter()
                .filter_map(|p| wire::protocol(p))
                .collect(),
            excluded: record
                .excluded
                .iter()
                .filter_map(RangeRecord::rebuild)
                .collect(),
            withheld: record.withheld,
        })
    }
}

/// A phase's port scope, as a file holds it.
///
/// The set is written as the specification
/// [`PortSet`] parses, so a full sweep is six bytes
/// rather than a hundred and thirty thousand entries, and a person reading the
/// file can see what was scanned.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PortsRecord {
    /// Which of the four things the set below is, by wire name.
    ///
    /// The single most important field here, and the reason a bare
    /// specification would not do. `every` is the only value from which a
    /// reader may conclude that one endpoint of a covered address was probed.
    pub kind: String,
    /// The ports, as the specification
    /// [`PortSet`] parses. Empty where the kind
    /// carries no set.
    #[serde(default)]
    pub spec: String,
}

impl PortsRecord {
    /// The record of a port scope, or `None` where the scope states nothing,
    /// which is what a record written before this field existed reads back as.
    pub fn of(scope: &PortScope) -> Option<Self> {
        match scope {
            PortScope::Unstated => None,
            other => Some(Self {
                kind: wire::port_scope_name(other).to_owned(),
                spec: other.ports().map(PortSet::to_string).unwrap_or_default(),
            }),
        }
    }

    /// The scope this records.
    ///
    /// A kind this build does not know, or a specification that will not parse,
    /// rebuilds as [`Unstated`](PortScope::Unstated), the reading that claims
    /// nothing. An unreadable set is not evidence that any port was walked, nor
    /// that none was.
    pub fn rebuild(&self) -> PortScope {
        let ports = PortSet::try_from(self.spec.as_str())
            .ok()
            .filter(|ports| !ports.is_empty());

        wire::port_scope(&self.kind, ports).unwrap_or(PortScope::Unstated)
    }
}

/// One address range, by its ends.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RangeRecord {
    /// The first address in the range.
    pub start: IpAddr,
    /// The last address in the range.
    pub end: IpAddr,
    /// The interface a link-local range is valid on.
    #[serde(default)]
    pub zone: Option<u32>,
}

impl From<&IpRange> for RangeRecord {
    fn from(range: &IpRange) -> Self {
        Self {
            start: range.start_addr(),
            end: range.end_addr(),
            zone: match range {
                IpRange::V6(range) => range.zone(),
                IpRange::V4(_) => None,
            },
        }
    }
}

impl RangeRecord {
    /// Rebuilds the range, or `None` where its ends do not describe one, meaning
    /// a mixed pair or an end before its start.
    pub fn rebuild(&self) -> Option<IpRange> {
        match (self.start, self.end) {
            (IpAddr::V4(start), IpAddr::V4(end)) => {
                Ipv4Range::new(start, end).ok().map(IpRange::V4)
            }
            (IpAddr::V6(start), IpAddr::V6(end)) => Ipv6Range::scoped(start, end, self.zone)
                .ok()
                .map(IpRange::V6),
            _ => None,
        }
    }
}

/// Ground a sitting declined to cover.
///
/// No timestamp, unlike [`FailureRecord`], and the difference is the point: a
/// failure happened at a moment and a refusal was decided before the phase
/// began. Recording a clock reading for one would invite a reader to order it
/// against the failures, which says nothing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RefusalRecord {
    /// Which strategy would have taken the work, by wire name.
    pub scanner: String,
    /// What was not done, and what could be asked for instead.
    pub reason: String,
}

impl From<&Refusal> for RefusalRecord {
    fn from(refusal: &Refusal) -> Self {
        Self {
            scanner: wire::scanner_kind_name(refusal.scanner()).to_owned(),
            reason: refusal.reason().to_owned(),
        }
    }
}

impl From<&RefusalRecord> for Refusal {
    fn from(record: &RefusalRecord) -> Self {
        Refusal::new(
            wire::scanner_kind(&record.scanner).unwrap_or(ScannerKind::Composite),
            record.reason.clone(),
        )
    }
}

/// A strategy that could not do its job.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FailureRecord {
    /// Which strategy, by wire name.
    pub scanner: String,
    /// What went wrong.
    pub reason: String,
    /// When it was recorded.
    pub at: SystemTime,
}

impl From<&ScannerFailure> for FailureRecord {
    fn from(failure: &ScannerFailure) -> Self {
        Self {
            scanner: wire::scanner_kind_name(failure.scanner()).to_owned(),
            reason: failure.reason().to_owned(),
            at: failure.at(),
        }
    }
}

impl From<&FailureRecord> for ScannerFailure {
    fn from(record: &FailureRecord) -> Self {
        ScannerFailure::new(
            wire::scanner_kind(&record.scanner).unwrap_or(ScannerKind::Composite),
            record.reason.clone(),
        )
        .recorded_at(record.at)
    }
}

/// The settings one sitting ran under.
///
/// The enums here already read and write their own names, so this carries those
/// rather than restating a vocabulary.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SettingsRecord {
    /// How raw probes were placed on the wire.
    pub send_mode: String,
    /// Which segment each TCP port probe carried.
    pub tcp_technique: String,
    /// Which chunk each SCTP port probe carried.
    ///
    /// Defaulted on the way in, so a record written before the technique was a
    /// choice reads back as the INIT scan it was.
    #[serde(default)]
    pub sctp_technique: String,
    /// The retransmission effort in force.
    pub retry_effort: String,
    /// A caller's override of the attempt budget.
    ///
    /// Written as a plain number, and read back through the bound the
    /// configuration enforces: a journal from a build that accepted zero is read
    /// as no override, which is what it was.
    #[serde(default)]
    pub retry_max_attempts: Option<u8>,
    /// A caller's scaling of the timeout. Read back the same way, so a scale a
    /// schedule could not have been built from reads as absent.
    #[serde(default)]
    pub retry_timeout_scale: Option<f64>,
    /// Whether silent hosts were probed less.
    pub retry_dampen_silent_hosts: bool,
    /// The probe-rate ceiling, where one applied.
    #[serde(default)]
    pub max_probe_rate: Option<u32>,
    /// The probe-rate floor, where one applied.
    ///
    /// Defaulted on the way in, so a record written before the floor existed
    /// reads back as a sitting that set none. That is what it was.
    #[serde(default)]
    pub min_probe_rate: Option<u32>,
    /// The shortest gap kept between two probes at one host, where one applied.
    #[serde(default)]
    pub host_probe_interval: Option<Duration>,
    /// The wall-clock budget each host was given, where one applied.
    ///
    /// Defaulted on the way in, so a record written before the budget existed
    /// reads back as a sitting that set none. That is what it was.
    #[serde(default)]
    pub host_timeout: Option<Duration>,
    /// The wall-clock budget the whole sitting was given, where one applied.
    #[serde(default)]
    pub scan_timeout: Option<Duration>,
    /// Whether name resolution could generate traffic.
    pub dns_enabled: bool,
    /// Whether identifying detail was masked.
    pub redact: bool,
    /// How far the sitting went to identify operating systems.
    pub os_detection: String,
    /// How far it went to identify services.
    pub service_detection: String,
    /// The intrusiveness ceiling detections ran under, by wire name. Defaults on
    /// an older journal that predates the field.
    #[serde(default)]
    pub detection: String,
    /// Whether it traced the path to each host.
    pub traceroute: bool,
    /// Whether it characterised the filter in front of each host.
    #[serde(default)]
    pub characterise: bool,

    /// Which IP protocols it asked each host about, ascending. Empty for a
    /// phase that ran no such pass, which is a phase written before there was
    /// one too.
    #[serde(default)]
    pub ip_protocols: Vec<u8>,
    /// Whether it established what each TLS port accepts.
    ///
    /// Defaulted on the way in, so a record written before the pass existed
    /// reads back as a sitting that did not enumerate. That is what it was.
    #[serde(default)]
    pub tls_enumeration: bool,
    /// What the sitting changed about the packets it sent, omitted when it
    /// changed nothing.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub evasion: Option<EvasionSettingsRecord>,
    /// The zombie a TCP port scan read its verdicts through, omitted for an
    /// ordinary scan.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub idle_scan: Option<IdleScanRecord>,
    /// Whether the capture kept ICMP errors a technique did not need.
    ///
    /// Defaulted on the way in, which reads a record written before the option
    /// existed as a sitting that did not keep them. That is what it was.
    #[serde(default)]
    pub icmp_evidence: bool,
}

/// The zombie an idle scan ran through, as written to the journal. The
/// serialized form of [`IdleScan`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IdleScanRecord {
    /// The zombie's address.
    pub zombie: String,
    /// The port on the zombie its counter was read from, omitted for the
    /// scanner's default.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub zombie_port: Option<u16>,
}

/// What a sitting changed about the packets it sent, as written to the journal.
/// The serialized form of
/// [`EvasionRecord`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EvasionSettingsRecord {
    /// The source port every probe left from.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source_port: Option<u16>,
    /// The hop limit every ordinary probe carried.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ttl: Option<u8>,
    /// The number of random bytes appended to each probe's payload.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub padding: Option<u16>,
    /// Whether TCP probes carried a deliberately wrong checksum.
    #[serde(default, skip_serializing_if = "is_false")]
    pub bad_tcp_checksum: bool,
    /// The hardware address every frame claimed to come from.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub spoof_mac: Option<String>,
    /// The largest each IP fragment a probe was split into, in bytes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fragment: Option<u16>,
    /// The addresses probes were also sent from as decoys.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub decoys: Vec<String>,
    /// The TCP flags every port probe carried in place of the technique's own,
    /// named (e.g. `fin|psh|urg`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags: Option<String>,
}

/// Omits a `false` boolean, so a recorded technique appears only when it was
/// used. The counterpart of `skip_serializing_if` on the optional fields.
fn is_false(value: &bool) -> bool {
    !*value
}

impl From<&ScanSettings> for SettingsRecord {
    fn from(settings: &ScanSettings) -> Self {
        Self {
            send_mode: settings.send_mode.name().to_owned(),
            tcp_technique: settings.tcp_technique.name().to_owned(),
            sctp_technique: settings.sctp_technique.name().to_owned(),
            retry_effort: settings.retry.effort.name().to_owned(),
            retry_max_attempts: settings.retry.max_attempts.map(NonZeroU8::get),
            retry_timeout_scale: settings.retry.timeout_scale.map(TimeoutScale::get),
            retry_dampen_silent_hosts: settings.retry.dampen_silent_hosts,
            max_probe_rate: settings.max_probe_rate.map(NonZeroU32::get),
            min_probe_rate: settings.min_probe_rate.map(NonZeroU32::get),
            host_probe_interval: settings.host_probe_interval,
            host_timeout: settings.host_timeout,
            scan_timeout: settings.scan_timeout,
            dns_enabled: settings.dns_enabled,
            redact: settings.redact,
            os_detection: settings.os_detection.name().to_owned(),
            service_detection: settings.service_detection.name().to_owned(),
            detection: wire::detection_ceiling_name(settings.detection.ceiling()).to_owned(),
            traceroute: settings.traceroute,
            characterise: settings.characterise,
            ip_protocols: settings.ip_protocols.clone(),
            tls_enumeration: settings.tls_enumeration,
            evasion: settings.evasion.as_ref().map(|e| EvasionSettingsRecord {
                source_port: e.source_port,
                ttl: e.ttl,
                padding: e.padding,
                bad_tcp_checksum: e.bad_tcp_checksum,
                spoof_mac: e.spoof_mac.map(|mac| mac.to_string()),
                fragment: e.fragment,
                decoys: e.decoys.iter().map(|ip| ip.to_string()).collect(),
                flags: e.flags.map(wire::tcp_flags_name),
            }),
            idle_scan: settings.idle_scan.map(|i| IdleScanRecord {
                zombie: i.zombie.to_string(),
                zombie_port: i.zombie_port,
            }),
            icmp_evidence: settings.icmp_evidence,
        }
    }
}

impl From<&SettingsRecord> for ScanSettings {
    fn from(record: &SettingsRecord) -> Self {
        Self {
            send_mode: record.send_mode.parse().unwrap_or_default(),
            tcp_technique: record.tcp_technique.parse().unwrap_or_default(),
            sctp_technique: record.sctp_technique.parse().unwrap_or_default(),
            retry: RetryConfig {
                effort: record.retry_effort.parse().unwrap_or_default(),
                // Read downward, as every other field of this record is: a
                // journal written by a build that accepted a zero budget or a
                // NaN scale carries a value that never applied, and reading it
                // as absent is what it always meant.
                max_attempts: record.retry_max_attempts.and_then(NonZeroU8::new),
                timeout_scale: record.retry_timeout_scale.and_then(TimeoutScale::new),
                dampen_silent_hosts: record.retry_dampen_silent_hosts,
            },
            max_probe_rate: record.max_probe_rate.and_then(NonZeroU32::new),
            min_probe_rate: record.min_probe_rate.and_then(NonZeroU32::new),
            host_probe_interval: record.host_probe_interval,
            host_timeout: record.host_timeout,
            scan_timeout: record.scan_timeout,
            dns_enabled: record.dns_enabled,
            redact: record.redact,
            os_detection: record.os_detection.parse().unwrap_or_default(),
            service_detection: record.service_detection.parse().unwrap_or_default(),
            detection: match record.detection.as_str() {
                "off" => DetectionEnvelope::none(),
                name => wire::detection_class(name)
                    .map(DetectionEnvelope::up_to)
                    .unwrap_or_default(),
            },
            traceroute: record.traceroute,
            characterise: record.characterise,
            ip_protocols: record.ip_protocols.clone(),
            tls_enumeration: record.tls_enumeration,
            evasion: record.evasion.as_ref().map(|e| EvasionRecord {
                source_port: e.source_port,
                ttl: e.ttl,
                padding: e.padding,
                bad_tcp_checksum: e.bad_tcp_checksum,
                spoof_mac: e.spoof_mac.as_ref().and_then(|s| s.parse().ok()),
                fragment: e.fragment,
                decoys: e.decoys.iter().filter_map(|s| s.parse().ok()).collect(),
                flags: e.flags.as_deref().map(wire::tcp_flags),
            }),
            idle_scan: record.idle_scan.as_ref().and_then(|i| {
                i.zombie.parse().ok().map(|zombie| IdleScan {
                    zombie,
                    zombie_port: i.zombie_port,
                })
            }),
            icmp_evidence: record.icmp_evidence,
        }
    }
}

/// What one strategy recorded about its own run.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProbeStatsRecord {
    /// Which strategy, by wire name.
    pub scanner: String,
    /// How many targets it was given.
    pub targets: u128,
    /// Why its receive loop stopped, by wire name.
    pub stop_reason: String,
    /// How long it ran.
    pub elapsed: Duration,
    /// How many sends it attempted.
    pub sends_attempted: u64,
    /// How many the host refused.
    pub sends_failed: u64,
    /// How many were seen leaving on the wire. Defaulted, so an older record
    /// reads as a run that witnessed nothing.
    #[serde(default)]
    pub sends_witnessed: u64,
    /// How many segments its capture handed it.
    pub segments_seen: u64,
    /// Where its congestion window ended up.
    #[serde(default)]
    pub window: Option<WindowRecord>,
    /// How many captured segments belonged to something else.
    pub segments_off_target: u64,
    /// How many replies named no attempt.
    pub replies_without_rtt: u64,
    /// How many hosts it found.
    pub hosts_found: u64,
    /// How many answers arrived on each attempt.
    #[serde(default)]
    pub answered_on: Vec<u64>,
    /// How many answers could not be attributed.
    pub answered_unattributed: u64,
    /// When the first reply arrived.
    #[serde(default)]
    pub first_reply: Option<Duration>,
    /// When the last did.
    #[serde(default)]
    pub last_reply: Option<Duration>,
    /// How many hosts were found in each time bucket.
    #[serde(default)]
    pub found_at: Vec<u64>,
    /// What the capture reported about its own losses.
    #[serde(default)]
    pub capture: Option<CaptureRecord>,
}

impl From<&ProbeStats> for ProbeStatsRecord {
    fn from(stats: &ProbeStats) -> Self {
        Self {
            scanner: wire::scanner_kind_name(stats.scanner()).to_owned(),
            targets: stats.targets(),
            stop_reason: wire::stop_reason_name(stats.stop_reason()).to_owned(),
            elapsed: stats.elapsed(),
            sends_attempted: stats.sends_attempted(),
            sends_failed: stats.sends_failed(),
            sends_witnessed: stats.sends_witnessed(),
            segments_seen: stats.segments_seen(),
            window: stats.window().map(WindowRecord::from),
            segments_off_target: stats.segments_off_target(),
            replies_without_rtt: stats.replies_without_rtt(),
            hosts_found: stats.hosts_found(),
            answered_on: stats.answered_on().to_vec(),
            answered_unattributed: stats.answered_unattributed(),
            first_reply: stats.first_reply(),
            last_reply: stats.last_reply(),
            found_at: stats.found_at().to_vec(),
            capture: stats.capture().map(CaptureRecord::from),
        }
    }
}

impl From<&ProbeStatsRecord> for ProbeStats {
    fn from(record: &ProbeStatsRecord) -> Self {
        /// Copies as much of `from` as `into` has room for. A file written by a
        /// build that counted more attempts is read for the attempts this one
        /// counts, rather than refused.
        fn fill<const N: usize>(from: &[u64]) -> [u64; N] {
            let mut into = [0u64; N];
            for (slot, value) in into.iter_mut().zip(from) {
                *slot = *value;
            }
            into
        }

        ProbeStats::from_parts(ProbeStatsParts {
            scanner: wire::scanner_kind(&record.scanner).unwrap_or(ScannerKind::Composite),
            targets: record.targets,
            // An unreadable stop reason reads as the one that claims least: a
            // run that was cut short rather than one that finished.
            stop_reason: wire::stop_reason(&record.stop_reason)
                .unwrap_or(StopReason::DeadlineExpired),
            elapsed: record.elapsed,
            sends_attempted: record.sends_attempted,
            sends_failed: record.sends_failed,
            sends_witnessed: record.sends_witnessed,
            segments_seen: record.segments_seen,
            window: record.window.as_ref().map(WindowSummary::from),
            segments_off_target: record.segments_off_target,
            replies_without_rtt: record.replies_without_rtt,
            hosts_found: record.hosts_found,
            answered_on: fill(&record.answered_on),
            answered_unattributed: record.answered_unattributed,
            first_reply: record.first_reply,
            last_reply: record.last_reply,
            found_at: fill(&record.found_at),
            capture: record.capture.as_ref().map(CaptureCounts::from),
        })
    }
}

/// Where a strategy's congestion window ended up.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct WindowRecord {
    /// The capacity it finished at.
    pub capacity: usize,
    /// The largest it reached.
    pub peak: usize,
    /// How many times it was cut.
    pub reductions: u32,
    /// Whether it was allowed to adapt.
    pub adaptive: bool,
    /// Whether it ended at its floor.
    pub at_floor: bool,
}

impl From<WindowSummary> for WindowRecord {
    fn from(window: WindowSummary) -> Self {
        Self {
            capacity: window.capacity,
            peak: window.peak,
            reductions: window.reductions,
            adaptive: window.adaptive,
            at_floor: window.at_floor,
        }
    }
}

impl From<&WindowRecord> for WindowSummary {
    fn from(record: &WindowRecord) -> Self {
        Self {
            capacity: record.capacity,
            peak: record.peak,
            reductions: record.reductions,
            adaptive: record.adaptive,
            at_floor: record.at_floor,
        }
    }
}

/// What a capture reported about its own losses.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct CaptureRecord {
    /// How many packets it handed over.
    pub received: u64,
    /// How many it dropped.
    pub dropped: u64,
    /// How many the interface dropped before it.
    pub if_dropped: u64,
    /// How many captures ended before they were told to.
    ///
    /// Defaulted on read, so a journal written before this was recorded opens
    /// and reports none, which is what it knew. A scan whose receive path was
    /// intact writes zero, and the two are indistinguishable in an old journal
    /// for the same reason every other field added to this record is.
    #[serde(default)]
    pub stopped_early: u64,
}

impl From<CaptureCounts> for CaptureRecord {
    fn from(counts: CaptureCounts) -> Self {
        Self {
            received: counts.received,
            dropped: counts.dropped,
            if_dropped: counts.if_dropped,
            stopped_early: counts.stopped_early,
        }
    }
}

impl From<&CaptureRecord> for CaptureCounts {
    fn from(record: &CaptureRecord) -> Self {
        Self {
            received: record.received,
            dropped: record.dropped,
            if_dropped: record.if_dropped,
            stopped_early: record.stopped_early,
        }
    }
}

/// The plan a scan ran against, as a file holds it.
///
/// Ranges and port lists rather than targets, so a `/16` on every port is a few
/// dozen bytes rather than four billion records. This is what lets a scan be
/// continued without being described again: the plan a resume needs is the one
/// that ran, not one reconstructed from what somebody typed.
///
/// A host-discovery plan is a set of addresses and no ports, and is written
/// here as a single unit whose port list is empty. One shape covers both
/// phases, so a reader does not have to know which it is holding to read the
/// addresses out of it.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlanRecord {
    /// The units, in the order they were walked. Order is part of the plan:
    /// positions are counted through it.
    #[serde(default)]
    pub units: Vec<UnitRecord>,
}

impl PlanRecord {
    /// Every address the plan covers, whatever its ports.
    ///
    /// For a discovery plan this is the whole of it. For a port-scan plan it is
    /// the addresses with the port dimension dropped, so two units naming the
    /// same address collapse into one; a set has no room to say a thing twice.
    pub fn addresses(&self) -> IpSet {
        let mut ips = IpSet::new();
        for unit in &self.units {
            for range in unit.ranges.iter().filter_map(RangeRecord::rebuild) {
                ips.insert_range(range);
            }
        }
        ips.canonicalize();
        ips
    }
}

impl From<&IpSet> for PlanRecord {
    /// Records a host-discovery plan: the addresses, and no ports.
    fn from(addresses: &IpSet) -> Self {
        Self {
            units: vec![UnitRecord {
                ranges: ranges_of(addresses),
                spec: String::new(),
                enumerated_ports: Vec::new(),
            }],
        }
    }
}

/// An address set's ranges, v4 before v6, in the order the set enumerates them.
fn ranges_of(ips: &IpSet) -> Vec<RangeRecord> {
    ips.v4()
        .iter()
        .map(|range| RangeRecord::from(&IpRange::V4(*range)))
        .chain(
            ips.v6()
                .iter()
                .map(|range| RangeRecord::from(&IpRange::V6(*range))),
        )
        .collect()
}

/// One set of addresses paired with the ports to try on each.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UnitRecord {
    /// The addresses, as ranges.
    #[serde(default)]
    pub ranges: Vec<RangeRecord>,
    /// The ports, as the specification [`PortSet`] parses.
    ///
    /// Written the way [`PortsRecord`] writes a phase's scope, and for the
    /// reason given there: a full sweep is six bytes rather than a hundred and
    /// thirty thousand entries, and a person reading the manifest can see what
    /// is being scanned. A [`PortSet`] is canonical from construction, so the
    /// specification and the enumeration are the same order and a position keeps
    /// its meaning across the round trip.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub spec: String,
    /// The ports one at a time, as journal format 1 wrote them.
    ///
    /// Read and never written. A journal from that format cannot be continued,
    /// which [`JOURNAL_VERSION`](crate::journal::JOURNAL_VERSION) covers, but it
    /// still reads back as the report its scan produced, and a manifest that
    /// would not deserialize takes that away too.
    #[serde(default, rename = "ports", skip_serializing_if = "Vec::is_empty")]
    pub enumerated_ports: Vec<(u16, String)>,
}

impl UnitRecord {
    /// The ports this unit walks, from whichever form the record carries.
    fn port_set(&self) -> PortSet {
        if !self.spec.is_empty()
            && let Ok(ports) = PortSet::try_from(self.spec.as_str())
        {
            return ports;
        }

        self.enumerated_ports
            .iter()
            .filter_map(|(port, protocol)| wire::protocol(protocol).map(|p| (*port, p)))
            .collect()
    }
}

impl From<&TargetMap> for PlanRecord {
    fn from(plan: &TargetMap) -> Self {
        Self {
            units: plan
                .units
                .iter()
                .map(|unit| UnitRecord {
                    ranges: ranges_of(unit.ips()),
                    spec: unit.ports().to_string(),
                    enumerated_ports: Vec::new(),
                })
                .collect(),
        }
    }
}

impl From<&PlanRecord> for TargetMap {
    fn from(record: &PlanRecord) -> Self {
        let mut plan = TargetMap::new();

        for unit in &record.units {
            let mut ips = IpSet::new();
            for range in unit.ranges.iter().filter_map(RangeRecord::rebuild) {
                ips.insert_range(range);
            }

            let ports = unit.port_set();

            // A unit with nothing left in it would renumber every position after
            // it, so an unreadable one is kept as an empty unit rather than
            // dropped. It contributes no targets and holds its place.
            plan.add_unit(TargetSet::new(ips, ports));
        }

        plan
    }
}

// ╔════════════════════════════════════════════╗
// ║ ████████╗███████╗███████╗████████╗███████╗ ║
// ║ ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝ ║
// ║    ██║   █████╗  ███████╗   ██║   ███████╗ ║
// ║    ██║   ██╔══╝  ╚════██║   ██║   ╚════██║ ║
// ║    ██║   ███████╗███████║   ██║   ███████║ ║
// ║    ╚═╝   ╚══════╝╚══════╝   ╚═╝   ╚══════╝ ║
// ╚════════════════════════════════════════════╝

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::host::{HostStatus, NetworkRole};
    use crate::model::mac::MacAddr;
    use std::net::Ipv4Addr;
    use std::time::UNIX_EPOCH;

    fn at(seconds: u64) -> SystemTime {
        UNIX_EPOCH + Duration::from_secs(seconds)
    }

    /// A host carrying something in every field the model has.
    ///
    /// The oracle below is only as good as this is complete: a field nothing
    /// populates is a field the round trip is not tested on. When the model
    /// gains one, it belongs here.
    fn maximal_host() -> Host {
        let mut host = Host::new(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)));
        host.add_ip(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 2)));
        host.add_ip("2001:db8::1".parse().expect("an address"));
        host.set_hostname(Some("router.example".to_string()));
        host.set_status(HostStatus::Up);

        let mut arp = StatusReason::new(StatusProtocol::Arp, "reply from gateway");
        arp.source = Some(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 254)));
        host.add_reason(arp);
        host.add_reason(StatusReason::new(
            StatusProtocol::Custom("a-strategy".into()),
            "said so",
        ));

        let mut os = OsFingerprint::new("Linux 5.15", 95)
            .with_family("Unix-like")
            .with_generation("5.15.0")
            .with_vendor("Canonical")
            .with_kernel("5.15.0-91-generic")
            .with_detail_accuracy(70)
            .with_evidence("syn-ack hops>=64 opts=M,S,T,N,W");
        os.add_cpe("cpe:/o:linux:linux_kernel:5.15.0");
        os.add_cpe("cpe:/o:canonical:ubuntu_linux:22.04");
        host.set_os(os);

        host.record_os_evidence(OsEvidence {
            source: OsSource::TcpStack,
            family: Some("Linux".to_string()),
            device: None,
            vendor: Some("Canonical".to_string()),
            product: Some("Ubuntu".to_string()),
            version: Some("22.04".to_string()),
            kernel: Some("5.15.0".to_string()),
            arch: None,
            cpe: Some("cpe:/o:canonical:ubuntu_linux:22.04".to_string()),
            confidence: 0.65,
            evidence: "stack reading".to_string(),
        });
        host.record_os_evidence(OsEvidence {
            source: OsSource::ServiceBanner,
            family: Some("Linux".to_string()),
            device: None,
            vendor: None,
            product: None,
            version: None,
            kernel: None,
            arch: None,
            cpe: None,
            confidence: 0.4,
            evidence: "ssh banner".to_string(),
        });

        host.record_mac(MacAddr::new(0x2c, 0xcf, 0x67, 0xf2, 0x51, 0xe3));
        host.record_mac(MacAddr::new(0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e));
        host.set_zone(Zone::new(7, "eth0"));

        host.add_rtts([
            Duration::from_micros(1_200),
            Duration::from_micros(1_800),
            Duration::from_micros(3_000),
        ]);
        host.record_hop_counter(58);

        host.record_hop(Hop::answered(
            3,
            IpAddr::V4(Ipv4Addr::new(198, 51, 100, 1)),
            Some(Duration::from_micros(4_100)),
        ));
        host.record_hop(Hop::silent(2));
        host.record_hop(
            Hop::answered(1, IpAddr::V4(Ipv4Addr::new(192, 0, 2, 254)), None).as_inferred(),
        );

        host.add_network_role(NetworkRole::Tarpit);
        host.add_network_role(NetworkRole::Truncated);

        host.record_ip_protocol(47, IpProtocolState::Open);
        host.record_ip_protocol(89, IpProtocolState::Closed);
        host.record_ip_protocol(103, IpProtocolState::Unasked);

        host.add_port(maximal_port());
        host.add_port(Port::new(53, Protocol::Udp, PortState::OpenFiltered));
        host.add_port(Port::new(80, Protocol::Tcp, PortState::Closed));

        host.add_finding(maximal_finding());

        host.restore_seen(at(1_700_000_000), at(1_700_003_600));
        host
    }

    fn maximal_port() -> Port {
        let service = Service::new("http", 100)
            .with_product("nginx")
            .with_vendor("F5")
            .with_version("1.24.0")
            .with_extrainfo("Ubuntu")
            .with_cpe("cpe:/a:nginx:nginx:1.24.0");

        let certificate = CertificateInfo::new(
            "example.com",
            "R3",
            at(1_690_000_000),
            at(1_720_000_000),
            "ab:cd:ef",
        )
        .with_sans(["example.com".into(), "www.example.com".into()])
        .with_public_key("rsa", 2048);

        let security = Security::new()
            .with_tls_version("TLSv1.3")
            .with_cipher_suite("TLS_AES_256_GCM_SHA384")
            .with_alpn("h2")
            .with_alpn("http/1.1")
            .with_certificate(certificate);

        let discovery = Discovery::new(ScanResponse::TcpSynAck)
            .seen_at(at(1_700_000_500))
            .with_rtt(Duration::from_micros(2_400))
            .with_ttl(58)
            .with_source_ip(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)));

        let mut port = Port::new(443, Protocol::Tcp, PortState::Open)
            .with_service(service)
            .with_security(security)
            .with_discovery(discovery);
        port.add_finding(maximal_finding());
        port
    }

    /// The oracle.
    ///
    /// A host rendered through the export path before and after a round trip has
    /// to render identically. The export DTO is an independent, complete view of
    /// a host, so a field the record forgets to carry shows up here as a
    /// difference. That keeps this honest as the model grows, with no
    /// hand-written comparison to maintain.
    #[test]
    fn a_fully_populated_host_survives_a_round_trip() {
        let original = maximal_host();
        let rebuilt = Host::from(&HostRecord::from(&original));

        let options = crate::export::ExportOptions::default();
        let render = |host: &Host| {
            serde_json::to_value(crate::export::schema::HostDto::new(host, &options))
                .expect("a host renders")
        };

        assert_eq!(
            render(&original),
            render(&rebuilt),
            "a field was lost in the round trip"
        );
    }

    fn maximal_finding() -> Finding {
        Finding::new(
            DetectionId::new("grafana-path-traversal", Version::new(1, 2, 3), "deadbeef").unwrap(),
            "Grafana plugin path traversal",
            Severity::Critical,
            Confidence::Strong,
            DetectionClass::ActiveBenign,
        )
        .unwrap()
        .with_excerpt(Excerpt::new("root:x:0:0:root:/root:/bin/bash"))
        .with_reference(Reference::cve("CVE-2021-43798").unwrap())
        .with_reference(Reference::cwe(22))
        .with_reference(Reference::url("https://grafana.com/security/"))
        .with_remediation("Upgrade to 8.3.1 or later.")
    }

    #[test]
    fn a_fully_populated_finding_survives_a_round_trip() {
        let original = maximal_finding();
        let rebuilt = FindingRecord::from(&original)
            .rebuild()
            .expect("a finding rebuilds");
        assert_eq!(original, rebuilt, "a field was lost in the round trip");
    }

    #[test]
    fn a_finding_record_reads_unknown_names_downward_and_a_nameless_one_away() {
        // A record this build does not fully understand still yields a finding,
        // read to the least it could claim, never guessed upward and never
        // dropped over a soft field.
        let softened = FindingRecord {
            detection: DetectionIdRecord {
                id: "det".into(),
                version: "not-a-version".into(), // -> 0.0.0
                content_hash: "h".into(),
            },
            title: "A finding".into(),
            severity: "catastrophic".into(), // -> Info
            confidence: "absolute".into(),   // -> Heuristic
            class: "nosy".into(),            // -> Passive
            excerpt: None,
            references: vec![ReferenceRecord {
                kind: "mystery".into(),
                value: "x".into(),
            }],
            remediation: None,
        };
        let finding = softened
            .rebuild()
            .expect("an unparseable-but-named finding still rebuilds");
        assert_eq!(finding.severity(), Severity::Info);
        assert_eq!(finding.confidence(), Confidence::Heuristic);
        assert_eq!(finding.class(), DetectionClass::Passive);
        assert_eq!(finding.detection().version(), Version::new(0, 0, 0));
        assert_eq!(
            finding.references().count(),
            0,
            "an unknown reference is dropped, the finding kept"
        );

        // A finding that names no detection, or has no title, is not a finding.
        // Those two are refused rather than softened.
        let nameless = FindingRecord {
            detection: DetectionIdRecord {
                id: "  ".into(),
                ..softened.detection.clone()
            },
            ..softened.clone()
        };
        assert!(nameless.rebuild().is_none());
        let titleless = FindingRecord {
            title: "  ".into(),
            ..softened.clone()
        };
        assert!(titleless.rebuild().is_none());
    }

    #[test]
    fn a_hosts_findings_survive_a_record_round_trip() {
        // The silent failure guarded here: a HostRecord that carries ports but
        // forgets findings would round-trip a host with its findings gone.
        let mut host = Host::new(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 9)));
        host.add_finding(maximal_finding());
        let rebuilt = Host::from(&HostRecord::from(&host));

        let original: Vec<_> = host.findings().cloned().collect();
        let round: Vec<_> = rebuilt.findings().cloned().collect();
        assert_eq!(original, round, "a host's findings were lost in the record");
    }

    #[test]
    fn a_ports_findings_survive_a_record_round_trip() {
        let mut port = Port::new(3000, Protocol::Tcp, PortState::Open);
        port.add_finding(maximal_finding());
        let rebuilt = Port::from(&PortRecord::from(&port));

        let original: Vec<_> = port.findings().cloned().collect();
        let round: Vec<_> = rebuilt.findings().cloned().collect();
        assert_eq!(original, round, "a port's findings were lost in the record");
    }

    /// The record is the same after a round trip through the model, which
    /// catches a rebuild that drops something the record did carry.
    #[test]
    fn the_record_is_stable_across_a_rebuild() {
        let record = HostRecord::from(&maximal_host());
        let rebuilt = HostRecord::from(&Host::from(&record));

        assert_eq!(record, rebuilt);
    }

    /// And through a file, which is what it exists for.
    #[test]
    fn the_record_survives_json() {
        let record = HostRecord::from(&maximal_host());

        let text = serde_json::to_string(&record).expect("serializes");
        let read: HostRecord = serde_json::from_str(&text).expect("deserializes");

        assert_eq!(record, read);
    }

    /// The times a host was first and last seen are evidence, and a scan
    /// resumed the next morning must not report having first seen everything
    /// that morning.
    #[test]
    fn the_times_a_host_was_seen_survive() {
        let rebuilt = Host::from(&HostRecord::from(&maximal_host()));

        assert_eq!(rebuilt.first_seen(), at(1_700_000_000));
        assert_eq!(rebuilt.last_seen(), at(1_700_003_600));
    }

    /// A record naming values this build does not know reads as the least it
    /// could have established, never as more.
    #[test]
    fn unknown_names_read_downwards() {
        let mut record = HostRecord::from(&maximal_host());
        record.status = "ascended".to_string();
        record.ports[0].state = "ajar".to_string();
        record.ports[0].protocol = "dccp".to_string();

        let rebuilt = Host::from(&record);

        assert_eq!(rebuilt.status(), HostStatus::Unknown);
        let port = rebuilt.ports().next().expect("the port is still recorded");
        assert_eq!(
            port.state(),
            PortState::Unasked,
            "a verdict this build cannot read is no verdict, which is what the \
             bottom of the ordering says"
        );
        assert_eq!(port.protocol(), Protocol::Tcp);
    }

    /// A phase survives the round trip, statistics included.
    ///
    /// Rendered through the export path before and after, for the reason the
    /// host oracle gives: it is an independent view, so a field the record
    /// forgets shows up as a difference rather than as silence.
    /// A plan's ports survive as a specification, and a manifest from format 1
    /// still reads.
    ///
    /// The enumeration is what positions are counted through, so the two forms
    /// have to agree about it exactly. They do because a `PortSet` is canonical
    /// from construction: the specification is written from the same order it is
    /// read back into.
    #[test]
    fn a_plans_ports_round_trip_as_a_specification() {
        use crate::model::ip::set::IpSet;
        use crate::model::target::TargetSet;

        let mut plan = TargetMap::new();
        plan.add_unit(TargetSet::new(
            "192.0.2.0/30".parse::<IpSet>().expect("a range"),
            "80,443,1000-1010,u:53".parse::<PortSet>().expect("ports"),
        ));

        let record = PlanRecord::from(&plan);
        assert!(
            record.units[0].enumerated_ports.is_empty(),
            "the enumerated form is read and never written"
        );
        assert_eq!(
            TargetMap::from(&record).iter().collect::<Vec<_>>(),
            plan.iter().collect::<Vec<_>>(),
            "the enumeration a position is counted through has to be identical"
        );

        // Six bytes and change, rather than one entry per port.
        assert!(record.units[0].spec.len() < 40, "{}", record.units[0].spec);

        // And a manifest written before the specification existed still reads.
        let legacy = PlanRecord {
            units: vec![UnitRecord {
                ranges: record.units[0].ranges.clone(),
                spec: String::new(),
                enumerated_ports: plan.units[0]
                    .ports()
                    .iter()
                    .map(|(port, protocol)| (port, wire::protocol_name(protocol).to_owned()))
                    .collect(),
            }],
        };
        assert_eq!(
            TargetMap::from(&legacy).iter().collect::<Vec<_>>(),
            plan.iter().collect::<Vec<_>>()
        );
    }

    #[test]
    fn a_phase_survives_a_round_trip() {
        let report = crate::export::fixture::report();

        for phase in report.phases() {
            let rebuilt = ScanPhase::from(&PhaseRecord::from(phase));

            let options = crate::export::ExportOptions::default();
            let render = |phase: &ScanPhase| {
                serde_json::to_value(crate::export::schema::PhaseDto::new(phase, &options))
                    .expect("a phase renders")
            };
            assert_eq!(render(phase), render(&rebuilt), "a field was lost");
        }
    }

    /// A capture that stopped early survives the journal, and an older journal
    /// that never recorded one still opens.
    ///
    /// The count existed only as a log line until this crossed the wire. A log
    /// line is not the record: a resumed scan, or a report read a week later,
    /// had no way to know part of the receive path was missing for part of the
    /// run, and the counts beside it looked like a complete measurement.
    #[test]
    fn a_capture_that_stopped_early_survives_the_journal() {
        let counts = CaptureCounts {
            received: 271,
            dropped: 4,
            if_dropped: 1,
            stopped_early: 2,
        };

        let record = CaptureRecord::from(counts);
        assert_eq!(CaptureCounts::from(&record), counts, "a field was lost");

        // Through the serialized form the journal actually holds.
        let json = serde_json::to_string(&record).expect("a record serializes");
        assert!(json.contains("stopped_early"), "{json}");
        let read: CaptureRecord = serde_json::from_str(&json).expect("and reads back");
        assert_eq!(CaptureCounts::from(&read), counts);

        // A journal written before this was recorded opens and reports none,
        // which is what it knew.
        let older = r#"{"received":271,"dropped":4,"if_dropped":1}"#;
        let read: CaptureRecord = serde_json::from_str(older).expect("an older journal opens");
        assert_eq!(CaptureCounts::from(&read).stopped_early, 0);
        assert_eq!(CaptureCounts::from(&read).received, 271);
    }

    /// Privilege is a `Privilege` in the model and a boolean on the wire, and
    /// this is where the two meet.
    ///
    /// The journal's format and the published report schema both promised a
    /// boolean before the type existed and go on promising one, so the
    /// conversion has to be exact in both directions and cannot drift with the
    /// enum. A flipped polarity here would report every unprivileged scan as
    /// privileged in an archived report, which is a claim about what a result is
    /// worth rather than a cosmetic error.
    ///
    /// Driven off a real phase with only this field varied, so it stays true as
    /// `PhaseParts` grows.
    #[test]
    fn privilege_crosses_the_record_boundary_as_the_boolean_the_format_promised() {
        let report = crate::export::fixture::report();
        let original = report.phases().first().expect("the fixture has a phase");

        for (privilege, written) in [
            (Some(Privilege::Raw), Some(true)),
            (Some(Privilege::Connect), Some(false)),
            // A third state, not a missing boolean: a phase read out of another
            // scanner's document is not a phase that ran unprivileged.
            (None, None),
        ] {
            let mut record = PhaseRecord::from(original);
            record.privileged = written;

            let phase = ScanPhase::from(&record);
            assert_eq!(phase.privilege(), privilege, "reading {written:?}");
            assert_eq!(
                PhaseRecord::from(&phase).privileged,
                written,
                "writing {privilege:?}"
            );
        }
    }

    /// The detection envelope has to come back as what the scan ran, not as the
    /// default: a report replayed from a journal must gate a re-analysis the same
    /// way the live scan did.
    /// What a silence means depends on this, so a record that lost it would
    /// leave every filtered port's `no reply` unreadable.
    /// A record that lost which probe measured its round trips would read them
    /// back as figures nobody can place.
    #[test]
    fn the_probe_behind_a_round_trip_survives_the_record() {
        use crate::model::host::StatusProtocol;

        let mut host = Host::new(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 9)));
        host.add_rtt_from(Duration::from_micros(90), StatusProtocol::Arp);
        host.add_rtt_from(Duration::from_micros(110), StatusProtocol::Arp);

        let record = HostRecord::from(&host);
        assert_eq!(record.telemetry.rtt_protocol.as_deref(), Some("arp"));

        let mut restored = Host::new(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 9)));
        record.telemetry.restore(&mut restored);

        assert_eq!(restored.rtt_protocol(), Some(StatusProtocol::Arp));
        assert_eq!(restored.min_rtt(), host.min_rtt());
    }

    #[test]
    fn asking_for_icmp_evidence_survives_the_settings_round_trip() {
        use crate::config::ZondConfig;

        let mut settings = ScanSettings::from(&ZondConfig::default());
        assert!(settings.icmp_evidence, "on unless declined");

        // Both ways round: a run that declined it has to say so, or a reader
        // cannot tell a scan that saw no errors from one that never looked.
        settings.icmp_evidence = false;
        assert!(!ScanSettings::from(&SettingsRecord::from(&settings)).icmp_evidence);

        settings.icmp_evidence = true;
        assert!(ScanSettings::from(&SettingsRecord::from(&settings)).icmp_evidence);
    }

    #[test]
    fn the_detection_envelope_survives_the_settings_round_trip() {
        use crate::config::DetectionEnvelope;
        use crate::config::ZondConfig;
        use crate::model::finding::DetectionClass;
        use crate::report::ScanSettings;

        // A raised ceiling, so the round trip must carry the value rather than
        // fall back to the default it happens to start at.
        let mut settings = ScanSettings::from(&ZondConfig::default());
        settings.detection = DetectionEnvelope::up_to(DetectionClass::Exploit);

        let rebuilt = ScanSettings::from(&SettingsRecord::from(&settings));
        assert_eq!(
            rebuilt.detection.ceiling(),
            Some(DetectionClass::Exploit),
            "the envelope ceiling was lost in the round trip"
        );
    }

    /// And through a file.
    #[test]
    fn a_phase_survives_json() {
        let report = crate::export::fixture::report();

        for phase in report.phases() {
            let record = PhaseRecord::from(phase);
            let text = serde_json::to_string(&record).expect("serializes");
            let read: PhaseRecord = serde_json::from_str(&text).expect("deserializes");

            assert_eq!(record, read);
        }
    }

    /// What a scan measured has to come back as what a scan measured.
    ///
    /// A replayed report is rendered by the same code the live run used, so
    /// anything the record drops shows up as a quieter report rather than an
    /// error: fewer protocols behind a host, a round trip with no spread.
    #[test]
    fn a_hosts_measurements_survive_the_round_trip() {
        use crate::model::host::{HostStatus, StatusProtocol, StatusReason};
        use std::time::Duration;

        let mut host = Host::new("192.0.2.1".parse().expect("an address"));
        host.set_status(HostStatus::Up);

        for protocol in [
            StatusProtocol::Arp,
            StatusProtocol::IcmpEcho,
            StatusProtocol::TcpSyn,
        ] {
            host.record_evidence(HostStatus::Up, StatusReason::new(protocol, "answered"));
        }
        host.add_rtts([
            Duration::from_micros(5_100),
            Duration::from_micros(6_060),
            Duration::from_micros(6_540),
        ]);

        let restored = Host::from(&HostRecord::from(&host));

        assert_eq!(
            restored.reasons().len(),
            host.reasons().len(),
            "a protocol that answered was lost"
        );
        assert_eq!(restored.min_rtt(), host.min_rtt(), "the fastest round trip");
        assert_eq!(restored.max_rtt(), host.max_rtt(), "the slowest");
        assert_eq!(restored.average_rtt(), host.average_rtt(), "the mean");
    }
}