mahbot 0.7.3

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
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
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
//! The Excel (`.xlsx`/`.xlsm`) reader: the workbook part, its shared-string
//! table and the worksheet parts, the cell comments and hyperlinks they name and
//! the workbook's defined names, plus the embedded media and charts.
//!
//! [`super`] owns the ZIP plumbing and the other two OOXML families; this
//! module is the whole Excel half, built on the package helpers there.
//! [`crate::document`] routes `.xlsx`/`.xlsm` files here.
//!
//! # Invariants
//!
//! - **A value is shown the way Excel shows it, or not at all.** A cell's number
//!   format decides what is printed ([`numfmt`]), so a date reads as a date and
//!   a percentage as a percentage; a format this reader cannot read, cannot
//!   reproduce, or that has no display for the value prints the stored value under
//!   its mark ([`MARK_STORED`]) — a raw number is never passed off as the display,
//!   and a wrong one is never invented. One exception is stated rather than marked:
//!   a `t="d"` cell holds its date as ISO text, shown as the workbook wrote it
//!   rather than through its number format.
//! - **A part the package left incomplete is never passed off as a whole one.** The
//!   workbook part's own walk still yields the sheets and names it got to, with the
//!   rest said not read ([`WORKBOOK_CUT_NOTE`]); a sheet, styles or comments part cut
//!   short is declined whole rather than delivered by halves; a string table cut short
//!   leaves the strings it did not reach counted as lost; and a workbook whose
//!   relationship list could not be read is read by its parts' conventional names, so
//!   a table named only there is never found and a cell naming a format past the
//!   schema's default entry 0 carries the number that table does not describe
//!   ([`WORKBOOK_RELS_NOTE`]). A part holding no element of its own kind (no
//!   workbook, no sheet, no comments element) is read like one the package does not
//!   carry; an entry the package left empty, for its part, declares nothing.
//! - **Structure is marked, never silently dropped.** Hidden sheets, hidden rows
//!   and columns, merged ranges, cell comments, hyperlinks and defined names all
//!   reach the answer, and the marks are by range rather than by cell, so a
//!   hidden block costs one line rather than one per cell in it. The merge and
//!   link lists are swept down the sheet's rows ([`RowSweep`]), so a cell is only
//!   tested against a range spanning its own row and each range costs one push and
//!   one pop to track; the tests themselves are bounded by [`MAX_RANGE_PROBES`],
//!   past which the rest of the marks are reported rather than worked through — as
//!   is a marker the sheet's own declaration does not let this reader place (a
//!   `<col>` range past the grid, a merged range or link naming no range).
//! - **The notation the model reads is documented in
//!   `src/prompt/tool/read.md`** — a mark this module adds belongs there too.

use super::{
    Part, Relationship, ZipEntry, append_entity, attr, blank, part_dir, read_zip_entry, rel_id,
    relationship_targets, relationships, relationships_and_whole, rels_part_of, resolve_part,
    scan_elements, unopened_parts, unreadable, write_media_parts,
};
use crate::document::{DocOutcome, SkippedImages, ensure_out_dir};
use crate::reader_output::{
    CHART_SHEET, NO_VALUES, Unshown, UnshownKind, column_index, column_letters, sheet_header,
    text_block, text_lines,
};
use crate::util::one_line;
use quick_xml::Reader;
use quick_xml::events::{BytesStart, Event};
use std::cmp::Reverse;
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet};
use std::io::{Cursor, Read, Seek};
use std::path::Path;
use zip::ZipArchive;

mod numfmt;

// ── Part names ──────────────────────────────────────────────────

/// Part holding the workbook's sheet names and their relationship ids.
const WORKBOOK_PART: &str = "xl/workbook.xml";
/// Relationships of the workbook part: sheet relationship id -> sheet part.
const WORKBOOK_RELS_PART: &str = "xl/_rels/workbook.xml.rels";
/// Part holding the strings the `t="s"` cells index into.
const SHARED_STRINGS_PART: &str = "xl/sharedStrings.xml";
/// Part holding the workbook's cell formats.
const STYLES_PART: &str = "xl/styles.xml";
/// Note pushed when the workbook declares cell formats this reader cannot read.
const STYLES_NOTE: &str =
    "the workbook's cell formats could not be read, so values are shown as stored";
/// Note pushed when the workbook's relationships part is there and cannot be read:
/// its parts are then read by their conventional names, and its sheet parts are
/// matched to their entries by position. A part named only there may be missed
/// altogether, which leaves the cells of that part showing the value as stored —
/// the one loss this note stands for, which is why it says so.
const WORKBOOK_RELS_NOTE: &str = "the workbook's relationships could not be read, so its parts are read by their conventional names and one named only there may be missed, leaving its cells showing the value as stored";
/// Note pushed when the workbook part is cut short: the sheets and defined names it
/// declares after that point were not read, and cannot be named from what is left, so
/// the ones read before it are delivered with the loss said rather than passed off as
/// the whole list.
const WORKBOOK_CUT_NOTE: &str = "the workbook part is cut short, so the sheets and names it declares after that point are not read";
/// Note pushed when the workbook part carries a workbook element declaring no sheet:
/// there is nothing in it to read, and the generic "no text" the delivery layer
/// answers with would leave the cause unnamed.
const WORKBOOK_NO_SHEETS_NOTE: &str = "the workbook declares no sheets";
/// Base a worksheet's own relationships resolve against: a comments or
/// threaded-comment target is relative to the sheet part's directory.
const SHEET_BASE: &str = "xl/worksheets/";
/// Prefix of the embedded-media parts in an Excel package.
const XLSX_MEDIA_PREFIX: &str = "xl/media/";
/// Charts in an Excel package: `xl/charts/chart<N>.xml`.
const XLSX_CHARTS_PREFIX: &str = "xl/charts/";
/// The data part of each SmartArt diagram a workbook holds
/// (`xl/diagrams/data<N>.xml`), which no reading reaches.
const XLSX_DIAGRAMS_PREFIX: &str = "xl/diagrams/";
/// The objects a workbook embeds (`xl/embeddings/`) — an object's own bytes,
/// which the reading never opens.
const XLSX_EMBEDDINGS_PREFIX: &str = "xl/embeddings/";
/// The directory a workbook puts chart sheets in: where a sheet's part is found,
/// the sheet is one chart over its whole tab rather than a grid of cells.
const XLSX_CHARTSHEETS_PREFIX: &str = "xl/chartsheets/";
/// The tail of the relationship kind by which a workbook names a chart sheet
/// (`…/relationships/chartsheet`).
const CHART_SHEET_REL: &str = "/chartsheet";

// ── Excel ───────────────────────────────────────────────────────

/// Extract sheet text and embedded images from an `.xlsx`/`.xlsm` package.
///
/// Every sheet the workbook names is walked in workbook order and rendered as
/// `Sheet "<name>":` plus one indented line per valued cell. A cell shows the
/// value its number format displays, each sheet's hidden rows and columns are
/// annotated, merged ranges are marked against the cells they cover, and a
/// hyperlink is marked on the cell it covers. A sheet's cell comments follow its
/// cells, the workbook's defined names follow the last sheet, and a sheet whose
/// part is missing costs only itself and a note.
#[must_use]
pub(crate) fn convert_xlsx(bytes: &[u8], out_dir: &Path) -> DocOutcome {
    let Ok(mut archive) = ZipArchive::new(Cursor::new(bytes)) else {
        return unreadable(".xlsx");
    };
    // The workbook part is read in one walk: its sheets, its date system and its
    // defined names come out of the same pass, which also says whether the part was
    // cut short. A part carrying no workbook element is read like one the package
    // does not carry; one cut short still yields the sheets and names it got to.
    let Some(workbook) = read_zip_entry(&mut archive, WORKBOOK_PART)
        .bytes()
        .and_then(|xml| workbook(&xml))
    else {
        return unreadable(".xlsx");
    };
    // The workbook relationships are read once: the sheet parts resolve through
    // them, the shared-string table, the styles part and the persons part are
    // named by kind, and the sheet parts then resolve their own comments through
    // their own rels. One that is there and cannot be read leaves every part at its
    // conventional name and every sheet matched to its entry by position, which
    // `rels_lost` says once rather than letting each part read as one the workbook
    // never declared.
    let workbook_rels = read_relationships(&mut archive, WORKBOOK_RELS_PART);
    let rels_lost = workbook_rels.is_none();
    let rels = workbook_rels.unwrap_or_default();
    // The shared-string table is named by kind like the styles part, so a
    // producer that lays it out elsewhere still has its strings read; only the
    // conventional name is a fallback.
    let shared_part = related_part(&rels, "/sharedStrings", "xl/")
        .unwrap_or_else(|| SHARED_STRINGS_PART.to_owned());
    let shared = read_zip_entry(&mut archive, &shared_part)
        .bytes()
        .map(|xml| shared_strings(&xml));
    let shared = shared.as_deref().unwrap_or_default();

    ensure_out_dir(out_dir);

    let mut notes = Vec::new();
    if rels_lost {
        notes.push(WORKBOOK_RELS_NOTE.to_owned());
    }
    if workbook.cut {
        notes.push(WORKBOOK_CUT_NOTE.to_owned());
    } else if workbook.sheets.is_empty() {
        notes.push(WORKBOOK_NO_SHEETS_NOTE.to_owned());
    }
    let persons = read_persons(&mut archive, &rels);
    let styles = read_styles(&mut archive, &rels, &mut notes);
    let mut unshown = Unshown::default();
    let mut skipped = SkippedImages::default();
    let images = write_media_parts(
        &mut archive,
        XLSX_MEDIA_PREFIX,
        out_dir,
        &mut skipped,
        &mut unshown,
    );

    let book = Book {
        shared,
        styles: &styles,
        date1904: workbook.date1904,
    };
    let (mut blocks, lost, chart_sheets) = sheet_blocks(
        &mut archive,
        &workbook,
        &rels,
        &book,
        &persons,
        &mut notes,
        &mut unshown,
    );
    // The census of chart parts comes after the sheets are walked: a chart sheet
    // draws one of them, and the sheet is what names it.
    unshown.merge(&unopened_parts(
        &mut archive,
        XLSX_CHARTS_PREFIX,
        Some(XLSX_DIAGRAMS_PREFIX),
        XLSX_EMBEDDINGS_PREFIX,
        Some(&chart_sheets),
    ));
    // Defined names are workbook-level, so they follow every sheet rather than
    // being repeated into the sheet whose cells they point at.
    if !workbook.defined_names.is_empty() {
        blocks.push(text_lines(
            "Defined names:",
            &defined_name_lines(&workbook.defined_names, &workbook.sheets),
        ));
    }
    // A workbook whose string table is absent, truncated or partly unreadable
    // leaves every cell that indexes into it empty — for an Excel/Sheets/
    // LibreOffice-authored file that is all of its text, so it is said out loud
    // rather than read as a workbook with blank cells.
    if lost > 0 {
        notes.push(format!(
            "{lost} cell(s) left out: the shared string table does not provide their text"
        ));
    }
    notes.extend(skipped.notes());
    DocOutcome::Text {
        text: blocks.join("\n\n").trim_end().to_string(),
        images,
        notes,
        unshown,
        all_page_text_lost: false,
    }
}

/// Walk every sheet the workbook declares, in workbook order: their blocks, the
/// shared-string cells they lose between them, and the chart parts the package's
/// chart sheets draw — the caller's census excludes those, so the one chart such a
/// sheet is is not named twice. Each sheet's own losses go into `notes` and each
/// chart sheet into `unshown` as the walk meets them.
fn sheet_blocks<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    workbook: &Workbook,
    rels: &[Relationship],
    book: &Book<'_>,
    persons: &HashMap<String, String>,
    notes: &mut Vec<String>,
    unshown: &mut Unshown,
) -> (Vec<String>, usize, HashSet<String>) {
    let mut blocks = Vec::new();
    let mut lost = 0usize;
    let mut chart_sheets: HashSet<String> = HashSet::new();
    for (index, entry) in workbook.sheets.iter().enumerate() {
        let name = entry.name.as_str();
        let (part, chart_sheet) = resolve_sheet(rels, entry, index);
        let mark = if chart_sheet { CHART_SHEET } else { NO_VALUES };
        if chart_sheet {
            unshown.add(UnshownKind::ChartSheet, 1);
        }
        // The sheet's own relationships name its comments and its hyperlink
        // targets; the list is kept for the parts named by kind, and the link
        // targets resolve through the shared id -> target map.
        let sheet_rels = sheet_relationships(archive, &part, name, notes);
        // The chart the sheet draws, so the package's census of chart parts can be
        // told the sheet is what that chart belongs to.
        if chart_sheet && let Some(chart) = chart_sheet_chart(archive, &part, &sheet_rels) {
            chart_sheets.insert(chart);
        }
        let link_targets = relationship_targets(&sheet_rels);
        // A sheet part the package does not carry is as much of a loss as one
        // that will not parse, so both are said the same way — except for a chart
        // sheet, whose tab is known without its part and had no cells to read.
        let rows = read_zip_entry(archive, &part)
            .bytes()
            .and_then(|xml| sheet_rows(&xml, *book, &link_targets));
        match rows {
            Some(read) => {
                lost += read.lost_strings;
                sheet_losses(name, &read, notes);
                let mut lines = read.lines;
                let comments = cell_comments(archive, &sheet_rels, name, persons, notes);
                lines.extend(comment_lines(comments, name, notes));
                blocks.push(sheet_block(entry, mark, &lines));
            }
            None if chart_sheet => blocks.push(sheet_block(entry, mark, &[])),
            None => notes.push(format!("sheet \"{name}\" could not be read")),
        }
    }
    (blocks, lost, chart_sheets)
}

/// One sheet's block: the header its name and state print, over its lines, or the
/// mark its empty unit carries — a chart sheet says `(chart sheet)` rather than
/// standing empty.
fn sheet_block(entry: &SheetEntry, mark: &str, lines: &[String]) -> String {
    text_block(
        &sheet_header(&entry.name, entry.state.as_deref()),
        mark,
        lines,
    )
}

/// Resolve one `<sheet>` entry: the part it names — what the entry's relationship
/// points at, or the conventional name for the entry's position when the package
/// declares no relationship, so such a sheet still yields its cells instead of
/// nothing — and whether the workbook declares the sheet a chart sheet, one chart
/// over a whole tab rather than a grid of cells.
///
/// The workbook says so two ways: the relationship's own kind (the statement the
/// standard requires) and the `chartsheets/` directory Excel's targets name. A
/// sheet is named a chart sheet rather than one that could not be read, because
/// there were never cells to read.
fn resolve_sheet(rels: &[Relationship], entry: &SheetEntry, index: usize) -> (String, bool) {
    let rel = rels.iter().find(|rel| rel.id == entry.id);
    let part = rel.map_or_else(
        || format!("{SHEET_BASE}sheet{}.xml", index + 1),
        |rel| resolve_part("xl/", &rel.target),
    );
    let chart_sheet = rel.is_some_and(|rel| rel.kind.ends_with(CHART_SHEET_REL))
        || part.starts_with(XLSX_CHARTSHEETS_PREFIX);
    (part, chart_sheet)
}

/// The notes one sheet's reading adds: the hyperlinks whose target it could not
/// resolve, the merged ranges it could not mark, the range marks it could not
/// afford, and the column definitions — the ones it read too late for them to reach
/// a cell, the ones whose column it cannot place, and the ones whose range names an
/// end it cannot read.
fn sheet_losses(name: &str, read: &SheetRead, notes: &mut Vec<String>) {
    if read.lost_links > 0 {
        notes.push(format!(
            "sheet \"{name}\": {} link(s) left out: the target or the cell range it names is not one this reader can resolve",
            read.lost_links
        ));
    }
    if read.lost_merges > 0 {
        notes.push(format!(
            "sheet \"{name}\": {} merged range(s) left out: the cell range they name is not one this reader can mark",
            read.lost_merges
        ));
    }
    if read.late_columns > 0 {
        notes.push(format!(
            "sheet \"{name}\": {} column definition(s) left out: the sheet writes them after \
             its data, where they reach no cell of it",
            read.late_columns
        ));
    }
    if read.lost_columns > 0 {
        notes.push(format!(
            "sheet \"{name}\": {} column definition(s) left out: the columns they hide or \
             style are not ones this reader can place",
            read.lost_columns
        ));
    }
    if read.partial_columns > 0 {
        notes.push(format!(
            "sheet \"{name}\": {} column definition(s) cover a range whose end this \
             reader cannot place, so only the column each one starts at is hidden or \
             styled",
            read.partial_columns
        ));
    }
    if read.dropped_marks {
        notes.push(format!(
            "sheet \"{name}\": later merged ranges and links left out: the sheet declares more \
             of them than this reader marks"
        ));
    }
}

/// The workbook part's own content, read in the part's one walk: its sheets in
/// document order, whether it uses the 1904 date system, and its defined names.
/// The walk that reads them also says whether the part was cut short, so the sheet
/// list and the loss of what followed it are read together rather than by a pass
/// of their own each.
struct Workbook {
    /// The workbook's `<sheet>` entries.
    sheets: Vec<SheetEntry>,
    /// Whether the workbook's `workbookPr` puts it in the 1904 date system.
    date1904: bool,
    /// The workbook's defined names in document order, each with its text.
    defined_names: Vec<DefinedName>,
    /// Whether the part was cut short — at an element boundary or inside a tag — so
    /// the sheets and names it would have listed after that point were not read.
    cut: bool,
}

/// One `<sheet>` entry of the workbook.
struct SheetEntry {
    /// The sheet's name, one line by construction ([`one_line`]): a name reaches
    /// the sheet header, the notes naming the sheet and the defined-names block,
    /// each of which is a line of its own, so a break in it must not become a line
    /// there.
    name: String,
    /// The relationship id naming the sheet's part.
    id: String,
    /// The visibility state the header marks.
    state: Option<String>,
}

/// The workbook part, or `None` when it carries no workbook element — an entry the
/// package left empty, an error page in its place, or one whose bytes are no
/// document are all read like a part the package does not carry, rather than as a
/// workbook declaring no sheet. Every `<sheet>` element is kept, the ones this
/// reader cannot fully place included: a sheet whose relationship the workbook does
/// not name still has a part the caller reads by its position, and a sheet that
/// vanished here would both lose its cells and shift the position every later
/// sheet's scoped defined name is matched by.
fn workbook(xml: &[u8]) -> Option<Workbook> {
    let mut part = Part::new(xml);
    let mut reader = Reader::from_reader(xml);
    let mut buffer = Vec::new();
    let mut read_workbook = false;
    let mut sheets = Vec::new();
    let mut date1904: Option<String> = None;
    let mut names = Vec::new();
    let mut current: Option<DefinedName> = None;
    let mut text = String::new();
    // The walk ends where the part does, so the sheets and names read before a cut are
    // delivered rather than the whole workbook being declined; whether the part counts
    // as cut — and so carries [`WORKBOOK_CUT_NOTE`] — is what its own open elements
    // say ([`Part::cut`]).
    while let Ok(event) = reader.read_event_into(&mut buffer) {
        part.note(&event);
        match event {
            Event::Start(event) => match event.local_name().as_ref() {
                b"workbook" => read_workbook = true,
                b"sheet" => sheets.push(sheet_entry(&event)),
                // The first `workbookPr` that declares a date system is the one
                // the workbook's dates are written in.
                b"workbookPr" if date1904.is_none() => {
                    date1904 = attr(&event, b"date1904");
                }
                b"definedName" => {
                    text.clear();
                    current = defined_name(&event);
                }
                _ => {}
            },
            Event::Empty(event) => match event.local_name().as_ref() {
                b"workbook" => read_workbook = true,
                b"sheet" => sheets.push(sheet_entry(&event)),
                b"workbookPr" if date1904.is_none() => {
                    date1904 = attr(&event, b"date1904");
                }
                b"definedName" => {
                    if let Some(name) = defined_name(&event) {
                        names.push(name);
                    }
                }
                _ => {}
            },
            // A defined name's text is the character data of its own element (a
            // plain string or a CDATA section), entity references resolved.
            Event::Text(event) if current.is_some() => {
                if let Ok(chunk) = event.xml10_content() {
                    text.push_str(&chunk);
                }
            }
            Event::CData(event) if current.is_some() => {
                if let Ok(chunk) = event.xml10_content() {
                    text.push_str(&chunk);
                }
            }
            Event::GeneralRef(event) if current.is_some() => {
                append_entity(&mut text, &event);
            }
            // A name whose element never closes is dropped rather than costing the
            // ones before it; the part's cut is said by the caller.
            Event::End(event) => {
                if event.local_name().as_ref() == b"definedName"
                    && let Some(mut name) = current.take()
                {
                    name.text = one_line(text.trim().to_owned());
                    names.push(name);
                }
            }
            Event::Eof => break,
            _ => {}
        }
        buffer.clear();
    }
    // A part carrying no workbook element — an entry the package left empty, an
    // error page in its place, or one whose bytes are no document — is read like a
    // part the package does not carry rather than as a workbook declaring no sheet.
    // A part cut short still yields the sheets and names it got to, with `cut` saying
    // the list after that point was not read.
    if !read_workbook {
        return None;
    }
    Some(Workbook {
        sheets,
        date1904: date1904.as_deref().is_some_and(is_true),
        defined_names: names,
        cut: part.cut(),
    })
}

/// One `<sheet .../>` entry: the name it carries, the relationship id naming its
/// part, and the state it marks.
fn sheet_entry(event: &BytesStart<'_>) -> SheetEntry {
    SheetEntry {
        name: one_line(attr(event, b"name").unwrap_or_default()),
        id: rel_id(event).unwrap_or_default(),
        state: attr(event, b"state"),
    }
}

/// Whether an XML boolean attribute is true: `1` or `true`, as Excel writes it.
fn is_true(value: &str) -> bool {
    value == "1" || value.eq_ignore_ascii_case("true")
}

// ── Defined names ───────────────────────────────────────────────

/// One workbook `<definedName>`: its name, the sheet its `localSheetId` names
/// (a zero-based index into the workbook's sheets), and its text.
struct DefinedName {
    name: String,
    local_sheet: Option<u32>,
    text: String,
}

/// One `<definedName .../>` element as a name, or `None` when it carries no
/// `name` or is a service name Excel writes for itself (`_xlnm.…`: print areas,
/// print titles, `_FilterDatabase`, …), which the workbook's own UI never shows.
/// The name is one line by construction ([`one_line`]): it is printed as part of
/// the defined-names block, a line of its own.
fn defined_name(event: &BytesStart<'_>) -> Option<DefinedName> {
    let name = attr(event, b"name")?;
    if name.to_ascii_lowercase().starts_with("_xlnm.") {
        return None;
    }
    Some(DefinedName {
        local_sheet: attr(event, b"localSheetId").and_then(|id| id.trim().parse().ok()),
        name: one_line(name),
        text: String::new(),
    })
}

/// One line per defined name: a sheet-local one names the sheet its
/// `localSheetId` indexes, a workbook-level one (or one whose index resolves to
/// no sheet) is bare. A name whose element carries no text names nothing, so it is
/// left out rather than printed as a bare name and a colon.
fn defined_name_lines(names: &[DefinedName], sheets: &[SheetEntry]) -> Vec<String> {
    names
        .iter()
        .filter(|name| !name.text.is_empty())
        .map(|name| {
            match name
                .local_sheet
                .and_then(|index| sheets.get(index as usize))
            {
                Some(sheet) => format!("{} (sheet \"{}\"): {}", name.name, sheet.name, name.text),
                None => format!("{}: {}", name.name, name.text),
            }
        })
        .collect()
}

// ── Shared strings ──────────────────────────────────────────────

/// Read `xl/sharedStrings.xml` into the strings a `t="s"` cell's index refers
/// to: each `<si>` is the concatenation of its `<t>` texts, rich-text runs
/// included.
fn shared_strings(xml: &[u8]) -> Vec<String> {
    let mut reader = Reader::from_reader(xml);
    let mut buffer = Vec::new();
    let mut strings = Vec::new();
    let mut current = String::new();
    let mut in_text = false;
    loop {
        match reader.read_event_into(&mut buffer) {
            Ok(Event::Start(event)) => match event.local_name().as_ref() {
                b"si" => current.clear(),
                b"t" => in_text = true,
                _ => {}
            },
            Ok(Event::Text(event)) if in_text => {
                if let Ok(chunk) = event.xml10_content() {
                    current.push_str(&chunk);
                }
            }
            Ok(Event::GeneralRef(event)) if in_text => append_entity(&mut current, &event),
            Ok(Event::End(event)) => match event.local_name().as_ref() {
                b"t" => in_text = false,
                b"si" => strings.push(std::mem::take(&mut current)),
                _ => {}
            },
            // A self-closed `<si/>` is still a shared string, so it must occupy
            // its index rather than shift every later one.
            Ok(Event::Empty(event)) if event.local_name().as_ref() == b"si" => {
                strings.push(std::mem::take(&mut current));
            }
            Ok(Event::Eof) | Err(_) => break,
            Ok(_) => {}
        }
        buffer.clear();
    }
    strings
}

// ── Formats ─────────────────────────────────────────────────────

/// The workbook's cell formats.
#[derive(Default)]
struct Styles {
    /// Declared `numFmt` code by id.
    num_fmts: HashMap<u32, String>,
    /// The `numFmtId` of each `cellXfs` entry, in order: a cell's `s` indexes
    /// this for the format it shows, and `None` is an entry whose own declaration
    /// this reader could not read — the cell then shows its stored value, marked.
    cell_xfs: Vec<Option<u32>>,
    /// Whether the workbook's formats were there to read and no table came back: the
    /// styles part the workbook declares and the package does not carry, one that is
    /// there and will not parse, or one that declares a format but no cell format to
    /// name any of them — a `numFmt` a malformed entry included, a base-style list,
    /// or a `cellXfs` holding no entry. Every cell then shows its stored text marked
    /// as stored, whatever format it names, rather than being read as one of a
    /// workbook that declares no format at all.
    lost: bool,
}

/// Parse `xl/styles.xml`. `None` when the part cannot be read as XML, is cut short,
/// or carries anything but a stylesheet element — an error page or a text body left
/// in the part's place parses as XML just as happily, and a workbook whose formats
/// were not read is not one to pass its cells off as unformatted. An entry the
/// package left empty declares no format for any cell, which is the value a
/// stylesheet declaring no format also reads as.
fn styles(xml: &[u8]) -> Option<Styles> {
    let mut part = Part::new(xml);
    let mut reader = Reader::from_reader(xml);
    let mut buffer = Vec::new();
    let mut num_fmts = HashMap::new();
    // The base styles a cell format may inherit its number format from, and the
    // cell formats as the three things each declares: its own `numFmtId`, the base
    // style it names and whether it applies its declaration.
    let mut base_xfs: Vec<Option<u32>> = Vec::new();
    let mut cell_xfs: Vec<(Option<u32>, Base, bool)> = Vec::new();
    // Whether the stylesheet declares any format at all — a `numFmt` element (a
    // malformed one included: it is still a format the workbook claims to hold), a
    // cell-format list, or a base-style list, whether or not it holds an entry.
    let mut declares_formats = false;
    let (mut in_cell_xfs, mut in_base_xfs) = (false, false);
    loop {
        let Ok(event) = reader.read_event_into(&mut buffer) else {
            return None;
        };
        part.note(&event);
        match event {
            Event::Start(event) => match event.local_name().as_ref() {
                b"styleSheet" => part.kind_seen(),
                b"cellXfs" => {
                    in_cell_xfs = true;
                    declares_formats = true;
                }
                b"cellStyleXfs" => {
                    in_base_xfs = true;
                    declares_formats = true;
                }
                b"numFmt" => {
                    declares_formats = true;
                    add_num_fmt(&event, &mut num_fmts);
                }
                b"xf" if in_cell_xfs || in_base_xfs => {
                    collect_xf(&event, in_cell_xfs, &mut cell_xfs, &mut base_xfs);
                }
                _ => {}
            },
            Event::Empty(event) => match event.local_name().as_ref() {
                b"styleSheet" => part.kind_seen(),
                b"cellXfs" | b"cellStyleXfs" => declares_formats = true,
                b"numFmt" => {
                    declares_formats = true;
                    add_num_fmt(&event, &mut num_fmts);
                }
                b"xf" if in_cell_xfs || in_base_xfs => {
                    collect_xf(&event, in_cell_xfs, &mut cell_xfs, &mut base_xfs);
                }
                _ => {}
            },
            Event::End(event) => match event.local_name().as_ref() {
                b"cellXfs" => in_cell_xfs = false,
                b"cellStyleXfs" => in_base_xfs = false,
                _ => {}
            },
            Event::Eof => break,
            _ => {}
        }
        buffer.clear();
    }
    // A styles part cut short — or one carrying anything but a stylesheet element —
    // reads as a stylesheet declaring no cell format, which would leave every cell of
    // the workbook at the schema's default with nothing said.
    if part.unreadable() {
        return None;
    }
    // A stylesheet that declares a format but no cell format to name it from is one
    // this reader cannot derive any cell's format from: the formats it names are lost
    // rather than read as a workbook that declares no format for any cell.
    if cell_xfs.is_empty() && declares_formats {
        return None;
    }
    // A cell format that declares no format of its own and does not apply it shows
    // the one the base style it names holds — how a workbook that leaves its number
    // format to a named style writes it. Resolving here keeps every cell lookup a
    // plain index into `cell_xfs`: `None` is an entry whose format cannot be derived,
    // so the cell that shows it carries its stored value under the mark.
    let cell_xfs = cell_xfs
        .into_iter()
        .map(|(own, base, applies)| match (own, base) {
            // The format the entry would inherit is one this reader cannot read (the
            // base style's own declaration is unreadable, or its `xfId` is not one to
            // place), so the entry is read like one whose own declaration is
            // unreadable rather than as a format declaring nothing.
            (Some(0), Base::Unread) if !applies => None,
            (Some(0), Base::Index(base)) if !applies => base_xfs
                .get(base as usize)
                .copied()
                // A base list that does not hold the entry the format names: entry 0
                // stands for the schema's default base style, which is `General`, while
                // a later entry the list does not hold leaves the format underivable.
                .unwrap_or_else(|| (base == 0).then_some(0)),
            // A format that applies its own declaration keeps it, even when that
            // declaration is `General` (`numFmtId` 0): the format claims the format it
            // shows, so it is not handed the base style's.
            (own, _) => own,
        })
        .collect();
    Some(Styles {
        num_fmts,
        cell_xfs,
        lost: false,
    })
}

/// Record an `xf`: a cell format (`numFmtId`, the base style it inherits from and
/// whether it applies the format it declares — only the three together decide what
/// a cell shows) or, before `cellXfs`, a base style's own `numFmtId`.
fn collect_xf(
    event: &BytesStart<'_>,
    in_cell_xfs: bool,
    cell_xfs: &mut Vec<(Option<u32>, Base, bool)>,
    base_xfs: &mut Vec<Option<u32>>,
) {
    if in_cell_xfs {
        cell_xfs.push((
            xf_num_fmt_id(event),
            xf_base_id(event),
            attr(event, b"applyNumberFormat")
                .as_deref()
                .is_some_and(is_true),
        ));
    } else {
        base_xfs.push(xf_num_fmt_id(event));
    }
}

/// Record a `<numFmt numFmtId formatCode/>`; both attributes are required, so a
/// malformed entry is skipped.
fn add_num_fmt(event: &BytesStart<'_>, formats: &mut HashMap<u32, String>) {
    let (Some(id), Some(code)) = (attr(event, b"numFmtId"), attr(event, b"formatCode")) else {
        return;
    };
    if let Ok(id) = id.trim().parse() {
        formats.insert(id, code);
    }
}

/// The `numFmtId` an `xf` declares: `Some(0)`, the default format, when it
/// declares none, and `None` when the attribute is there and does not parse — an
/// entry whose own format this reader cannot read.
fn xf_num_fmt_id(event: &BytesStart<'_>) -> Option<u32> {
    match attr(event, b"numFmtId") {
        None => Some(0),
        Some(id) => id.trim().parse().ok(),
    }
}

/// The base style an `xf` names by its `xfId`, whose number format the entry shows
/// where it declares none of its own and does not apply it.
#[derive(Clone, Copy)]
enum Base {
    /// The entry names no base style.
    None,
    /// The base style the entry names, by the index of its `cellStyleXfs` entry.
    Index(u32),
    /// The entry names a base style this reader cannot read.
    Unread,
}

/// The base style an `xf` names.
fn xf_base_id(event: &BytesStart<'_>) -> Base {
    match attr(event, b"xfId") {
        None => Base::None,
        Some(id) => id.trim().parse().map_or(Base::Unread, Base::Index),
    }
}

/// The workbook's styles part: the first relationship whose kind names a styles
/// part, else the conventional name.
fn styles_part(rels: &[Relationship]) -> String {
    related_part(rels, "/styles", "xl/").unwrap_or_else(|| STYLES_PART.to_owned())
}

/// The workbook's cell formats, read from the part its `/styles` relationship names
/// (the conventional one as a fallback): the schema default when the workbook
/// carries no such part, plus a note when a part that was there to read yielded no
/// table — one the workbook declares through its relationships and the package does
/// not carry, or one that will not parse. A workbook that declares no styles part,
/// and carries none, declares no format for any cell: nothing is lost, nothing is
/// said, and the cells show the schema's default — bar the cell that names a format
/// the missing table would have held, which shows its stored number under the mark.
fn read_styles<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    rels: &[Relationship],
    notes: &mut Vec<String>,
) -> Styles {
    let declared = relationship_target(rels, "/styles").is_some();
    let read = match read_zip_entry(archive, &styles_part(rels)) {
        ZipEntry::Bytes(xml) => styles(&xml),
        ZipEntry::Missing if !declared => return Styles::default(),
        // A part the workbook says it carries and the package does not is as much of
        // a loss as one that will not parse.
        ZipEntry::Missing | ZipEntry::Unreadable => None,
    };
    let Some(styles) = read else {
        notes.push(STYLES_NOTE.to_owned());
        return Styles {
            lost: true,
            ..Styles::default()
        };
    };
    styles
}

// ── Sheet relationships, comments and links ─────────────────────

/// The relationships the part at `name` declares, or `None` when the part is there
/// and cannot be read. A part the package does not carry, and an entry it left empty,
/// both declare no relationship at all — nothing is lost; a part that is no
/// relationship list, or one the package cut short, names its remaining parts
/// nowhere, so its list is not the document's own and is read like one that could
/// not be read at all.
fn read_relationships<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    name: &str,
) -> Option<Vec<Relationship>> {
    match read_zip_entry(archive, name) {
        ZipEntry::Bytes(xml) if blank(&xml) => Some(Vec::new()),
        ZipEntry::Bytes(xml) => match relationships_and_whole(&xml) {
            (Some(rels), true) => Some(rels),
            _ => None,
        },
        ZipEntry::Missing => Some(Vec::new()),
        ZipEntry::Unreadable => None,
    }
}

/// A sheet's own relationships, and the note a part that is there and cannot be
/// read is said in rather than passed off as a sheet with none.
fn sheet_relationships<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    part: &str,
    name: &str,
    notes: &mut Vec<String>,
) -> Vec<Relationship> {
    let Some(rels) = read_relationships(archive, &rels_part_of(part)) else {
        notes.push(format!(
            "sheet \"{name}\": its relationships could not be read"
        ));
        return Vec::new();
    };
    rels
}

/// The first relationship of `rels` whose kind names `suffix`, as the raw
/// `Target` it points at.
fn relationship_target<'a>(rels: &'a [Relationship], suffix: &str) -> Option<&'a str> {
    rels.iter()
        .find(|rel| rel.kind.ends_with(suffix))
        .map(|rel| rel.target.as_str())
}

/// The package part of `base` that the first relationship of `rels` whose kind
/// names `suffix` points at, or `None` when no relationship names one. A
/// relative target is resolved against `base`, an absolute one is already a
/// package path.
fn related_part(rels: &[Relationship], suffix: &str, base: &str) -> Option<String> {
    relationship_target(rels, suffix).map(|target| resolve_part(base, target))
}

/// The chart part a chart sheet draws, or `None` when the package does not say:
/// the sheet's own relationships name it, and where they name a drawing instead —
/// what Excel writes, a chartsheet part referencing a drawing part — the drawing's
/// own relationships name the chart.
fn chart_sheet_chart<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    part: &str,
    sheet_rels: &[Relationship],
) -> Option<String> {
    if let Some(chart) = chart_of(part_dir(part), sheet_rels) {
        return Some(chart);
    }
    let drawing = related_part(sheet_rels, "/drawing", part_dir(part))?;
    let rels = read_zip_entry(archive, &rels_part_of(&drawing))
        .bytes()
        .and_then(|xml| relationships(&xml))
        .unwrap_or_default();
    chart_of(part_dir(&drawing), &rels)
}

/// The chart part `rels` names against `base` — the classic `chart<N>.xml` or the
/// modern `chartEx<N>.xml`, which is a relationship kind of its own.
fn chart_of(base: &str, rels: &[Relationship]) -> Option<String> {
    related_part(rels, "/chart", base).or_else(|| related_part(rels, "/chartEx", base))
}

/// The workbook's person names, read from the part the workbook's `/person`
/// relationship names: a `personId` a thread names resolves here, and one it does
/// not — the part absent, unreadable, holding no `person`, or the workbook's
/// relationships unreadable in the first place — leaves that thread's author
/// unresolved, which its sheet says.
fn read_persons<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    rels: &[Relationship],
) -> HashMap<String, String> {
    let Some(part) = related_part(rels, "/person", "xl/") else {
        return HashMap::new();
    };
    read_zip_entry(archive, &part)
        .bytes()
        .and_then(|xml| scan_elements(&xml, b"person", person_entry))
        .unwrap_or_default()
        .into_iter()
        .collect()
}

/// One `<person id displayName/>` entry, or `None` when either attribute is
/// absent. The display name is one line by construction ([`one_line`]): it stands
/// inside a comment's line, so a break in it must not become a line there.
fn person_entry(event: &BytesStart<'_>) -> Option<(String, String)> {
    let id = attr(event, b"id")?;
    let name = one_line(attr(event, b"displayName")?);
    Some((id, name))
}

/// One cell comment: the cell it is attached to, its author and its text as the
/// part wrote it. The text keeps its lines — a threaded discussion's legacy stub
/// leads each of its entries with a line of its own, which is what tells it from a
/// comment written by a person — and the line the comment is shown on collapses
/// them.
struct Comment {
    reference: String,
    author: Author,
    text: String,
}

/// A comment's author: none named by the workbook, one this reader resolved, or
/// one the workbook names and this reader could not resolve — a `personId` the
/// person names hold no entry for, an `authorId` past its authors list. An author
/// that is in the file but not in this answer is said per sheet rather than passed
/// off as a comment by nobody.
#[derive(PartialEq)]
enum Author {
    None,
    Named(String),
    Lost,
}

impl Comment {
    /// The comment, or `None` when its text is blank once whitespace is collapsed
    /// — a comment that shows nothing is not worth a line of its own.
    fn new(reference: String, author: Author, text: &str) -> Option<Self> {
        if flatten(text).is_empty() {
            return None;
        }
        Some(Self {
            reference,
            author: match author {
                Author::Named(name) if name.is_empty() => Author::None,
                author => author,
            },
            text: text.to_owned(),
        })
    }

    /// The comment's line: its cell, its author when one is known, its text in one
    /// line whatever lines the part wrote it in.
    fn line(&self) -> String {
        match &self.author {
            Author::Named(author) => format!(
                "{} comment ({author}): {}",
                self.reference,
                flatten(&self.text)
            ),
            Author::None | Author::Lost => {
                format!("{} comment: {}", self.reference, flatten(&self.text))
            }
        }
    }
}

/// A text as one line: every run of whitespace becomes one space.
fn flatten(text: &str) -> String {
    text.split_whitespace().collect::<Vec<_>>().join(" ")
}

/// The comments in a legacy `xl/comments<N>.xml` part: its `commentList` entries,
/// each with the name its `authorId` indexes into the part's `authors` list.
/// The text is the nested character data of the entry's `<text>` element, so a
/// plain string, a `<t>` run and a run inside a rich `<r>` all read the same.
/// `None` when the part cannot be read as XML.
fn legacy_comments(xml: &[u8]) -> Option<Vec<Comment>> {
    let mut part = Part::new(xml);
    let mut reader = Reader::from_reader(xml);
    let mut buffer = Vec::new();
    let mut authors: Vec<String> = Vec::new();
    let mut author_text = String::new();
    let mut in_author = false;
    let mut current: Option<(String, Option<String>)> = None;
    let mut text = String::new();
    let mut in_text = false;
    let mut comments = Vec::new();
    loop {
        let Ok(event) = reader.read_event_into(&mut buffer) else {
            return None;
        };
        part.note(&event);
        match event {
            Event::Start(event) => match event.local_name().as_ref() {
                b"comments" => part.kind_seen(),
                b"author" => {
                    in_author = true;
                    author_text.clear();
                }
                b"comment" => {
                    current = Some((
                        attr(&event, b"ref").unwrap_or_default(),
                        attr(&event, b"authorId"),
                    ));
                    text.clear();
                }
                b"text" if current.is_some() => in_text = true,
                _ => {}
            },
            Event::Empty(event) if event.local_name().as_ref() == b"comments" => {
                part.kind_seen();
            }
            Event::Empty(event) if event.local_name().as_ref() == b"author" => {
                // An empty author still occupies its index, so the entries after
                // it keep their `authorId`.
                authors.push(String::new());
            }
            Event::Text(event) => {
                if in_author {
                    if let Ok(chunk) = event.xml10_content() {
                        author_text.push_str(&chunk);
                    }
                } else if in_text && let Ok(chunk) = event.xml10_content() {
                    text.push_str(&chunk);
                }
            }
            Event::GeneralRef(event) => {
                if in_author {
                    append_entity(&mut author_text, &event);
                } else if in_text {
                    append_entity(&mut text, &event);
                }
            }
            Event::End(event) => match event.local_name().as_ref() {
                b"author" => {
                    in_author = false;
                    // The author is one line by construction ([`one_line`]): the name
                    // stands inside the comment's line, so a break in it must not
                    // become a line there.
                    authors.push(one_line(std::mem::take(&mut author_text)));
                }
                b"text" => in_text = false,
                b"comment" => {
                    if let Some((reference, author_id)) = current.take() {
                        // An `authorId` the authors list does not hold names an
                        // author that is in the file but not in this answer.
                        let author = match author_id {
                            Some(id) => id
                                .trim()
                                .parse::<usize>()
                                .ok()
                                .and_then(|index| authors.get(index).cloned())
                                .map_or(Author::Lost, Author::Named),
                            None => Author::None,
                        };
                        if let Some(comment) = Comment::new(reference, author, &text) {
                            comments.push(comment);
                        }
                    }
                }
                _ => {}
            },
            Event::Eof => break,
            _ => {}
        }
        buffer.clear();
    }
    // A comments part read as the comments it got to would pass the rest off as
    // comments the sheet never had, so a cut one is declined whole — as is a part
    // that is no comments document — while an empty entry declares none.
    if part.unreadable() {
        return None;
    }
    Some(comments)
}

/// The comments in a modern `xl/threadedComments/threadedComment<N>.xml` part,
/// each with the name its `personId` maps to — [`Author::Lost`] when it names a
/// person the workbook's person names do not hold. `None` when the part cannot be
/// read as XML.
fn threaded_comments(xml: &[u8], persons: &HashMap<String, String>) -> Option<Vec<Comment>> {
    let mut part = Part::new(xml);
    let mut reader = Reader::from_reader(xml);
    let mut buffer = Vec::new();
    let mut current: Option<(String, Author)> = None;
    let mut text = String::new();
    let mut in_text = false;
    let mut comments = Vec::new();
    loop {
        let Ok(event) = reader.read_event_into(&mut buffer) else {
            return None;
        };
        part.note(&event);
        match event {
            Event::Start(event) => match event.local_name().as_ref() {
                b"ThreadedComments" => part.kind_seen(),
                b"threadedComment" => {
                    let author = match attr(&event, b"personId") {
                        Some(id) => persons
                            .get(&id)
                            .cloned()
                            .map_or(Author::Lost, Author::Named),
                        None => Author::None,
                    };
                    current = Some((attr(&event, b"ref").unwrap_or_default(), author));
                    text.clear();
                }
                b"text" if current.is_some() => in_text = true,
                _ => {}
            },
            Event::Empty(event) if event.local_name().as_ref() == b"ThreadedComments" => {
                part.kind_seen();
            }
            Event::Text(event) if in_text => {
                if let Ok(chunk) = event.xml10_content() {
                    text.push_str(&chunk);
                }
            }
            Event::GeneralRef(event) if in_text => append_entity(&mut text, &event),
            Event::End(event) => match event.local_name().as_ref() {
                b"text" => in_text = false,
                b"threadedComment" => {
                    if let Some((reference, author)) = current.take()
                        && let Some(comment) = Comment::new(reference, author, &text)
                    {
                        comments.push(comment);
                    }
                }
                _ => {}
            },
            Event::Eof => break,
            _ => {}
        }
        buffer.clear();
    }
    // As for the legacy part: a discussion cut short would read as one holding the
    // entries it got to, so it is declined whole rather than delivered by halves.
    if part.unreadable() {
        return None;
    }
    Some(comments)
}

/// The sheet's cell comments: its modern threaded comments and its legacy ones,
/// with a thread replacing the legacy stub of its own discussion. Each part the
/// sheet names that is there but cannot be read is said in a note of its own,
/// since either can be lost without the other.
fn cell_comments<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    rels: &[Relationship],
    name: &str,
    persons: &HashMap<String, String>,
    notes: &mut Vec<String>,
) -> Vec<Comment> {
    // Every comment of a cell is kept, in the order the parts read: a threaded
    // discussion is a thread, so its entries are all shown.
    let mut by_cell: BTreeMap<String, Vec<Comment>> = BTreeMap::new();
    if let Some(target) = relationship_target(rels, "/comments") {
        let part = resolve_part(SHEET_BASE, target);
        match read_zip_entry(archive, &part)
            .bytes()
            .and_then(|xml| legacy_comments(&xml))
        {
            Some(part_comments) => {
                for comment in part_comments {
                    by_cell
                        .entry(comment.reference.clone())
                        .or_default()
                        .push(comment);
                }
            }
            None => notes.push(format!("sheet \"{name}\": its comments could not be read")),
        }
    }
    if let Some(target) = relationship_target(rels, "/threadedComment") {
        let part = resolve_part(SHEET_BASE, target);
        match read_zip_entry(archive, &part)
            .bytes()
            .and_then(|xml| threaded_comments(&xml, persons))
        {
            Some(part_comments) => {
                let mut threaded: BTreeMap<String, Vec<Comment>> = BTreeMap::new();
                for comment in part_comments {
                    threaded
                        .entry(comment.reference.clone())
                        .or_default()
                        .push(comment);
                }
                for (reference, thread) in threaded {
                    let cell = by_cell.entry(reference).or_default();
                    // A real workbook keeps the discussion's own text in the legacy
                    // part as a stub; a legacy comment the thread does not carry is a
                    // comment of its own, so it keeps its line.
                    cell.retain(|comment| !is_the_stub_of(comment, &thread));
                    cell.extend(thread);
                }
            }
            None => notes.push(format!(
                "sheet \"{name}\": its threaded comments could not be read"
            )),
        }
    }
    by_cell.into_values().flatten().collect()
}

/// Whether a legacy comment on a cell is the stub of the threaded discussion on
/// it — the same discussion written twice, and so shown once.
///
/// Excel keeps a copy of a discussion in the legacy part for readers without
/// threaded comments: a `[Threaded comment]` preamble, a note to the reader, and
/// then each entry's text behind a `Comment:` (or `Reply:`) lead of its own, in
/// thread order. That copy is what this reader recognizes — the texts the leads
/// carry, in their own places, are the thread's own texts. A legacy comment
/// carrying no lead at all, or carrying texts that are not the thread's, is a
/// comment of its own and keeps its line, since dropping it would lose a person's
/// words on a guess.
fn is_the_stub_of(comment: &Comment, thread: &[Comment]) -> bool {
    let entries = stub_entries(&comment.text);
    !entries.is_empty()
        && entries.len() <= thread.len()
        && entries
            .iter()
            .zip(thread)
            .all(|(entry, comment)| flatten(entry) == flatten(&comment.text))
}

/// The entries a legacy comment's text carries as a threaded stub: the text behind
/// each `Comment:`/`Reply:` lead, up to the next lead, with everything before the
/// first lead — the `[Threaded comment]` preamble and the note to the reader Excel
/// writes ahead of them — dropped. Empty when the text carries no lead at all,
/// which is a comment of its own rather than a copy of a discussion.
fn stub_entries(text: &str) -> Vec<String> {
    let mut entries: Vec<String> = Vec::new();
    let mut current: Option<String> = None;
    for line in text.lines().map(str::trim).filter(|line| !line.is_empty()) {
        match stub_lead(line) {
            Some(body) => {
                entries.extend(current.take());
                current = Some(body.to_owned());
            }
            None => {
                if let Some(entry) = current.as_mut() {
                    if !entry.is_empty() {
                        entry.push(' ');
                    }
                    entry.push_str(line);
                }
            }
        }
    }
    entries.extend(current);
    entries
}

/// The text behind a `Comment:` or `Reply:` lead at the start of `line`, or `None`
/// when the line carries no lead.
fn stub_lead(line: &str) -> Option<&str> {
    let (lead, rest) = line.split_once(':')?;
    let lead = lead.trim();
    (lead.eq_ignore_ascii_case("comment") || lead.eq_ignore_ascii_case("reply"))
        .then(|| rest.trim())
}

/// The sheet's comment lines, one per comment, sorted by the (row, column) of the
/// cell they are attached to. A comment whose cell reference this reader cannot
/// parse names no cell a line could be attached to, so it is left out and counted
/// rather than printed under an address that is not one.
fn comment_lines(comments: Vec<Comment>, name: &str, notes: &mut Vec<String>) -> Vec<String> {
    let total = comments.len();
    let mut placed: Vec<((u32, u32), Comment)> = comments
        .into_iter()
        .filter_map(|comment| {
            cell_position(&comment.reference).map(|(column, row)| ((row, column), comment))
        })
        .collect();
    if placed.len() < total {
        notes.push(format!(
            "sheet \"{name}\": {} comment(s) left out: the cell they name is not one this reader can resolve",
            total - placed.len()
        ));
    }
    let lost_authors = placed
        .iter()
        .filter(|(_, comment)| comment.author == Author::Lost)
        .count();
    if lost_authors > 0 {
        notes.push(format!(
            "sheet \"{name}\": {lost_authors} comment author(s) could not be resolved"
        ));
    }
    placed.sort_by_key(|(position, _)| *position);
    placed
        .into_iter()
        .map(|(_, comment)| comment.line())
        .collect()
}

// ── Worksheet pass ──────────────────────────────────────────────

/// What a cell needs to render: the workbook's shared strings, its formats and
/// its date system.
#[derive(Clone, Copy)]
struct Book<'a> {
    shared: &'a [String],
    styles: &'a Styles,
    date1904: bool,
}

/// What one worksheet pass yields: its rendered lines, how many cells held a
/// `t="s"` string the table does not provide, how many hyperlinks were lost — a
/// link is lost when its `r:id` is not declared, its `ref` is missing, or its
/// `ref` is not a range this reader can parse — and how many merges were lost —
/// a merge is lost when its `ref` is missing or is not a range this reader can
/// mark; all are reported by the caller as notes.
struct SheetRead {
    lines: Vec<String>,
    lost_strings: usize,
    lost_links: usize,
    lost_merges: usize,
    /// The `<col>` definitions the sheet wrote after its data, where they reach no
    /// cell of it — reported by the caller.
    late_columns: usize,
    /// The `<col>` definitions whose column this reader cannot place, so nothing of
    /// them is applied — reported by the caller.
    lost_columns: usize,
    /// The `<col>` definitions whose declared end this reader cannot read, so only
    /// the column each one starts at is hidden or styled — reported by the caller.
    partial_columns: usize,
    /// The sheet declared more merged ranges and links than this reader could
    /// mark, so its later marks are missing — reported by the caller.
    dropped_marks: bool,
}

/// The lines of one worksheet: the hidden-geometry annotations first, then one
/// line per valued cell, then a line per hyperlink its cells do not show.
///
/// `None` on any XML error — a sheet part that cannot be read as XML is
/// reported like a missing one.
#[expect(clippy::too_many_lines)] // one arm per worksheet element, in a single streaming pass
fn sheet_rows(
    xml: &[u8],
    book: Book<'_>,
    link_targets: &HashMap<String, String>,
) -> Option<SheetRead> {
    let mut part = Part::new(xml);
    let mut reader = Reader::from_reader(xml);
    let mut buffer = Vec::new();
    let mut cells: Vec<CellLine> = Vec::new();
    let mut lost = 0usize;
    let mut row_number = 0u32;
    // Column of the next cell that carries no `r`: real files omit `r` on
    // cells that follow one another, and the addresses still count distance.
    let mut next_column = 0u32;
    let mut cell: Option<Cell> = None;
    let mut shared_formulas: HashMap<String, String> = HashMap::new();
    let (mut in_value, mut in_text, mut in_formula) = (false, false, false);
    let (mut in_cols, mut in_merges, mut in_hyperlinks) = (false, false, false);
    let mut hidden_rows = BTreeSet::new();
    let mut column_ranges: Vec<ColumnRange> = Vec::new();
    let mut columns: Option<Columns> = None;
    // How many `<col>` definitions the sheet had written when its first cell
    // settled them; the ones written after that reach no cell of it.
    let mut settled_columns: Option<usize> = None;
    let mut row_style: Option<Style> = None;
    let mut merges = Vec::new();
    let mut links = Vec::new();
    let (mut lost_links, mut lost_merges, mut lost_columns, mut partial_columns) =
        (0usize, 0usize, 0usize, 0usize);
    loop {
        let Ok(event) = reader.read_event_into(&mut buffer) else {
            return None;
        };
        part.note(&event);
        match event {
            Event::Start(event) => match event.local_name().as_ref() {
                b"worksheet" | b"chartsheet" | b"dialogsheet" | b"macrosheet" => part.kind_seen(),
                b"row" => {
                    row_number = next_row_number(&event, row_number);
                    row_style = row_default_style(&event);
                    next_column = 0;
                    if attr(&event, b"hidden").as_deref().is_some_and(is_true) {
                        hidden_rows.insert(row_number);
                    }
                }
                b"cols" => in_cols = true,
                b"col" if in_cols => {
                    collect_columns(
                        &event,
                        &mut column_ranges,
                        &mut lost_columns,
                        &mut partial_columns,
                    );
                }
                b"mergeCells" => in_merges = true,
                b"mergeCell" if in_merges => {
                    collect_merge(&event, &mut merges, &mut lost_merges);
                }
                // `<hyperlinks>` follows `<sheetData>`, so it is collected in
                // this same pass and applied once the cells are rendered.
                b"hyperlinks" => in_hyperlinks = true,
                b"hyperlink" if in_hyperlinks => {
                    collect_link(&event, link_targets, &mut links, &mut lost_links);
                }
                // `<cols>` precedes `<sheetData>` in the schema, so the ranges
                // read so far are settled the first time a cell asks for them.
                b"c" => {
                    let columns = columns.get_or_insert_with(|| {
                        settled_columns = Some(column_ranges.len());
                        resolve_columns(&column_ranges)
                    });
                    cell = Some(Cell::start(
                        &event,
                        row_number,
                        row_style,
                        columns,
                        &mut next_column,
                    ));
                }
                b"v" => in_value = true,
                b"t" => in_text = true,
                b"f" => {
                    in_formula = true;
                    if let Some(cell) = &mut cell {
                        cell.start_formula(&event);
                    }
                }
                _ => {}
            },
            Event::Empty(event) => match event.local_name().as_ref() {
                b"worksheet" | b"chartsheet" | b"dialogsheet" | b"macrosheet" => part.kind_seen(),
                // A row with no cells still hides, so it counts like any other.
                b"row" => {
                    row_number = next_row_number(&event, row_number);
                    row_style = row_default_style(&event);
                    if attr(&event, b"hidden").as_deref().is_some_and(is_true) {
                        hidden_rows.insert(row_number);
                    }
                }
                b"col" if in_cols => {
                    collect_columns(
                        &event,
                        &mut column_ranges,
                        &mut lost_columns,
                        &mut partial_columns,
                    );
                }
                b"mergeCell" if in_merges => {
                    collect_merge(&event, &mut merges, &mut lost_merges);
                }
                b"hyperlink" if in_hyperlinks => {
                    collect_link(&event, link_targets, &mut links, &mut lost_links);
                }
                // A valueless cell still occupies its column, so the counter
                // advances even though there is nothing to emit for it.
                b"c" => {
                    let _ = cell_reference(&event, row_number, &mut next_column);
                }
                b"f" => {
                    if let Some(cell) = &mut cell {
                        cell.resolve_shared_formula(&event, &shared_formulas);
                    }
                }
                _ => {}
            },
            Event::Text(event) => {
                if let Some(cell) = &mut cell
                    && let Ok(chunk) = event.xml10_content()
                {
                    if in_formula {
                        cell.formula.push_str(&chunk);
                    } else if in_value || in_text {
                        cell.value.push_str(&chunk);
                    }
                }
            }
            Event::GeneralRef(event) => {
                if let Some(cell) = &mut cell {
                    if in_formula {
                        append_entity(&mut cell.formula, &event);
                    } else if in_value || in_text {
                        append_entity(&mut cell.value, &event);
                    }
                }
            }
            Event::End(event) => match event.local_name().as_ref() {
                b"cols" => in_cols = false,
                b"mergeCells" => in_merges = false,
                b"hyperlinks" => in_hyperlinks = false,
                b"v" => in_value = false,
                b"t" => in_text = false,
                b"f" => {
                    in_formula = false;
                    if let Some(cell) = &mut cell {
                        cell.end_formula(&mut shared_formulas);
                    }
                }
                b"c" => {
                    if let Some(mut cell) = cell.take() {
                        let line = cell.line(book);
                        if cell.shared_lost {
                            lost += 1;
                        }
                        if let Some(line) = line {
                            cells.push(line);
                        }
                    }
                }
                _ => {}
            },
            Event::Eof => break,
            _ => {}
        }
        buffer.clear();
    }
    // A sheet part the package left half written reads as one whose data simply
    // ends, so its later rows would be passed off as rows the sheet never had: it is
    // declined whole, as is a part that is no sheet document, while an entry the
    // package left empty ([`blank`]) declares a sheet with no values.
    if part.unreadable() {
        return None;
    }
    let columns = columns.unwrap_or_else(|| resolve_columns(&column_ranges));
    // A `<col>` the sheet writes after its data reaches no cell of it: the columns
    // a cell shows are settled the first time a cell asks for them. Those
    // definitions are counted here and reported rather than dropped in silence.
    let late_columns = settled_columns.map_or(0, |at| column_ranges.len() - at);
    let (rendered, dropped_marks) =
        render_lines(&cells, &hidden_rows, &columns.hidden, &merges, &links);
    Some(SheetRead {
        lines: rendered,
        lost_strings: lost,
        lost_links,
        lost_merges,
        late_columns,
        lost_columns,
        partial_columns,
        dropped_marks,
    })
}

/// The number of a `<row>`: its `r`, or one past `previous` when it has none.
fn next_row_number(event: &BytesStart<'_>, previous: u32) -> u32 {
    attr(event, b"r")
        .and_then(|row| row.trim().parse().ok())
        .unwrap_or(previous.saturating_add(1))
}

/// The style a row gives its own cells: `<row s customFormat="1"/>` styles every
/// cell of the row that declares no style of its own, and it wins over a covering
/// `<col>` — a row that marks its format custom but names no `s` takes the
/// schema's default entry 0 rather than the column's style, and a row naming an `s`
/// this reader cannot read gives its cells a format it cannot show. A row that does
/// not mark the style custom gives its cells none.
fn row_default_style(event: &BytesStart<'_>) -> Option<Style> {
    if !attr(event, b"customFormat").as_deref().is_some_and(is_true) {
        return None;
    }
    Some(match attr(event, b"s") {
        None => Style::Index(0),
        Some(style) => style.trim().parse().map_or(Style::Unread, Style::Index),
    })
}

/// The last column Excel can hold: XFD is the 16384th column. A `<col>`
/// element's declared range is document-controlled, so it is clamped to the
/// grid Excel itself has.
const MAX_COLUMNS: u32 = 16_384;

/// Where a cell's number format comes from.
#[derive(Clone, Copy, Default)]
enum Style {
    /// The cell format a cell shows: the index its own `s` names, else the one its
    /// row or its column gives it.
    Index(usize),
    /// The cell, its row or its column names a style this reader cannot read: no
    /// table holds an entry at it, not even one declaring nothing at all, so the
    /// cell's stored text is shown marked as stored rather than read as one showing
    /// the schema's default.
    Unread,
    /// The cell names no style of its own and neither its row nor its column gives
    /// it one.
    #[default]
    None,
}

/// One `<col>` element as the sheet writes it: the columns it covers, whether it
/// hides them, and the cell format a cell of those columns shows when it declares
/// no style of its own.
struct ColumnRange {
    min: u32,
    max: u32,
    hidden: bool,
    style: Option<Style>,
}

/// A sheet's `<col>` elements once resolved: the columns they hide, and every
/// column's default cell format by its zero-based index — `None` where the sheet
/// gives that column none.
#[derive(Default)]
struct Columns {
    hidden: BTreeSet<u32>,
    styles: Vec<Option<Style>>,
}

/// Record a `<col min max hidden="1" style="2"/>`: the 1-based columns it covers and
/// the style (if any) it gives them; `min` alone means a single column. An element
/// that neither hides its columns nor styles them says nothing, so nothing of it can
/// be lost; one that does and whose range this reader cannot place — a `min` missing,
/// unparsable or outside the grid, or a `max` that does not read — leaves the columns
/// it hides or styles unmarked, so it is counted as a loss. The two ways of losing
/// are counted apart: an element whose `min` it cannot place is applied to no column
/// at all, while one whose `max` does not read still hides or styles the column it
/// starts at. A `style` it names that this reader cannot read is a format the
/// column's cells show and it cannot name, so those cells are marked as stored rather
/// than read as unformatted.
fn collect_columns(
    event: &BytesStart<'_>,
    ranges: &mut Vec<ColumnRange>,
    lost: &mut usize,
    partial: &mut usize,
) {
    let hidden = attr(event, b"hidden").as_deref().is_some_and(is_true);
    let style =
        attr(event, b"style").map(|value| value.trim().parse().map_or(Style::Unread, Style::Index));
    if !hidden && style.is_none() {
        return;
    }
    let Some(min) = attr(event, b"min")
        .and_then(|value| value.trim().parse::<u32>().ok())
        .filter(|min| (1..=MAX_COLUMNS).contains(min))
    else {
        *lost += 1;
        return;
    };
    let max = match attr(event, b"max") {
        None => min,
        Some(value) => {
            if let Ok(max) = value.trim().parse::<u32>() {
                max.clamp(min, MAX_COLUMNS)
            } else {
                // The element's own `min` is one this reader can place, so that
                // column is hidden or styled as the element asks; only its range's
                // end — and the columns it would have covered beyond `min` — is
                // left out.
                *partial += 1;
                min
            }
        }
    };
    ranges.push(ColumnRange {
        min,
        max,
        hidden,
        style,
    });
}

/// Resolve a sheet's `<col>` elements into the columns they hide and every
/// column's default style. Every element may name the grid's whole width, so a
/// column is settled once and never looked at again: a hidden element hides the
/// columns it names, and a column's style is the one of the last element naming
/// it, so the elements are walked backwards for that.
fn resolve_columns(ranges: &[ColumnRange]) -> Columns {
    // A sheet with no `<col>` element hides nothing and styles nothing, which is
    // the shape most sheets have.
    if ranges.is_empty() {
        return Columns::default();
    }
    let mut hidden = BTreeSet::new();
    let mut styles = vec![None; MAX_COLUMNS as usize];
    let mut unhidden: BTreeSet<u32> = (1..=MAX_COLUMNS).collect();
    let mut unstyled: BTreeSet<u32> = (1..=MAX_COLUMNS).collect();
    // The columns of `range` not settled yet, taken out of `open`: a column is
    // looked at once however many elements name it.
    let settle = |open: &mut BTreeSet<u32>, range: &ColumnRange| {
        let columns: Vec<u32> = open.range(range.min..=range.max).copied().collect();
        for column in &columns {
            open.remove(column);
        }
        columns
    };
    for range in ranges.iter().filter(|range| range.hidden) {
        for column in settle(&mut unhidden, range) {
            hidden.insert(column);
        }
    }
    for range in ranges.iter().rev().filter(|range| range.style.is_some()) {
        for column in settle(&mut unstyled, range) {
            styles[column as usize - 1] = range.style;
        }
    }
    Columns { hidden, styles }
}

/// The default style of the column a cell address sits in, or `None` when the
/// sheet gives that column none.
fn column_style(columns: &Columns, reference: &str) -> Option<Style> {
    let column = column_index(reference)?;
    *columns.styles.get(column as usize)?
}

/// Record a `<mergeCell ref="A1:C1"/>`. A merge is sheet structure, so one whose
/// ref is missing or is an address this reader cannot parse (an inverted `C3:A1`
/// included) is counted as lost rather than dropped in silence — while a ref
/// naming one cell covers nothing to mark and is no loss at all.
fn collect_merge(event: &BytesStart<'_>, merges: &mut Vec<Merge>, lost: &mut usize) {
    let Some(reference) = attr(event, b"ref") else {
        *lost += 1;
        return;
    };
    match Rect::parse(&reference) {
        Some(rect) if rect.min_col < rect.max_col || rect.min_row < rect.max_row => {
            merges.push(Merge { rect, reference });
        }
        Some(_) => {}
        None => *lost += 1,
    }
}

/// Record a `<hyperlink ref="A1" r:id="rId1" location="…" display="…"/>`: a link
/// whose `r:id` the sheet's relationships do not declare, whose `ref` is missing
/// or is not a range this reader can parse, is counted as lost rather than
/// rendered pointing nowhere.
fn collect_link(
    event: &BytesStart<'_>,
    targets: &HashMap<String, String>,
    links: &mut Vec<Link>,
    lost: &mut usize,
) {
    match Link::new(event, targets) {
        Some(link) => links.push(link),
        None => *lost += 1,
    }
}

/// A cell's address: its own `r` attribute, or one synthesised from `row` and
/// the running column counter. Either way the counter is left on the column
/// after this cell — a cell with no address still occupies its column. An `r`
/// this reader cannot parse is kept as the workbook wrote it: a value is never
/// dropped for its address (a comment, which is only ever an annotation, is left
/// out and counted instead), it just carries no merge or link mark. It is one line
/// by construction ([`one_line`]): it is printed at the head of the cell's own
/// line, so a break in it must not become a line there.
fn cell_reference(event: &BytesStart<'_>, row: u32, next_column: &mut u32) -> String {
    let Some(reference) = attr(event, b"r") else {
        let reference = format!("{}{}", column_letters(*next_column), row);
        *next_column = next_column.saturating_add(1);
        return reference;
    };
    let reference = one_line(reference);
    if let Some(index) = column_index(&reference) {
        *next_column = index.saturating_add(1);
    }
    reference
}

/// The rectangle a `ref` covers. Both a merge and a hyperlink are one, so the
/// containment test and the parse live here once. A column is zero-based, a row
/// one-based.
#[derive(Clone, Copy)]
struct Rect {
    /// The rectangle's leftmost column, zero-based.
    min_col: u32,
    /// The rectangle's top row, one-based.
    min_row: u32,
    /// The rectangle's rightmost column, zero-based.
    max_col: u32,
    /// The rectangle's bottom row, one-based.
    max_row: u32,
}

impl Rect {
    /// The rectangle `reference` covers — `A1:C1` or a single `A1` — or `None`
    /// when it is not an address this reader can parse, or its minimum is past
    /// its maximum (an inverted `C3:A1` names no cell at all).
    fn parse(reference: &str) -> Option<Self> {
        if let Some((start, end)) = reference.split_once(':') {
            let (min_col, min_row) = cell_position(start)?;
            let (max_col, max_row) = cell_position(end)?;
            if min_col > max_col || min_row > max_row {
                return None;
            }
            return Some(Self {
                min_col,
                min_row,
                max_col,
                max_row,
            });
        }
        let (column, row) = cell_position(reference)?;
        Some(Self {
            min_col: column,
            min_row: row,
            max_col: column,
            max_row: row,
        })
    }

    /// Whether `(column, row)` falls inside the rectangle.
    fn holds(&self, column: u32, row: u32) -> bool {
        column >= self.min_col
            && column <= self.max_col
            && row >= self.min_row
            && row <= self.max_row
    }

    /// Whether `(column, row)` is the rectangle's top-left corner, the cell a
    /// merge's value belongs to.
    fn is_corner(&self, column: u32, row: u32) -> bool {
        column == self.min_col && row == self.min_row
    }
}

/// One `<mergeCell ref="A1:C1"/>`: the rectangle it covers and the ref text as
/// the workbook wrote it.
struct Merge {
    rect: Rect,
    reference: String,
}

/// One `<hyperlink>`: the rectangle it covers, the ref and display text as the
/// workbook wrote them, and the raw target it points at — a URL or a file path,
/// never a package part, so it is kept exactly as the workbook wrote it. Display
/// and target are one line by construction ([`one_line`]): they stand inside the
/// cell's line, so a break in either must not become a line there.
struct Link {
    rect: Rect,
    reference: String,
    display: Option<String>,
    target: String,
}

impl Link {
    /// The link, or `None` when the workbook declares no target for it or its
    /// `ref` is not a range this reader can parse — either way the link is lost
    /// rather than rendered pointing nowhere.
    fn new(event: &BytesStart<'_>, targets: &HashMap<String, String>) -> Option<Self> {
        let reference = attr(event, b"ref")?;
        let rect = Rect::parse(&reference)?;
        let target = match rel_id(event) {
            Some(id) => one_line(targets.get(&id)?.clone()),
            None => one_line(attr(event, b"location")?),
        };
        Some(Self {
            rect,
            reference,
            display: attr(event, b"display").map(one_line),
            target,
        })
    }
}

/// The mark a cell's line carries when its value is the number the cell stores
/// rather than a display of it: the cell's number format was not read, is not one
/// this reader reproduces for this value, or shows nothing at all for it.
const MARK_STORED: &str = " (stored number, format not shown)";

/// A cell's value as this reader shows it.
struct Shown {
    /// The text the cell's line carries: always one line ([`one_line`]).
    text: String,
    /// Whether `text` is the value as the cell stores it rather than a display of
    /// it, which is what makes the line carry [`MARK_STORED`].
    stored: bool,
}

impl Shown {
    /// The value as the cell's own format displays it: a format code the workbook
    /// wrote may carry a break of its own, so the display is made one line here
    /// rather than promised at the call site.
    fn displayed(text: String) -> Self {
        Self {
            text: one_line(text),
            stored: false,
        }
    }

    /// The value as the cell stores it, under [`MARK_STORED`].
    fn stored(text: String) -> Self {
        Self {
            text: one_line(text),
            stored: true,
        }
    }

    /// The mark the line carries after this value, empty when it is a display.
    fn mark(&self) -> &'static str {
        if self.stored { MARK_STORED } else { "" }
    }
}

/// What one cell holds, once its value is rendered.
enum Held {
    /// A value the cell holds, as this reader shows it.
    Value(Shown),
    /// The value and the formula that produces it.
    ValueAndFormula(Shown, String),
    /// A formula whose value is not stored, shown as its own text.
    Formula(String),
}

/// One cell of a sheet, before the marks of the ranges holding it are known: the
/// address as the workbook wrote it, what it holds, and that address as a position.
/// The line the answer carries is built from these in [`render_lines`], where those
/// marks — and the link a caption has to be weighed against — are known too.
struct CellLine {
    /// The cell address, from `r` or synthesised from its row and column.
    reference: String,
    held: Held,
    /// The address's `(column, row)`, or `None` when it is not one.
    position: Option<(u32, u32)>,
}

impl CellLine {
    /// The line before its range and link marks: the address, the value as shown
    /// when the cell holds one — with the mark a value shown as stored carries — and
    /// the formula when it holds one.
    fn text(&self) -> String {
        match &self.held {
            Held::Value(shown) => format!("{}: {}{}", self.reference, shown.text, shown.mark()),
            Held::ValueAndFormula(shown, formula) => format!(
                "{}: {} (={formula}){}",
                self.reference,
                shown.text,
                shown.mark()
            ),
            Held::Formula(formula) => format!("{}: ={formula}", self.reference),
        }
    }

    /// The value the cell shows, when it holds one.
    fn shown(&self) -> Option<&Shown> {
        match &self.held {
            Held::Value(shown) | Held::ValueAndFormula(shown, _) => Some(shown),
            Held::Formula(_) => None,
        }
    }
}

/// The position of a cell address as `(column, row)`: a zero-based column and a
/// one-based row (`B12` -> `(1, 12)`), or `None` when the address is not one.
fn cell_position(reference: &str) -> Option<(u32, u32)> {
    let column = column_index(reference)?;
    let row = reference
        .trim_start_matches(|character: char| character.is_ascii_alphabetic())
        .parse()
        .ok()?;
    Some((column, row))
}

/// The most range-to-cell tests one sheet's marks may cost. Both the ranges and
/// the cells are document-controlled, and only the ranges spanning a cell's own
/// row are ever tested against it, so an ordinary sheet — a handful of merges and
/// links over many cells — cannot spend this; one crafted to put a million ranges
/// over a million cells is marked up to here and the rest is reported, rather
/// than stalling the conversion every reader shares.
const MAX_RANGE_PROBES: usize = 1 << 24;

/// What one sheet's range marks may spend, and whether they ran out.
struct ProbeBudget {
    left: usize,
    exhausted: bool,
}

impl ProbeBudget {
    /// One range-to-cell test: `false` once the budget is spent, which leaves the
    /// rest of the marks out.
    fn spend(&mut self) -> bool {
        if self.left == 0 {
            self.exhausted = true;
            return false;
        }
        self.left -= 1;
        true
    }
}

/// The worksheet's lines: the hidden-geometry annotations first, then one line
/// per valued cell — each carrying its merge mark, then its link mark, when it
/// is part of one — and finally a line per link no cell shows. The flag says the
/// sheet's later range marks were left out because marking them would cost more
/// than [`MAX_RANGE_PROBES`].
fn render_lines(
    cells: &[CellLine],
    hidden_rows: &BTreeSet<u32>,
    hidden_columns: &BTreeSet<u32>,
    merges: &[Merge],
    hyperlinks: &[Link],
) -> (Vec<String>, bool) {
    let mut lines = Vec::new();
    if !hidden_rows.is_empty() {
        lines.push(format!(
            "(hidden rows: {})",
            compress(hidden_rows, '-', |row| row.to_string())
        ));
    }
    if !hidden_columns.is_empty() {
        lines.push(format!(
            "(hidden columns: {})",
            compress(hidden_columns, ':', |column| column_letters(
                column.saturating_sub(1)
            ))
        ));
    }
    // The cells are marked in document order but swept in position order: a range
    // is only ever tested against a cell on a row it spans, so the marks cost the
    // ranges a cell is really in rather than every range declared before it.
    let mut order: Vec<(u32, u32, usize)> = Vec::with_capacity(cells.len());
    for (index, cell) in cells.iter().enumerate() {
        if let Some((column, row)) = cell.position {
            order.push((row, column, index));
        }
    }
    order.sort_unstable();
    let mut budget = ProbeBudget {
        left: MAX_RANGE_PROBES,
        exhausted: false,
    };
    let mut merge_sweep = RowSweep::new(merges.iter().map(|merge| merge.rect));
    let mut link_sweep = RowSweep::new(hyperlinks.iter().map(|link| link.rect));
    let mut merges_at: Vec<Option<(usize, bool)>> = vec![None; cells.len()];
    let mut links_at: Vec<Option<usize>> = vec![None; cells.len()];
    for &(row, column, index) in &order {
        merges_at[index] = containing_merge(column, row, merges, &mut merge_sweep, &mut budget);
        links_at[index] = containing_link(column, row, hyperlinks, &mut link_sweep, &mut budget);
    }
    let mut shown = vec![false; hyperlinks.len()];
    for (index, cell) in cells.iter().enumerate() {
        // A covered cell that kept its own value names the range holding it;
        // the merge's own top-left names the merge instead.
        let mut text = match merges_at[index] {
            Some((merge, true)) => {
                format!("{} (merged {})", cell.text(), merges[merge].reference)
            }
            Some((merge, false)) if cell.shown().is_some() => {
                format!(
                    "{} (in merged range {})",
                    cell.text(),
                    merges[merge].reference
                )
            }
            _ => cell.text(),
        };
        if let Some(link) = links_at[index] {
            shown[link] = true;
            let link = &hyperlinks[link];
            text.push_str(" (link: ");
            // The caption a link carries is what it shows where the cell's own value
            // does not: it is named in front of the target when the two differ, so a
            // caption this reading would otherwise drop is still read.
            let caption = link.display.as_deref().filter(|caption| {
                !caption.is_empty()
                    && cell.shown().map(|shown| shown.text.as_str()) != Some(*caption)
            });
            if let Some(caption) = caption {
                text.push_str(caption);
                text.push_str(" -> ");
            }
            text.push_str(&link.target);
            text.push(')');
        }
        lines.push(text);
    }
    // A link range no cell line covers — a hyperlink over empty cells — still
    // names somewhere to go, so it gets its own line rather than being lost.
    for (index, link) in hyperlinks.iter().enumerate() {
        if shown[index] {
            continue;
        }
        let display = match &link.display {
            Some(display) if !display.is_empty() => format!(" {display}"),
            _ => String::new(),
        };
        lines.push(format!(
            "{}:{display} (link: {})",
            link.reference, link.target
        ));
    }
    (lines, budget.exhausted)
}

/// A sheet's ranges as a sweep down its rows: a range joins the candidates when
/// the first row it spans is reached and leaves once its last row is passed, so a
/// cell is only ever tested against a range that can actually hold it. A cell and
/// a range are both document-controlled, and testing every cell against every
/// range whose corner reaches it — what a sheet with a few dozen merges against a
/// few hundred thousand cells needs — costs their product; the sweep costs each
/// cell the ranges over its own row, plus one push and one pop per range.
struct RowSweep {
    /// Every range's rectangle, by its index in the sheet's own list.
    rects: Vec<Rect>,
    /// `(first row, range index)` sorted by first row: the ranges not yet reached.
    pending: Vec<(u32, usize)>,
    /// How many of `pending` have been reached.
    reached: usize,
    /// The ranges reached and not yet passed, read in the order they leave rather
    /// than the order the sheet declares them: the soonest to expire is at the
    /// front, so dropping the ones a row is past costs a pop each however many are
    /// held — a sweep whose ranges end on different rows never re-reads the ones
    /// that are still holding.
    active: BinaryHeap<Reverse<(u32, usize)>>,
}

impl RowSweep {
    /// A sweep over `rects`, ordered by the row each range begins on.
    fn new(rects: impl Iterator<Item = Rect>) -> Self {
        let rects: Vec<Rect> = rects.collect();
        let mut pending: Vec<(u32, usize)> = rects
            .iter()
            .enumerate()
            .map(|(index, rect)| (rect.min_row, index))
            .collect();
        pending.sort_unstable();
        Self {
            rects,
            pending,
            reached: 0,
            active: BinaryHeap::new(),
        }
    }

    /// The ranges spanning `row`, in no particular order. Rows are queried in
    /// increasing order.
    fn covering(&mut self, row: u32) -> impl Iterator<Item = usize> + '_ {
        while self.reached < self.pending.len() && self.pending[self.reached].0 <= row {
            let index = self.pending[self.reached].1;
            self.reached += 1;
            self.active
                .push(Reverse((self.rects[index].max_row, index)));
        }
        while self
            .active
            .peek()
            .is_some_and(|&Reverse((last_row, _))| last_row < row)
        {
            self.active.pop();
        }
        self.active.iter().map(|&Reverse((_, index))| index)
    }
}

/// The index of the first range in document order whose rectangle holds the cell
/// its `holds` closure answers for — `covering` is the ranges spanning that cell's
/// row — or `None`. Every rectangle actually tested spends one of the sheet's
/// probes.
fn first_holding(
    covering: impl Iterator<Item = usize>,
    budget: &mut ProbeBudget,
    holds: impl Fn(usize) -> bool,
) -> Option<usize> {
    let mut best = None;
    for index in covering {
        if !budget.spend() {
            break;
        }
        if holds(index) {
            best = Some(best.map_or(index, |best: usize| best.min(index)));
        }
    }
    best
}

/// The index of the merge a cell falls in, and whether the cell is that merge's
/// top-left corner — the first merge in document order that holds it.
fn containing_merge(
    column: u32,
    row: u32,
    merges: &[Merge],
    sweep: &mut RowSweep,
    budget: &mut ProbeBudget,
) -> Option<(usize, bool)> {
    let index = first_holding(sweep.covering(row), budget, |index| {
        merges[index].rect.holds(column, row)
    })?;
    Some((index, merges[index].rect.is_corner(column, row)))
}

/// The index of the link a cell falls in, so the line it is rendered on can carry
/// the target and the link is not repeated below.
fn containing_link(
    column: u32,
    row: u32,
    links: &[Link],
    sweep: &mut RowSweep,
    budget: &mut ProbeBudget,
) -> Option<usize> {
    first_holding(sweep.covering(row), budget, |index| {
        links[index].rect.holds(column, row)
    })
}

/// A sorted set of coordinates compressed into runs, each run written by `label`
/// over its endpoints and joined by `joiner`: `5, 12-14` style text.
fn compress(numbers: &BTreeSet<u32>, joiner: char, label: impl Fn(u32) -> String) -> String {
    let mut runs = Vec::new();
    let mut numbers = numbers.iter().copied().peekable();
    while let Some(start) = numbers.next() {
        let mut end = start;
        while numbers.peek().copied() == end.checked_add(1) {
            end += 1;
            numbers.next();
        }
        if start == end {
            runs.push(label(start));
        } else {
            runs.push(format!("{}{joiner}{}", label(start), label(end)));
        }
    }
    runs.join(", ")
}

/// One `<c>` cell being read.
#[derive(Default)]
struct Cell {
    /// The cell address, from `r` or synthesised from its row and column.
    reference: String,
    /// The `t` attribute: how `value` is interpreted.
    kind: Option<String>,
    /// Where this cell's number format comes from: its own `s`, else the default of
    /// its row or of its column, else nothing — or [`Style::Unread`] when the cell
    /// names a style this reader cannot read.
    style: Style,
    /// The `<v>`/inline `<t>` text, raw.
    value: String,
    /// The `<f>` text.
    formula: String,
    /// Whether a `<f>` element was seen.
    has_formula: bool,
    /// `si` of a shared formula definition/reference.
    formula_si: Option<String>,
    /// Whether the `<f>` was `t="shared"`.
    formula_shared: bool,
    /// The cell is a `t="s"` one whose string the shared-string table does not
    /// hold, so its text is lost — reported as a note, never as content.
    shared_lost: bool,
}

impl Cell {
    /// Start a cell from its `<c>` element. A cell with no `s` of its own shows
    /// the default style of its row, else the one of its column, else none — so
    /// its address is settled first, a cell with no `r` included.
    fn start(
        event: &BytesStart<'_>,
        row: u32,
        row_style: Option<Style>,
        columns: &Columns,
        next_column: &mut u32,
    ) -> Self {
        let reference = cell_reference(event, row, next_column);
        let style = match attr(event, b"s") {
            // A cell naming a style this reader cannot read is not one of an
            // unformatted cell: its stored text is shown marked as stored rather
            // than passed off as the display, and the cell takes no row or column
            // style either.
            Some(style) => style.trim().parse().map_or(Style::Unread, Style::Index),
            None => row_style
                .or_else(|| column_style(columns, &reference))
                .unwrap_or_default(),
        };
        Self {
            reference,
            kind: attr(event, b"t"),
            style,
            ..Self::default()
        }
    }

    /// On `<f ...>`: the formula this cell holds, and whether it is a shared
    /// definition (whose text is kept for the references that follow) or one of
    /// those references.
    fn start_formula(&mut self, event: &BytesStart<'_>) {
        self.has_formula = true;
        self.formula_si = attr(event, b"si");
        self.formula_shared = attr(event, b"t").as_deref() == Some("shared");
    }

    /// On `</f>`: a shared definition with text teaches the later references of
    /// the same sheet; a shared reference with none borrows the definition's.
    fn end_formula(&mut self, shared: &mut HashMap<String, String>) {
        if !self.formula_shared {
            return;
        }
        let Some(si) = self.formula_si.take() else {
            return;
        };
        if self.formula.is_empty() {
            if let Some(text) = shared.get(&si) {
                self.formula.clone_from(text);
            }
        } else {
            shared.insert(si, self.formula.clone());
        }
    }

    /// A `<f .../>` shared reference: it carries no text of its own.
    fn resolve_shared_formula(&mut self, event: &BytesStart<'_>, shared: &HashMap<String, String>) {
        self.has_formula = true;
        if attr(event, b"t").as_deref() == Some("shared")
            && let Some(si) = attr(event, b"si")
            && let Some(text) = shared.get(&si)
        {
            self.formula.clone_from(text);
        }
    }

    /// What the cell holds. `None` when it holds neither a value nor a formula.
    fn line(&mut self, book: Book<'_>) -> Option<CellLine> {
        let shown = self.value(book);
        // The formula is the workbook's own text like a value, and it is printed on
        // the cell's line, so it is one line too ([`one_line`]).
        let formula = self
            .has_formula
            .then(|| one_line(self.formula.trim().to_owned()));
        let held = match (shown, formula) {
            (Some(shown), Some(formula)) => Held::ValueAndFormula(shown, formula),
            (Some(shown), None) => Held::Value(shown),
            (None, Some(formula)) => Held::Formula(formula),
            (None, None) => return None,
        };
        Some(CellLine {
            position: cell_position(&self.reference),
            reference: std::mem::take(&mut self.reference),
            held,
        })
    }

    /// The cell's value as shown, resolved by the `t` attribute — `None` when the
    /// cell holds none, or holds one that shows as nothing at all. A shared-string
    /// or inline string keeps its own spacing; every other type is trimmed of the
    /// indentation a pretty-printed part puts inside `<v>`. A value the workbook
    /// wrote with a line break in it is shown as one line (`\n`), so no value can
    /// stand where this reader's own lines do.
    fn value(&mut self, book: Book<'_>) -> Option<Shown> {
        let shown = match self.kind.as_deref() {
            // A `t="s"` cell holds an index into the shared-string table, so one
            // the table does not hold is text that was lost (a table that could
            // not be read at all, or a truncated one, leaves every such cell
            // here). The cell then reads empty and the loss is reported through
            // the notes — the channel this module reports every unreadable part
            // on — rather than as text that cannot be told from the workbook's
            // own content.
            Some("s") => {
                let text = self
                    .value
                    .trim()
                    .parse::<usize>()
                    .ok()
                    .and_then(|index| book.shared.get(index));
                let Some(text) = text else {
                    self.shared_lost = true;
                    return None;
                };
                Shown::displayed(text.clone())
            }
            Some("b") => Shown::displayed(match self.value.trim() {
                "1" => "TRUE".to_owned(),
                "0" => "FALSE".to_owned(),
                other => other.to_owned(),
            }),
            Some("inlineStr") => Shown::displayed(self.value.clone()),
            // A numeric cell (`t` absent or `n`) is shown as its number format
            // displays it.
            None | Some("n") => self.number(book),
            _ => Shown::displayed(self.value.trim().to_owned()),
        };
        (!shown.text.is_empty()).then_some(shown)
    }

    /// A numeric cell's value as shown, or its stored text under [`MARK_STORED`]
    /// when no display of it was produced.
    fn number(&self, book: Book<'_>) -> Shown {
        let raw = self.value.trim().to_owned();
        // A cell's own `s`, else the workbook's first cell format: `s` defaults to
        // the first entry, so a cell naming none and one naming `0` are the same
        // cell to Excel.
        let table = &book.styles.cell_xfs;
        let index = match self.style {
            Style::Unread => return Shown::stored(raw),
            Style::Index(index) => index,
            Style::None => 0,
        };
        let id = match table.get(index) {
            // The schema's default entry 0 — which a cell naming no style also shows —
            // stands in for itself where the workbook declares no format at all, the one
            // way a table this reader did read comes back empty.
            None if index == 0 && !book.styles.lost => 0,
            // Every other entry the table does not hold, and an entry whose own
            // declaration this reader could not read, is a display it cannot know: the
            // cell's stored text is marked rather than passed off as the display.
            None | Some(None) => return Shown::stored(raw),
            Some(Some(id)) => *id,
        };
        let code = book.styles.num_fmts.get(&id).map(String::as_str);
        if numfmt::is_general(id, code) {
            return Shown::displayed(raw);
        }
        let Ok(value) = raw.parse::<f64>() else {
            return Shown::displayed(raw);
        };
        match numfmt::display(value, id, code, book.date1904) {
            Some(shown) => Shown::displayed(shown),
            None => Shown::stored(raw),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ooxml::test_fixtures::zip_fixture;
    use std::fmt::Write as _;

    // ── Excel ───────────────────────────────────────────────────

    const WORKBOOK: &[u8] = br#"<workbook><sheets><sheet name="Data" sheetId="1" r:id="rId1"/><sheet name="More" sheetId="2" r:id="rId2"/></sheets></workbook>"#;

    const WORKBOOK_RELS: &[u8] = br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Target="/xl/worksheets/sheet2.xml"/></Relationships>"#;
    /// One plain string, one rich-text one split across runs, and one written
    /// as numeric character references — the shape a producer that escapes
    /// non-ASCII writes (every Cyrillic letter becomes `&#NNNN;`).
    const SHARED_STRINGS: &[u8] = br"<sst><si><t>hello</t></si><si><r><t>rich</t></r><r><t> text</t></r></si><si><t>&#1046;&#1091;&#1082;</t></si></sst>";

    const SHEET_WITH_CELLS: &[u8] = br#"<worksheet><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c><c r="B1"><v>42</v></c></row><row r="2"><c r="A2"><f>SUM(B1:B2)</f></c><c r="B2" t="b"><v>1</v></c></row><row r="3"><c r="A3" t="inlineStr"><is><t>inline</t></is></c><c><v>7</v></c><c t="str"><f>B1&amp;1</f><v>jog</v></c></row><row r="4"><c r="A4" t="s"><v>1</v></c></row><row r="5"><c r="A5" t="s"><v>2</v></c><c r="B5" t="inlineStr"><is><t>&#1054;&#1090;&#1095;&#1105;&#1090;</t></is></c></row></sheetData></worksheet>"#;

    #[test]
    fn xlsx_reads_cells_in_workbook_order() {
        let bytes = zip_fixture(&[
            ("xl/workbook.xml", WORKBOOK),
            ("xl/_rels/workbook.xml.rels", WORKBOOK_RELS),
            ("xl/sharedStrings.xml", SHARED_STRINGS),
            ("xl/worksheets/sheet1.xml", SHEET_WITH_CELLS),
            (
                "xl/worksheets/sheet2.xml",
                b"<worksheet><sheetData/></worksheet>",
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text {
            text,
            images,
            notes,
            ..
        } = convert_xlsx(&bytes, dir.path())
        else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: hello\n  B1: 42\n  A2: =SUM(B1:B2)\n  B2: TRUE\n  A3: inline\n  B3: 7\n  C3: jog (=B1&1)\n  A4: rich text\n  A5: Жук\n  B5: Отчёт\n\nSheet \"More\": (no values)"
        );
        assert!(images.is_empty(), "a workbook with no media writes none");
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A workbook whose relationship list cannot be read still yields its
    /// sheets: the parts are conventionally numbered, so their names are the
    /// order.
    #[test]
    fn xlsx_falls_back_to_conventional_sheet_parts() {
        let bytes = zip_fixture(&[
            ("xl/workbook.xml", WORKBOOK),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
            ),
            (
                "xl/worksheets/sheet2.xml",
                br#"<worksheet><sheetData><row r="1"><c r="B1"><v>2</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(text, "Sheet \"Data\":\n  A1: 1\n\nSheet \"More\":\n  B1: 2");
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A sheet whose part is absent costs only itself and a note — the sheets
    /// around it are still delivered.
    #[test]
    fn xlsx_keeps_sheets_whose_part_is_missing() {
        let bytes = zip_fixture(&[
            ("xl/workbook.xml", WORKBOOK),
            ("xl/_rels/workbook.xml.rels", WORKBOOK_RELS),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(text, "Sheet \"Data\":\n  A1: 1");
        assert_eq!(notes, ["sheet \"More\" could not be read"]);
    }

    /// A sheet part the package left half written — truncated at an element
    /// boundary, where its events simply end — is reported like one that will not
    /// parse, rather than read as a sheet whose data ends where the truncation is.
    #[test]
    fn xlsx_reports_a_sheet_part_truncated_at_an_element_boundary() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a readable workbook");
        };
        assert_eq!(text, "");
        assert_eq!(notes, ["sheet \"Data\" could not be read"]);
    }

    /// A workbook part the package left half written — cut at an element boundary
    /// after one of its `<sheet/>` entries, or inside a tag — still yields the sheets
    /// it declared before the cut, and the loss of the sheets and names that would
    /// have followed is said once rather than passed off as the whole list.
    #[test]
    fn xlsx_keeps_the_sheets_a_cut_workbook_part_declared() {
        for workbook in [
            // Cut right after the second sheet: `</sheets>` and `</workbook>` are
            // missing, and the events simply end.
            br#"<workbook><sheets><sheet name="Data" r:id="rId1"/><sheet name="More" r:id="rId2"/>"#
                .as_slice(),
            // Cut inside the third sheet's tag, where the walk gets a parse error
            // instead of an ending.
            br#"<workbook><sheets><sheet name="Data" r:id="rId1"/><sheet name="More" r:id="rId2"/><sheet name="Th"#
                .as_slice(),
        ] {
            let bytes = zip_fixture(&[
                ("xl/workbook.xml", workbook),
                (
                    "xl/_rels/workbook.xml.rels",
                    br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Target="worksheets/sheet2.xml"/></Relationships>"#,
                ),
                (
                    "xl/worksheets/sheet1.xml",
                    br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
                ),
                (
                    "xl/worksheets/sheet2.xml",
                    br#"<worksheet><sheetData><row r="1"><c r="A1"><v>2</v></c></row></sheetData></worksheet>"#,
                ),
            ]);
            let dir = tempfile::tempdir().expect("tempdir");
            let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
                panic!("expected Text outcome for a readable workbook");
            };
            assert_eq!(text, "Sheet \"Data\":\n  A1: 1\n\nSheet \"More\":\n  A1: 2");
            assert_eq!(notes, [WORKBOOK_CUT_NOTE]);
        }
    }

    /// A workbook part that is well-formed XML but no workbook — an error page in
    /// its place — carries no workbook element, so it is read like a part the package
    /// does not carry and costs the whole document. A `<workbook/>` declaring no
    /// sheet is a workbook still: it reads as an empty sheet list, with the cause of
    /// the empty answer said rather than left to the delivery layer's "no text".
    #[test]
    fn xlsx_reads_a_part_carrying_no_workbook_element_like_a_missing_one() {
        let dir = tempfile::tempdir().expect("tempdir");
        let html = zip_fixture(&[(
            "xl/workbook.xml",
            br"<html><head><title>404</title></head><body>Not Found</body></html>",
        )]);
        assert!(matches!(
            convert_xlsx(&html, dir.path()),
            DocOutcome::Unreadable { .. }
        ));
        let empty = zip_fixture(&[("xl/workbook.xml", b"<workbook/>".as_slice())]);
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&empty, dir.path()) else {
            panic!("expected Text outcome for a workbook declaring no sheet");
        };
        assert_eq!(text, "");
        assert_eq!(notes, [WORKBOOK_NO_SHEETS_NOTE]);
    }

    /// A sheet or comments part that is well-formed XML but carries no element of
    /// its own — an error page in its place — is read like a part the package does
    /// not carry: the sheet is reported as unreadable rather than as a sheet with no
    /// values, and the comments as lost rather than as a sheet without any. An entry
    /// the package left empty declares a sheet with no values and a comments part
    /// with no comments, so neither is a loss.
    #[test]
    fn xlsx_reads_a_sheet_or_comments_part_carrying_no_such_element_like_a_missing_one() {
        let error_page: &[u8] =
            b"<html><head><title>404</title></head><body>Not Found</body></html>";
        let sheet: &[u8] =
            br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#;
        for (sheet_part, comments_part, expected_text, expected_notes) in [
            (
                error_page,
                b"".as_slice(),
                "",
                &["sheet \"Data\" could not be read"][..],
            ),
            (
                sheet,
                error_page,
                "Sheet \"Data\":\n  A1: 1",
                &["sheet \"Data\": its comments could not be read"][..],
            ),
            (
                b"".as_slice(),
                b"".as_slice(),
                "Sheet \"Data\": (no values)",
                &[][..],
            ),
        ] {
            let bytes = zip_fixture(&[
                (
                    "xl/workbook.xml",
                    br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
                ),
                (
                    "xl/_rels/workbook.xml.rels",
                    br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
                ),
                (
                    "xl/worksheets/_rels/sheet1.xml.rels",
                    br#"<Relationships><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments1.xml"/></Relationships>"#,
                ),
                ("xl/worksheets/sheet1.xml", sheet_part),
                ("xl/comments1.xml", comments_part),
            ]);
            let dir = tempfile::tempdir().expect("tempdir");
            let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
                panic!("expected Text outcome for a readable workbook");
            };
            assert_eq!(text, expected_text);
            assert_eq!(notes, expected_notes);
        }
    }

    /// A sheet the workbook names but gives no relationship is read by the part its
    /// position names rather than dropped: its cells would otherwise vanish, and
    /// every sheet after it would be matched to the wrong scoped defined name.
    #[test]
    fn xlsx_reads_a_sheet_whose_relationship_is_not_named() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/><sheet name="Bare" sheetId="2"/></sheets><definedNames><definedName name="Second" localSheetId="1">First!$A$1</definedName></definedNames></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
            ),
            (
                "xl/worksheets/sheet2.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>2</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: 1\n\nSheet \"Bare\":\n  A1: 2\n\n\
             Defined names:\n  Second (sheet \"Bare\"): First!$A$1"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// Shared formulas: a definition teaches the references that come after it,
    /// so a reference with no text of its own still reads back as its formula.
    #[test]
    fn xlsx_resolves_shared_formulas() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="S" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><f t="shared" ref="A1:A2" si="0">B1*2</f><v>2</v></c></row><row r="2"><c r="A2"><f t="shared" si="0"/><v>4</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(text, "Sheet \"S\":\n  A1: 2 (=B1*2)\n  A2: 4 (=B1*2)");
    }

    /// Media and charts are reported the same way the Word path reports its
    /// media: a raster is written through, an undecodable entry is counted, and a
    /// chart is named for the report rather than silently dropped.
    #[test]
    fn xlsx_extracts_media_and_reports_the_charts_it_cannot_draw() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="S" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
            ),
            ("xl/media/pic.png", b"\x89PNG\r\n\x1a\nfake image bytes"),
            ("xl/media/diagram.emf", b"EMF bytes this stack cannot decode"),
            ("xl/media/scan.tiff", b"TIFF bytes this stack does not convert"),
            ("xl/charts/chart1.xml", b"<chart/>"),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text {
            images,
            notes,
            unshown,
            ..
        } = convert_xlsx(&bytes, dir.path())
        else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(images, vec![dir.path().join("pic.png")]);
        assert_eq!(
            unshown.lines(),
            ["1 chart(s) not shown", "1 embedded drawing(s) not shown",]
        );
        assert_eq!(
            notes,
            ["skipped 1 embedded image(s) in a format this pipeline cannot convert"]
        );
    }

    /// A chart sheet is one chart over a whole tab, and the tab is named for what
    /// it is: the workbook says so through the relationship's kind and the
    /// `chartsheets/` directory its part lives in, so a sheet nothing can be read
    /// from is named a chart sheet rather than one that could not be read. The
    /// chart it draws is resolved through the sheet's own relationships — directly,
    /// or through the drawing part a chartsheet references, which is what Excel
    /// writes — so the sheet's chart is not named a second time as a chart of its
    /// own.
    #[test]
    fn xlsx_names_a_chart_sheet_by_its_kind() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" sheetId="1" r:id="rId1"/><sheet name="Chart1" sheetId="2" r:id="rId2"/><sheet name="Chart2" sheetId="3" r:id="rId3"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet" Target="chartsheets/sheet1.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet" Target="chartsheets/sheet2.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
            ),
            (
                "xl/chartsheets/sheet1.xml",
                br#"<chartsheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><drawing r:id="rId1"/></chartsheet>"#,
            ),
            (
                "xl/chartsheets/_rels/sheet1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" Target="../drawings/drawing1.xml"/></Relationships>"#,
            ),
            ("xl/drawings/drawing1.xml", b"<xdr:wsDr/>"),
            (
                "xl/drawings/_rels/drawing1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" Target="../charts/chart1.xml"/></Relationships>"#,
            ),
            ("xl/charts/chart1.xml", b"<chart/>"),
            (
                "xl/chartsheets/sheet2.xml",
                br#"<chartsheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"/>"#,
            ),
            (
                "xl/chartsheets/_rels/sheet2.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartEx" Target="../charts/chartEx1.xml"/></Relationships>"#,
            ),
            ("xl/charts/chartEx1.xml", b"<chartEx/>"),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text {
            text,
            notes,
            unshown,
            ..
        } = convert_xlsx(&bytes, dir.path())
        else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: 1\n\nSheet \"Chart1\": (chart sheet)\n\nSheet \"Chart2\": (chart sheet)"
        );
        // Each sheet's chart is the sheet: the two losses are named once each, not
        // again as charts of their own.
        assert_eq!(unshown.lines(), ["2 chart sheet(s) not shown"]);
        assert!(
            notes.is_empty(),
            "the report carries the whole loss: {notes:?}"
        );
    }

    /// A chart sheet whose own part the package does not carry is a loss of the
    /// sheet, not of a reading: the tab is still named — there were never cells to
    /// read — so it is reported as a chart sheet rather than a sheet that could
    /// not be read. Its chart, named and absent, costs no other chart its line
    /// either: only a chart part the census counted can be left out of it.
    #[test]
    fn xlsx_a_chart_sheet_whose_part_is_absent_is_not_named_unread() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" sheetId="1" r:id="rId1"/><sheet name="Chart1" sheetId="2" r:id="rId2"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet" Target="chartsheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
            ),
            // The chart sheet names a chart the package does not carry, beside one
            // it does: the missing part is no part of the census, so the chart the
            // workbook really holds keeps its own line.
            (
                "xl/chartsheets/_rels/sheet1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" Target="../charts/chart1.xml"/></Relationships>"#,
            ),
            ("xl/charts/chart2.xml", b"<chart/>"),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text {
            text,
            notes,
            unshown,
            ..
        } = convert_xlsx(&bytes, dir.path())
        else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text, "Sheet \"Data\":\n  A1: 1\n\nSheet \"Chart1\": (chart sheet)",
            "the tab is named for what it is without its part, not dropped"
        );
        assert_eq!(
            unshown.lines(),
            ["1 chart(s) not shown", "1 chart sheet(s) not shown"]
        );
        assert!(
            notes.is_empty(),
            "a chart sheet is named, not reported unread: {notes:?}"
        );
    }

    /// A `t="s"` cell holds an index into the shared-string table, so one the
    /// table does not hold is text that was lost — a table that is truncated or
    /// partly unreadable, or an index past its end. The cell reads empty and the
    /// loss is reported as a note (the channel every unreadable part in this
    /// module uses), never as text the reader cannot tell from the workbook's own
    /// content.
    #[test]
    fn xlsx_reports_shared_string_cells_the_table_does_not_hold() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Sheet1" sheetId="1" r:id="rId1"/></sheets></workbook>"#,
            ),
            // A declared table that stops mid-`<si>`: its first entry is readable
            // and everything after it is not, which is what a truncated part
            // looks like.
            (
                "xl/sharedStrings.xml",
                br"<sst><si><t>First</t></si><si><t>Sec",
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c><c r="C1" t="s"><v>7</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a readable workbook");
        };
        assert_eq!(text, "Sheet \"Sheet1\":\n  A1: First");
        assert_eq!(
            notes,
            ["2 cell(s) left out: the shared string table does not provide their text"]
        );

        // The same for a workbook that carries no table at all.
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Sheet1" sheetId="1" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a readable workbook");
        };
        assert_eq!(text, "Sheet \"Sheet1\": (no values)");
        assert_eq!(
            notes,
            ["1 cell(s) left out: the shared string table does not provide their text"]
        );
    }

    /// The shared-string table is named by a workbook relationship like the
    /// styles part, so a producer that lays it out at a non-conventional path
    /// still has its strings read — the conventional name is only the fallback.
    #[test]
    fn xlsx_resolves_the_shared_string_table_through_the_workbook_rels() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings2.xml"/></Relationships>"#,
            ),
            ("xl/sharedStrings2.xml", SHARED_STRINGS),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>2</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a readable workbook");
        };
        assert_eq!(text, "Sheet \"Data\":\n  A1: hello\n  B1: Жук");
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A cell is shown the way its number format displays it: a date, a
    /// percentage and a custom currency resolved through `numFmtId`s, and the
    /// stored text (marked) for a format this reader cannot reproduce or one
    /// that shows nothing.
    #[test]
    fn xlsx_renders_cells_with_their_number_formats() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#,
            ),
            (
                "xl/styles.xml",
                br##"<styleSheet><numFmts><numFmt numFmtId="164" formatCode="#,##0.00&quot; &#8364;&quot;"/><numFmt numFmtId="165" formatCode="# ?/?"/><numFmt numFmtId="166" formatCode=";;;"/></numFmts><cellXfs count="6"><xf numFmtId="0"/><xf numFmtId="14"/><xf numFmtId="9"/><xf numFmtId="164"/><xf numFmtId="165"/><xf numFmtId="166"/></cellXfs></styleSheet>"##,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1" s="1"><v>46300</v></c><c r="B1" s="2"><v>0.25</v></c><c r="C1" s="3"><v>1234.5</v></c><c r="D1" s="4"><v>1.5</v></c><c r="E1" s="5"><v>5</v></c><c r="F1"><v>42</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: 2026-10-05\n  B1: 25%\n  C1: 1,234.50 €\n  D1: 1.5 (stored number, format not shown)\n  E1: 5 (stored number, format not shown)\n  F1: 42"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A workbook in the 1904 date system shows its serial as a 1904-based
    /// date.
    #[test]
    fn xlsx_reads_the_1904_date_system() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><workbookPr date1904="1"/><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#,
            ),
            (
                "xl/styles.xml",
                br#"<styleSheet><cellXfs count="1"><xf numFmtId="14"/></cellXfs></styleSheet>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1" s="0"><v>0</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(text, "Sheet \"Data\":\n  A1: 1904-01-01");
    }

    /// A cell format declaring no format of its own shows the one the base style
    /// it names holds — how a workbook that leaves its number format to a named
    /// style writes it — so its serial is never passed off as the display; a
    /// format that applies its own declaration keeps it, even when that
    /// declaration is `General`. A base style whose declaration this reader cannot
    /// read, one it names by an `xfId` that does not parse, and one past the
    /// entries the base-style list holds all leave the format the entry would show
    /// unknown — while a format declaring one of its own keeps it, the base style
    /// being only what a declaration of nothing of its own falls back to.
    #[test]
    fn xlsx_inherits_a_cell_format_from_its_base_style() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#,
            ),
            (
                "xl/styles.xml",
                br#"<styleSheet><cellStyleXfs count="4"><xf numFmtId="0"/><xf numFmtId="14"/><xf numFmtId="9"/><xf numFmtId="bad"/></cellStyleXfs><cellXfs count="9"><xf numFmtId="0"/><xf numFmtId="0" xfId="1"/><xf numFmtId="0" xfId="2" applyNumberFormat="0"><alignment horizontal="center"/></xf><xf numFmtId="0" xfId="1" applyNumberFormat="1"/><xf numFmtId="0" xfId="3"/><xf numFmtId="0" xfId="9"/><xf numFmtId="14" xfId="bad"/><xf numFmtId="0" xfId="bad"/><xf numFmtId="0" xfId="bad" applyNumberFormat="1"/></cellXfs></styleSheet>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1" s="0"><v>45000</v></c><c r="B1" s="1"><v>45000</v></c><c r="C1" s="2"><v>0.25</v></c><c r="D1" s="3"><v>45000</v></c><c r="E1" s="4"><v>45000</v></c><c r="F1" s="5"><v>45000</v></c><c r="G1" s="6"><v>45000</v></c><c r="H1" s="7"><v>45000</v></c><c r="I1" s="8"><v>45000</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: 45000\n  B1: 2023-03-15\n  C1: 25%\n  D1: 45000\n  \
             E1: 45000 (stored number, format not shown)\n  \
             F1: 45000 (stored number, format not shown)\n  G1: 2023-03-15\n  \
             H1: 45000 (stored number, format not shown)\n  I1: 45000"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A cell with no `s` of its own shows the default style of its row — only
    /// when the row marks the style custom — else the one of its column (a cell
    /// with no `r` included: its synthesised address is what its column is read
    /// from); a cell that declares a style keeps it, and a row or column that
    /// gives none leaves the stored number.
    #[test]
    fn xlsx_shows_a_rows_or_columns_default_cell_style() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#,
            ),
            (
                "xl/styles.xml",
                br#"<styleSheet><cellXfs count="3"><xf numFmtId="0"/><xf numFmtId="14"/><xf numFmtId="9"/></cellXfs></styleSheet>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><cols><col min="2" max="2" style="2"/><col min="4" max="4" style="1"/></cols><sheetData><row r="1" s="1" customFormat="1"><c r="A1"><v>45000</v></c><c r="B1"><v>45000</v></c><c r="C1" s="0"><v>45000</v></c></row><row r="2"><c r="A2"><v>45000</v></c><c><v>0.25</v></c><c r="D2"><v>45000</v></c></row><row r="3" s="1"><c r="A3"><v>45000</v></c></row><row r="4" customFormat="1"><c r="B4"><v>45000</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: 2023-03-15\n  B1: 2023-03-15\n  C1: 45000\n  A2: 45000\n  B2: 25%\n  D2: 2023-03-15\n  A3: 45000\n  B4: 45000"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A styles part the workbook declares and this reader gets no format table
    /// out of — a mismatched end tag, a part that is no stylesheet at all (an error
    /// page a producer left in the part's place parses as XML just as happily), or
    /// one the package does not carry — costs every cell its format: the loss is
    /// said out loud, and a cell naming a format then reads as its stored text.
    #[test]
    fn xlsx_reports_styles_it_cannot_read() {
        for styles in [
            Some(b"<styleSheet><cellXfs></styleSheet>".as_slice()),
            Some(b"<html><head><title>404</title></head><body>Not Found</body></html>".as_slice()),
            // Truncated at an element boundary, where the events simply end.
            Some(b"<styleSheet><cellXfs>".as_slice()),
            None,
        ] {
            let mut parts: Vec<(&str, &[u8])> = vec![
                (
                    "xl/workbook.xml",
                    br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
                ),
                (
                    "xl/_rels/workbook.xml.rels",
                    br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#,
                ),
                (
                    "xl/worksheets/sheet1.xml",
                    br#"<worksheet><sheetData><row r="1"><c r="A1" s="3"><v>1234.5</v></c></row></sheetData></worksheet>"#,
                ),
            ];
            if let Some(styles) = styles {
                parts.push(("xl/styles.xml", styles));
            }
            let bytes = zip_fixture(&parts);
            let dir = tempfile::tempdir().expect("tempdir");
            let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
                panic!("expected Text outcome for a readable workbook");
            };
            assert_eq!(
                text,
                "Sheet \"Data\":\n  A1: 1234.5 (stored number, format not shown)"
            );
            assert_eq!(notes, [STYLES_NOTE]);
        }
    }

    /// A stylesheet that declares a format but no cell format to name it from — a
    /// `numFmt` (readable or not), a base-style list, or a `cellXfs` list holding no
    /// entry — is one this reader cannot derive any cell's format from: every cell —
    /// a serial a date format would have shown included — comes back as the number
    /// it stores, marked as stored, and the loss is said. A stylesheet declaring
    /// nothing, like `<styleSheet/>`, declares no format for any cell: the cells all
    /// show the default one, and nothing is lost. An `s` that is not even a number is
    /// a different matter again: the workbook says the cell carries a format this
    /// reader cannot read, so its stored value is marked as stored even in the
    /// no-format shape — as is an `s` the empty table does not hold, since entry 0 is
    /// the only one that stands for a cell naming no format.
    #[test]
    fn xlsx_reads_a_stylesheet_declaring_no_cell_format() {
        for (styles, expected_text, expected_notes) in [
            (
                b"<styleSheet/>".as_slice(),
                "Sheet \"Data\":\n  A1: 1234.5\n  \
                 B1: 1234.5 (stored number, format not shown)\n  C1: 46300\n  \
                 D1: 46300 (stored number, format not shown)",
                &[][..],
            ),
            (
                b"<styleSheet><cellXfs/></styleSheet>".as_slice(),
                "Sheet \"Data\":\n  A1: 1234.5 (stored number, format not shown)\n  \
                 B1: 1234.5 (stored number, format not shown)\n  \
                 C1: 46300 (stored number, format not shown)\n  \
                 D1: 46300 (stored number, format not shown)",
                &[STYLES_NOTE][..],
            ),
            (
                b"<styleSheet><numFmts><numFmt numFmtId=\"14\"/></numFmts></styleSheet>"
                    .as_slice(),
                "Sheet \"Data\":\n  A1: 1234.5 (stored number, format not shown)\n  \
                 B1: 1234.5 (stored number, format not shown)\n  \
                 C1: 46300 (stored number, format not shown)\n  \
                 D1: 46300 (stored number, format not shown)",
                &[STYLES_NOTE][..],
            ),
            (
                b"<styleSheet><numFmts><numFmt numFmtId=\"14\" formatCode=\"yyyy-mm-dd\"/></numFmts></styleSheet>".as_slice(),
                "Sheet \"Data\":\n  A1: 1234.5 (stored number, format not shown)\n  \
                 B1: 1234.5 (stored number, format not shown)\n  \
                 C1: 46300 (stored number, format not shown)\n  \
                 D1: 46300 (stored number, format not shown)",
                &[STYLES_NOTE][..],
            ),
        ] {
            let bytes = zip_fixture(&[
                (
                    "xl/workbook.xml",
                    br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
                ),
                (
                    "xl/_rels/workbook.xml.rels",
                    br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#,
                ),
                ("xl/styles.xml", styles),
                (
                    "xl/worksheets/sheet1.xml",
                    br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1234.5</v></c><c r="B1" s="99999999999999999999"><v>1234.5</v></c><c r="C1" s="0"><v>46300</v></c><c r="D1" s="3"><v>46300</v></c></row></sheetData></worksheet>"#,
                ),
            ]);
            let dir = tempfile::tempdir().expect("tempdir");
            let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
                panic!("expected Text outcome for a readable workbook");
            };
            assert_eq!(text, expected_text);
            assert_eq!(notes, expected_notes);
        }
    }

    /// An entry the package left empty declares nothing, so the part it stands for
    /// has no loss to report: an empty styles, comments, workbook-relationships or
    /// sheet-relationships part reads as one that declares no formats, no comments
    /// and no relationships, and no note is added. A part whose bytes are something
    /// other than a document — an error page a producer left in its place — is a
    /// loss, and is reported as one.
    #[test]
    fn xlsx_reads_an_empty_part_as_one_that_declares_nothing() {
        let sheet_rels: &[u8] = br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments1.xml"/></Relationships>"#;
        for (styles, comments, rels, sheet_rels, expected_notes) in [
            (
                b"".as_slice(),
                b"".as_slice(),
                b"".as_slice(),
                b"".as_slice(),
                &[][..],
            ),
            // The byte-order mark a producer may open an empty part with says how the
            // document would be encoded, not that it holds anything: the entry is as
            // empty as one the package left with no bytes at all.
            (
                b"\xEF\xBB\xBF".as_slice(),
                b"\xEF\xBB\xBF".as_slice(),
                b"\xEF\xBB\xBF".as_slice(),
                b"\xEF\xBB\xBF".as_slice(),
                &[][..],
            ),
            (
                b"Not Found".as_slice(),
                b"Not Found".as_slice(),
                b"Not Found".as_slice(),
                sheet_rels,
                &[
                    WORKBOOK_RELS_NOTE,
                    STYLES_NOTE,
                    "sheet \"Data\": its comments could not be read",
                ][..],
            ),
        ] {
            let bytes = zip_fixture(&[
                (
                    "xl/workbook.xml",
                    br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
                ),
                ("xl/_rels/workbook.xml.rels", rels),
                ("xl/styles.xml", styles),
                (
                    "xl/worksheets/sheet1.xml",
                    br#"<worksheet><sheetData><row r="1"><c r="A1" t="inlineStr"><is><t>x</t></is></c></row></sheetData></worksheet>"#,
                ),
                ("xl/worksheets/_rels/sheet1.xml.rels", sheet_rels),
                ("xl/comments1.xml", comments),
            ]);
            let dir = tempfile::tempdir().expect("tempdir");
            let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
                panic!("expected Text outcome for a readable workbook");
            };
            assert_eq!(text, "Sheet \"Data\":\n  A1: x");
            assert_eq!(notes, expected_notes);
        }
    }

    /// A relationships part the package cut at an element boundary names its
    /// remaining parts nowhere, so the list it got to is not the workbook's or the
    /// sheet's own: it is read like one that could not be read at all, and said
    /// rather than passed off as a workbook or a sheet declaring no relationship —
    /// which is what would lose the comments the sheet names after the cut.
    #[test]
    fn xlsx_reports_a_cut_relationships_part() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            // Cut after the sheet's own relationship: the styles part is named after
            // the cut and only found by its conventional name, if at all.
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml">"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
            ),
            // Cut before the comments relationship: the part it names is not read,
            // and the sheet says so rather than reading it as a sheet without one.
            (
                "xl/worksheets/_rels/sheet1.xml.rels",
                br#"<Relationships><Relationship Id="rId1""#,
            ),
            (
                "xl/comments1.xml",
                br#"<comments><authors><author>Ivan</author></authors><commentList><comment ref="A1" authorId="0"><text>note</text></comment></commentList></comments>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a readable workbook");
        };
        assert_eq!(text, "Sheet \"Data\":\n  A1: 1");
        assert_eq!(
            notes,
            [
                WORKBOOK_RELS_NOTE,
                "sheet \"Data\": its relationships could not be read"
            ]
        );
    }

    /// A cell naming a format the workbook's table does not hold — an index past
    /// its `cellXfs` entries, an entry whose own declaration does not parse, an entry
    /// inheriting from a base style that does not parse either, a row or a column
    /// naming an `s` this reader cannot read — shows its stored value marked as
    /// stored: a serial the reader cannot resolve a format for is never passed off as
    /// the display. A cell showing only its formula has no stored value to mark, and a
    /// cell whose row and column declare no style keeps the schema's default.
    #[test]
    fn xlsx_marks_a_cell_whose_format_the_table_does_not_hold() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#,
            ),
            (
                "xl/styles.xml",
                br#"<styleSheet><cellStyleXfs count="1"><xf numFmtId="14"/></cellStyleXfs><cellXfs count="3"><xf numFmtId="0"/><xf numFmtId="14abc"/><xf numFmtId="0" xfId="bad"/></cellXfs></styleSheet>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><cols><col min="3" max="3" style="oops"/></cols><sheetData><row r="1"><c r="A1" s="0"><v>1234.5</v></c><c r="B1" s="3"><v>1234.5</v></c><c r="C1" s="9"><f>SUM(A1)</f></c><c r="D1" s="99999999999999999999"><v>1234.5</v></c><c r="E1" s="1"><v>1234.5</v></c></row><row r="2" s="abc" customFormat="1"><c r="A2"><v>1234.5</v></c><c r="B2"><v>1234.5</v></c></row><row r="3"><c r="A3" s="2"><v>45000</v></c><c r="C3"><v>45000</v></c><c r="D3"><v>45000</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: 1234.5\n  B1: 1234.5 (stored number, format not shown)\n  \
             C1: =SUM(A1)\n  D1: 1234.5 (stored number, format not shown)\n  \
             E1: 1234.5 (stored number, format not shown)\n  \
             A2: 1234.5 (stored number, format not shown)\n  \
             B2: 1234.5 (stored number, format not shown)\n  \
             A3: 45000 (stored number, format not shown)\n  \
             C3: 45000 (stored number, format not shown)\n  D3: 45000"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A hidden sheet is marked in its header — the state a workbook writes is one
    /// of two words, whichever case it writes them in — and a sheet's hidden rows
    /// and columns are compressed and annotated before its cells.
    #[test]
    fn xlsx_marks_hidden_sheets_rows_and_columns() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/><sheet name="Hidden" state="hidden" r:id="rId2"/><sheet name="Secret" state="VERYHIDDEN" r:id="rId3"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Target="worksheets/sheet2.xml"/><Relationship Id="rId3" Target="worksheets/sheet3.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><cols><col min="3" max="5" hidden="1"/><col min="7" hidden="1"/></cols><sheetData><row r="1"><c r="A1"><v>1</v></c></row><row r="5" hidden="1"><c r="A5"><v>5</v></c></row><row r="12" hidden="1"/><row r="13" hidden="true"/><row r="14" hidden="1"/></sheetData></worksheet>"#,
            ),
            (
                "xl/worksheets/sheet2.xml",
                b"<worksheet><sheetData/></worksheet>",
            ),
            (
                "xl/worksheets/sheet3.xml",
                b"<worksheet><sheetData/></worksheet>",
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  (hidden rows: 5, 12-14)\n  (hidden columns: C:E, G)\n  A1: 1\n  A5: 5\n\nSheet \"Hidden\" (hidden): (no values)\n\nSheet \"Secret\" (very hidden): (no values)"
        );
    }

    /// A `<cols>` section that names the same range over and over costs the reader
    /// its own element count, never that count times the grid's width, so a sheet
    /// whose section is huge still marks its own shape rather than stalling.
    #[test]
    fn xlsx_resolves_a_repeated_columns_section_once() {
        let mut sheet = String::from("<worksheet><cols>");
        for _ in 0..20_000u32 {
            sheet.push_str("<col min=\"1\" max=\"16384\" hidden=\"1\" style=\"1\"/>");
        }
        sheet.push_str(
            "</cols><sheetData><row r=\"1\"><c r=\"A1\"><v>45000</v></c></row></sheetData></worksheet>",
        );
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#,
            ),
            (
                "xl/styles.xml",
                br#"<styleSheet><cellXfs count="2"><xf numFmtId="0"/><xf numFmtId="14"/></cellXfs></styleSheet>"#,
            ),
            ("xl/worksheets/sheet1.xml", sheet.as_bytes()),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  (hidden columns: A:XFD)\n  A1: 2023-03-15"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A `<cols>` section the sheet writes after its data reaches no cell of it —
    /// the columns a cell shows are settled the first time a cell asks for them —
    /// so it is counted and said rather than dropped in silence. A `<col>` whose
    /// `min` this reader cannot place is applied to no column at all, and one whose
    /// `max` does not read still hides or styles the column it starts at: the two
    /// are counted and said apart.
    #[test]
    fn xlsx_reports_late_and_unplaceable_column_definitions() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><cols><col min="abc" hidden="1"/><col min="16385" hidden="1"/><col min="2" max="zz" style="1"/><col min="3" max="3" hidden="1"/></cols><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData><cols><col min="4" max="5" hidden="1"/></cols></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(text, "Sheet \"Data\":\n  (hidden columns: C)\n  A1: 1");
        assert_eq!(
            notes,
            [
                "sheet \"Data\": 1 column definition(s) left out: the sheet writes them after \
                 its data, where they reach no cell of it",
                "sheet \"Data\": 2 column definition(s) left out: the columns they hide or \
                 style are not ones this reader can place",
                "sheet \"Data\": 1 column definition(s) cover a range whose end this reader \
                 cannot place, so only the column each one starts at is hidden or styled"
            ]
        );
    }

    /// A merge marks its top-left cell with the range, and a covered cell that
    /// kept its own value with the range that holds it; a covered cell with no
    /// value emits nothing, and a merge over a single cell covers nothing to mark
    /// and is no loss at all.
    #[test]
    fn xlsx_marks_merged_ranges() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1" t="inlineStr"><is><t>Title</t></is></c><c r="B1" t="inlineStr"><is><t>extra</t></is></c></row><row r="3"><c r="A3" t="inlineStr"><is><t>kept</t></is></c></row></sheetData><mergeCells count="3"><mergeCell ref="A1:C1"/><mergeCell ref="A3:B3"/><mergeCell ref="D4"/></mergeCells></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: Title (merged A1:C1)\n  B1: extra (in merged range A1:C1)\n  A3: kept (merged A3:B3)"
        );
        assert!(notes.is_empty(), "nothing was left out: {notes:?}");
    }

    /// A merge is sheet structure, so one whose `ref` is missing or is not a
    /// range this reader can mark is counted and reported rather than dropped in
    /// silence — while the sheet's cells and the merges around it still render.
    #[test]
    fn xlsx_reports_merges_it_cannot_mark() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData><mergeCells count="3"><mergeCell ref="A1:B1"/><mergeCell ref="C3:A1"/><mergeCell/></mergeCells></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(text, "Sheet \"Data\":\n  A1: 1 (merged A1:B1)");
        assert_eq!(
            notes,
            [
                "sheet \"Data\": 2 merged range(s) left out: the cell range they name is not one this reader can mark"
            ]
        );
    }

    /// A sheet declaring far more merged ranges than any real book pays for the
    /// marks past the reader's budget and says so, rather than stalling the
    /// conversion that every reader shares.
    #[test]
    fn xlsx_reports_range_marks_it_could_not_afford() {
        let mut sheet = String::from("<worksheet><sheetData>");
        for row in 1..=4_096u32 {
            let _ = write!(sheet, "<row r=\"{row}\"><c r=\"A{row}\"><v>1</v></c></row>");
        }
        sheet.push_str("</sheetData><mergeCells count=\"8192\">");
        // Every merge spans every cell's row, so each cell's candidates are the
        // whole list and the budget is spent before the cells are through.
        for _ in 0..8_192u32 {
            sheet.push_str("<mergeCell ref=\"A1:B1048576\"/>");
        }
        sheet.push_str("</mergeCells></worksheet>");
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            ("xl/worksheets/sheet1.xml", sheet.as_bytes()),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        // The cells before the budget still carry their mark.
        assert!(text.contains("A1: 1 (merged A1:B1048576)"), "{text}");
        assert!(
            notes
                .iter()
                .any(|note| note.contains("later merged ranges and links left out")),
            "the dropped marks are reported: {notes:?}"
        );
    }

    /// A sheet's ordinary shape — a merged label on every row of a large sheet —
    /// is marked in full rather than cut short: a range is only tested against a
    /// cell on a row it spans, so the ranges a cell is not in cost it nothing.
    #[test]
    fn xlsx_marks_a_large_sheet_within_its_budget() {
        let mut sheet = String::from("<worksheet><sheetData>");
        for row in 1..=2_048u32 {
            let _ = write!(sheet, "<row r=\"{row}\">");
            for column in 0..100u32 {
                let _ = write!(
                    sheet,
                    "<c r=\"{}{row}\"><v>1</v></c>",
                    column_letters(column)
                );
            }
            sheet.push_str("</row>");
        }
        sheet.push_str("</sheetData><mergeCells count=\"2048\">");
        for row in 1..=2_048u32 {
            let _ = write!(sheet, "<mergeCell ref=\"A{row}:B{row}\"/>");
        }
        sheet.push_str("</mergeCells></worksheet>");
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            ("xl/worksheets/sheet1.xml", sheet.as_bytes()),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert!(notes.is_empty(), "nothing was left out: {notes:?}");
        assert!(text.contains("A1: 1 (merged A1:B1)"), "{text}");
        assert!(text.contains("B1: 1 (in merged range A1:B1)"), "{text}");
        assert!(text.contains("A2048: 1 (merged A2048:B2048)"), "{text}");
        assert!(
            text.contains("\n  C1: 1\n"),
            "a cell in no merge is left unmarked: {text}"
        );
    }

    /// A sheet whose ranges all start on the first row and end on their own: every
    /// range is held at once, and each row the sweep reaches is past one more of
    /// them than the row before. The marks must not depend on the order the sweep
    /// drops what it is past, so every cell is marked, and the first merge holding a
    /// row is the shortest one reaching it — the marks a scan of the whole list in
    /// document order would find.
    #[test]
    fn xlsx_marks_ranges_that_end_on_their_own_rows() {
        const ROWS: u32 = 1_024;
        let mut sheet = String::from("<worksheet><sheetData>");
        for row in 1..=ROWS {
            let _ = write!(sheet, "<row r=\"{row}\"><c r=\"A{row}\"><v>1</v></c></row>");
        }
        sheet.push_str("</sheetData><mergeCells count=\"1024\">");
        for row in 2..=ROWS {
            let _ = write!(sheet, "<mergeCell ref=\"A1:B{row}\"/>");
        }
        sheet.push_str("</mergeCells></worksheet>");
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            ("xl/worksheets/sheet1.xml", sheet.as_bytes()),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert!(notes.is_empty(), "nothing was left out: {notes:?}");
        // The first merge holding a row is the shortest one reaching it.
        assert!(text.contains("A1: 1 (merged A1:B2)"), "{text}");
        assert!(text.contains("A2: 1 (in merged range A1:B2)"), "{text}");
        assert!(text.contains("A3: 1 (in merged range A1:B3)"), "{text}");
        assert!(
            text.contains(&format!("A{ROWS}: 1 (in merged range A1:B{ROWS})")),
            "{text}"
        );
    }

    /// A legacy `comments.xml` comment is surfaced after the sheet's cells, its
    /// author resolved through the part's `authorId` index and its rich text
    /// flattened to one line — while an `authorId` the part's authors list does not
    /// hold names an author that is in the file but not in this answer, so the
    /// comment says so rather than reading as one by nobody.
    #[test]
    fn xlsx_reads_a_legacy_cell_comment_with_its_author() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c><c r="B1"><v>2</v></c></row></sheetData></worksheet>"#,
            ),
            (
                "xl/worksheets/_rels/sheet1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments1.xml"/></Relationships>"#,
            ),
            (
                "xl/comments1.xml",
                br#"<comments><authors><author>Ivan Petrov</author></authors><commentList><comment ref="A1" authorId="0"><text><r><t>check</t></r><r><t> this</t></r></text></comment><comment ref="B1" authorId="7"><text>no such author</text></comment></commentList></comments>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: 1\n  B1: 2\n  \
             A1 comment (Ivan Petrov): check this\n  B1 comment: no such author"
        );
        assert_eq!(
            notes,
            ["sheet \"Data\": 1 comment author(s) could not be resolved"]
        );
    }

    /// A workbook whose relationships part is there but is no relationship list at
    /// all falls back to conventional part names: the styles part it finds there is
    /// read as usual, and one it does not find declares no format — nothing is
    /// claimed lost, since a format note must stand for a format a package really
    /// carries. The loss of the workbook's own index is said once either way.
    #[test]
    fn xlsx_reports_a_workbook_whose_relationship_list_is_lost() {
        // An error page in the relationships part reads as well-formed XML.
        let rels = b"<html><head><title>404</title></head><body>Not Found</body></html>".as_slice();
        let styles: &[u8] =
            br#"<styleSheet><cellXfs count="1"><xf numFmtId="14"/></cellXfs></styleSheet>"#;
        for styles in [None, Some(styles)] {
            let mut parts: Vec<(&str, &[u8])> = vec![
                (
                    "xl/workbook.xml",
                    br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
                ),
                ("xl/_rels/workbook.xml.rels", rels),
                (
                    "xl/worksheets/sheet1.xml",
                    br#"<worksheet><sheetData><row r="1"><c r="A1" s="0"><v>46300</v></c></row></sheetData></worksheet>"#,
                ),
            ];
            let expected = if let Some(styles) = styles {
                parts.push(("xl/styles.xml", styles));
                // The conventional part the fallback finds is read as usual.
                "Sheet \"Data\":\n  A1: 2026-10-05"
            } else {
                // No format is lost: the package carries none, so the cell shows
                // what it stores.
                "Sheet \"Data\":\n  A1: 46300"
            };
            let bytes = zip_fixture(&parts);
            let dir = tempfile::tempdir().expect("tempdir");
            let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
                panic!("expected Text outcome for a readable workbook");
            };
            assert_eq!(text, expected);
            assert_eq!(notes, [WORKBOOK_RELS_NOTE]);
        }
    }

    /// A cell with no `s` is the same cell as one naming the first cell format: `s`
    /// defaults to `0`, so both show what the workbook's first format displays.
    #[test]
    fn xlsx_reads_an_s_less_cell_as_the_first_cell_format() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#,
            ),
            (
                "xl/styles.xml",
                br#"<styleSheet><cellXfs count="2"><xf numFmtId="14"/><xf numFmtId="9"/></cellXfs></styleSheet>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>46300</v></c><c r="B1" s="0"><v>46300</v></c><c r="C1" s="1"><v>0.25</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: 2026-10-05\n  B1: 2026-10-05\n  C1: 25%"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A threaded discussion comes back whole, each entry with the name its person
    /// resolves to through the workbook's persons part. The copy a real workbook
    /// keeps in the legacy part as the discussion's stub — Excel's `[Threaded
    /// comment]` preamble, its note to the reader, and each entry's text behind a
    /// `Comment:`/`Reply:` lead of its own — is not repeated, while a legacy comment
    /// that is a comment of its own keeps its line, even when its words stand inside
    /// a threaded one, and a cell with no thread keeps its legacy comment.
    #[test]
    fn xlsx_reads_a_thread_without_repeating_its_legacy_stub() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/person" Target="persons/person.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c><c r="A2"><v>2</v></c><c r="A3"><v>3</v></c><c r="A4"><v>4</v></c></row></sheetData></worksheet>"#,
            ),
            (
                "xl/worksheets/_rels/sheet1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments1.xml"/><Relationship Id="rId2" Type="http://schemas.microsoft.com/office/2017/10/relationships/threadedComment" Target="../threadedComments/threadedComment1.xml"/></Relationships>"#,
            ),
            // The legacy part as Excel itself writes it for a threaded discussion:
            // its own preamble and note to the reader, then each entry's text behind
            // a `Comment:`/`Reply:` lead of its own — the shape the stub has to be
            // recognized in, and the discussion then shown only once.
            (
                "xl/comments1.xml",
                br#"<comments><authors><author>Old Author</author></authors><commentList><comment ref="A1" authorId="0"><text>[Threaded comment]

Your version of Excel allows you to read this threaded comment; however, any edits to it will get removed if the file is opened in a newer version of Excel. Learn more: https://go.microsoft.com/fwlink/?linkid=870924

Comment:
threaded wins
Reply:
a reply</text></comment><comment ref="A2" authorId="0"><text>legacy two</text></comment><comment ref="A3" authorId="0"><text>an older note</text></comment><comment ref="A4" authorId="0"><text>wins</text></comment></commentList></comments>"#,
            ),
            (
                "xl/threadedComments/threadedComment1.xml",
                br#"<ThreadedComments><threadedComment ref="A1" personId="{P1}"><text>threaded wins</text></threadedComment><threadedComment ref="A1" personId="{P2}"><text>a reply</text></threadedComment><threadedComment ref="A3"><text>a newer thread</text></threadedComment><threadedComment ref="A4" personId="{P1}"><text>threaded wins</text></threadedComment></ThreadedComments>"#,
            ),
            (
                "xl/persons/person.xml",
                br#"<personList><person id="{P1}" displayName="Ivan Petrov"/><person id="{P2}" displayName="Maria"/></personList>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: 1\n  A2: 2\n  A3: 3\n  A4: 4\n  \
             A1 comment (Ivan Petrov): threaded wins\n  A1 comment (Maria): a reply\n  \
             A2 comment (Old Author): legacy two\n  \
             A3 comment (Old Author): an older note\n  A3 comment: a newer thread\n  \
             A4 comment (Old Author): wins\n  A4 comment (Ivan Petrov): threaded wins"
        );
        // A thread naming no person loses no author, so nothing is said.
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A comment that names an author this reader cannot resolve — a threaded one
    /// whose `personId` the workbook's person names do not hold, whether the part is
    /// declared and absent, declared and no person list, or not declared at all —
    /// has an author that is in the file but not in this answer, which its sheet says
    /// rather than passing the comment off as one by nobody.
    #[test]
    fn xlsx_reports_a_comment_author_it_cannot_resolve() {
        for (declared, persons) in [
            (true, None),
            // An error page in the part's place reads as well-formed XML.
            (
                true,
                Some(
                    b"<html><head><title>404</title></head><body>Not Found</body></html>"
                        .as_slice(),
                ),
            ),
            (false, None),
        ] {
            let rels: &[u8] = if declared {
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/person" Target="persons/person.xml"/></Relationships>"#
            } else {
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#
            };
            let mut parts: Vec<(&str, &[u8])> = vec![
                (
                    "xl/workbook.xml",
                    br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
                ),
                ("xl/_rels/workbook.xml.rels", rels),
                (
                    "xl/worksheets/sheet1.xml",
                    br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
                ),
                (
                    "xl/worksheets/_rels/sheet1.xml.rels",
                    br#"<Relationships><Relationship Id="rId2" Type="http://schemas.microsoft.com/office/2017/10/relationships/threadedComment" Target="../threadedComments/threadedComment1.xml"/></Relationships>"#,
                ),
                (
                    "xl/threadedComments/threadedComment1.xml",
                    br#"<ThreadedComments><threadedComment ref="A1" personId="{P1}"><text>threaded wins</text></threadedComment></ThreadedComments>"#,
                ),
            ];
            if let Some(persons) = persons {
                parts.push(("xl/persons/person.xml", persons));
            }
            let bytes = zip_fixture(&parts);
            let dir = tempfile::tempdir().expect("tempdir");
            let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
                panic!("expected Text outcome for a well-formed xlsx");
            };
            assert_eq!(
                text,
                "Sheet \"Data\":\n  A1: 1\n  A1 comment: threaded wins"
            );
            assert_eq!(
                notes,
                ["sheet \"Data\": 1 comment author(s) could not be resolved"]
            );
        }
    }

    /// A workbook that declares a person part it does not use — no sheet carrying a
    /// threaded comment — loses nothing when that part cannot be read, so it is not
    /// told about a loss it does not have.
    #[test]
    fn xlsx_ignores_a_persons_part_no_sheet_uses() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/person" Target="persons/person.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
            ),
            (
                "xl/worksheets/_rels/sheet1.xml.rels",
                br#"<Relationships><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments1.xml"/></Relationships>"#,
            ),
            (
                "xl/comments1.xml",
                br#"<comments><authors><author>Ivan Petrov</author></authors><commentList><comment ref="A1" authorId="0"><text>legacy</text></comment></commentList></comments>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: 1\n  A1 comment (Ivan Petrov): legacy"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A comment whose cell reference is not an address names no cell a line could
    /// hang off, so it is left out and counted — and reading the sheet neither
    /// panics nor prints it under an address that is not one.
    #[test]
    fn xlsx_leaves_out_a_comment_whose_cell_is_not_an_address() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
            ),
            (
                "xl/worksheets/_rels/sheet1.xml.rels",
                br#"<Relationships><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments1.xml"/></Relationships>"#,
            ),
            (
                "xl/comments1.xml",
                br#"<comments><commentList><comment ref="A1"><text>kept</text></comment><comment><text>no address</text></comment><comment ref="1"><text>no column</text></comment></commentList></comments>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(text, "Sheet \"Data\":\n  A1: 1\n  A1 comment: kept");
        assert_eq!(
            notes,
            [
                "sheet \"Data\": 2 comment(s) left out: the cell they name is not one this reader can resolve"
            ]
        );
    }

    /// A hyperlink marks the cell it covers with the raw target its `r:id`
    /// resolves to (never re-resolved as a part), a target written as `location`
    /// is used as-is, and a link over no valued cell gets its own line.
    #[test]
    fn xlsx_marks_hyperlinks_on_their_cells_and_alone() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/_rels/sheet1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://example.com/a" TargetMode="External"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1" t="inlineStr"><is><t>Site</t></is></c><c r="B1"><v>2</v></c><c r="E1" t="inlineStr"><is><t>Report</t></is></c><c r="F1"><v>7</v></c></row></sheetData><hyperlinks><hyperlink ref="A1" r:id="rId1"/><hyperlink ref="C1" location="Sheet2!A1" display="Go"/><hyperlink ref="D2" location="Sheet2!B2"/><hyperlink ref="E1" location="Sheet2!C3" display="&#1054;&#1090;&#1095;&#1105;&#1090;"/><hyperlink ref="F1" location="Sheet2!D4" display="7"/></hyperlinks></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        // A caption is named in front of the target where it is not what the cell's
        // own value already says (E1), and left out where it is (F1). A link no cell
        // line covers — one over empty cells — follows every cell line.
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: Site (link: https://example.com/a)\n  B1: 2\n  \
             E1: Report (link: Отчёт -> Sheet2!C3)\n  F1: 7 (link: Sheet2!D4)\n  \
             C1: Go (link: Sheet2!A1)\n  D2: (link: Sheet2!B2)"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A value the workbook (or another producer) wrote with a line break in it is
    /// shown as one line, so no value can break the one-line-per-cell shape or stand
    /// where this reader's own lines do. Every break a text stack may take is
    /// collapsed — `\n` and `\r\n`, and the Unicode breaks a value may carry.
    #[test]
    fn xlsx_shows_a_value_with_a_line_break_as_one_line() {
        let bytes = zip_fixture(&[
            ("xl/workbook.xml", WORKBOOK),
            ("xl/_rels/workbook.xml.rels", WORKBOOK_RELS),
            (
                "xl/sharedStrings.xml",
                br"<sst><si><t>two&#10;lines</t></si></sst>".as_slice(),
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c><c r="B1" t="inlineStr"><is><t>one
two</t></is></c><c r="C1" t="inlineStr"><is><t>cr&#13;&#10;lf</t></is></c><c r="D1" t="inlineStr"><is><t>nel&#x85;sep&#x2028;para</t></is></c></row></sheetData></worksheet>"#,
            ),
            (
                "xl/worksheets/sheet2.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: two\\nlines\n  B1: one\\ntwo\n  C1: cr\\nlf\n  \
             D1: nel\\nsep\\npara\n\nSheet \"More\":\n  A1: 1"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A break in the text the workbook wrote for something other than a cell value
    /// — a hyperlink's caption, a comment's author, a defined name, a sheet name, a
    /// cell's formula and the display a cell's own number format produces — is shown
    /// as `\n` too, so none of them can add a line the workbook does not have: each
    /// keeps its own place in the answer's shape.
    #[test]
    fn xlsx_shows_a_break_in_a_document_text_as_one_line() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                // A sheet name and a defined name, each carrying a break, and the
                // defined name's own text carrying another.
                br#"<workbook><sheets><sheet name="Data&#10;Set" sheetId="1" r:id="rId1"/></sheets><definedNames><definedName name="Ra&#10;te" localSheetId="0">Data&#10;Set!$B$1</definedName></definedNames></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#,
            ),
            (
                // The format code the cell's display comes from carries a break of
                // its own; the first cell format is plain, so a cell showing it is
                // left alone.
                "xl/styles.xml",
                br#"<styleSheet><numFmts><numFmt numFmtId="164" formatCode="0&quot;a&#10;b&quot;"/></numFmts><cellXfs count="2"><xf numFmtId="0"/><xf numFmtId="164"/></cellXfs></styleSheet>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                // The formula carries a break of its own, as does the caption.
                br#"<worksheet><sheetData><row r="1"><c r="A1" t="inlineStr"><is><t>x</t></is></c><c r="B1"><v>1</v><f>SUM(A1,&#10;B1)</f></c><c r="C1" s="1"><v>1</v></c></row></sheetData><hyperlinks><hyperlink ref="A1" location="Sheet2!A1" display="Cap&#10;tion"/></hyperlinks></worksheet>"#,
            ),
            (
                "xl/worksheets/_rels/sheet1.xml.rels",
                br#"<Relationships><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments1.xml"/></Relationships>"#,
            ),
            (
                "xl/comments1.xml",
                br#"<comments><authors><author>Ivan&#10;Petrov</author></authors><commentList><comment ref="A1" authorId="0"><text>note</text></comment></commentList></comments>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\\nSet\":\n  A1: x (link: Cap\\ntion -> Sheet2!A1)\n  \
             B1: 1 (=SUM(A1,\\nB1))\n  \
             C1: 1a\\nb\n  \
             A1 comment (Ivan\\nPetrov): note\n\n\
             Defined names:\n  Ra\\nte (sheet \"Data\\nSet\"): Data\\nSet!$B$1"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A value the format this reader read has no display for is shown as the
    /// stored number under the same mark as one whose format was never read: the
    /// reader reports the one outcome either way.
    #[test]
    fn xlsx_marks_a_value_its_format_has_no_display_for() {
        let bytes = zip_fixture(&[
            ("xl/workbook.xml", WORKBOOK),
            ("xl/_rels/workbook.xml.rels", WORKBOOK_RELS),
            (
                "xl/styles.xml",
                br#"<styleSheet><cellXfs count="2"><xf numFmtId="14"/><xf numFmtId="0"/></cellXfs></styleSheet>"#
                    .as_slice(),
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1" s="0"><v>-5</v></c><c r="B1" s="1"><v>1234.5</v></c><c r="C1" s="0"><v>1e19</v></c></row></sheetData></worksheet>"#,
            ),
            (
                "xl/worksheets/sheet2.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: -5 (stored number, format not shown)\n  \
             B1: 1234.5\n  C1: 1e19 (stored number, format not shown)\n\n\
             Sheet \"More\":\n  A1: 1900-01-01"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A hyperlink whose `r:id` the sheet does not declare is dropped rather
    /// than rendered pointing nowhere, and the loss is counted.
    #[test]
    fn xlsx_reports_links_whose_target_is_not_declared() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/_rels/sheet1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://example.com/a"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData><hyperlinks><hyperlink ref="A1" r:id="rId1"/><hyperlink ref="B1" r:id="rId7"/></hyperlinks></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\":\n  A1: 1 (link: https://example.com/a)"
        );
        assert_eq!(
            notes,
            [
                "sheet \"Data\": 1 link(s) left out: the target or the cell range it names is not one this reader can resolve"
            ]
        );
    }

    /// A range whose minimum is past its maximum (`C3:A1`) names no cell, so an
    /// inverted hyperlink is a link this reader cannot resolve — counted and
    /// reported like any other loss, rather than accepted as an empty rectangle.
    #[test]
    fn xlsx_reports_a_hyperlink_whose_range_is_inverted() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData><hyperlinks><hyperlink ref="C3:A1" location="Sheet2!A1"/></hyperlinks></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(text, "Sheet \"Data\":\n  A1: 1");
        assert_eq!(
            notes,
            [
                "sheet \"Data\": 1 link(s) left out: the target or the cell range it names is not one this reader can resolve"
            ]
        );
    }

    /// Defined names follow the last sheet: a workbook-level one is bare, a
    /// sheet-local one names the sheet its `localSheetId` indexes (a CDATA text
    /// reads the same as a plain one), and Excel's own `_xlnm.` names are left
    /// out.
    #[test]
    fn xlsx_surfaces_defined_names_with_their_sheet() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                br#"<workbook><sheets><sheet name="Data" r:id="rId1"/><sheet name="More" r:id="rId2"/></sheets><definedNames><definedName name="Rate"><![CDATA[Data!$B$1]]></definedName><definedName name="Tax" localSheetId="0" hidden="1">Data!$B$2</definedName><definedName name="Local" localSheetId="1">More!$A$1</definedName><definedName name="NoText"/><definedName name="_xlnm.print_area" localSheetId="0">Data!$A$1:$C$9</definedName></definedNames></workbook>"#,
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Target="worksheets/sheet2.xml"/></Relationships>"#,
            ),
            (
                "xl/worksheets/sheet1.xml",
                b"<worksheet><sheetData/></worksheet>",
            ),
            (
                "xl/worksheets/sheet2.xml",
                b"<worksheet><sheetData/></worksheet>",
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Data\": (no values)\n\nSheet \"More\": (no values)\n\n\
             Defined names:\n  Rate: Data!$B$1\n  Tax (sheet \"Data\"): Data!$B$2\n  Local (sheet \"More\"): More!$A$1"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }

    /// A comments part the sheet names but that cannot be read — one that will not
    /// parse, one a package left half written, and the modern threaded part as
    /// well — costs the comments and is said out loud, naming the part it lost, so
    /// a sheet that lost both is never passed off as a sheet with no comments.
    #[test]
    fn xlsx_reports_a_comments_part_it_cannot_read() {
        let rels = [
            // The legacy part only.
            (br#"<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments1.xml"/>"#.as_slice(), "xl/comments1.xml", "its comments could not be read"),
            // The threaded part only.
            (br#"<Relationship Id="rId1" Type="http://schemas.microsoft.com/office/2017/10/relationships/threadedComment" Target="../threadedComments/threadedComment1.xml"/>"#.as_slice(), "xl/threadedComments/threadedComment1.xml", "its threaded comments could not be read"),
        ];
        for (relationship, part, message) in rels {
            for part_xml in [
                // A mismatched end tag.
                b"<comments><commentList></comments>".as_slice(),
                // Truncated at an element boundary: the events simply end.
                b"<comments><commentList>".as_slice(),
            ] {
                let sheet_rels = format!(
                    "<Relationships>{}</Relationships>",
                    String::from_utf8_lossy(relationship)
                );
                let bytes = zip_fixture(&[
                    (
                        "xl/workbook.xml",
                        br#"<workbook><sheets><sheet name="Data" r:id="rId1"/></sheets></workbook>"#,
                    ),
                    (
                        "xl/_rels/workbook.xml.rels",
                        br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
                    ),
                    (
                        "xl/worksheets/sheet1.xml",
                        br#"<worksheet><sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData></worksheet>"#,
                    ),
                    ("xl/worksheets/_rels/sheet1.xml.rels", sheet_rels.as_bytes()),
                    (part, part_xml),
                ]);
                let dir = tempfile::tempdir().expect("tempdir");
                let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
                    panic!("expected Text outcome for a readable workbook");
                };
                assert_eq!(text, "Sheet \"Data\":\n  A1: 1", "{part}");
                assert_eq!(notes, [format!("sheet \"Data\": {message}")], "{part}");
            }
        }
    }

    /// A working book with the pieces together: a date, a percentage, a
    /// currency amount, a merged heading, a hidden service column, a comment, a
    /// link and a named range — every value shown the way Excel shows it and
    /// every piece of structure marked rather than dropped.
    #[test]
    fn xlsx_reads_a_working_book_end_to_end() {
        let bytes = zip_fixture(&[
            (
                "xl/workbook.xml",
                "<workbook><workbookPr date1904=\"0\"/><sheets>\
                 <sheet name=\"Отчёт\" sheetId=\"1\" r:id=\"rId1\"/>\
                 <sheet name=\"Служебный\" sheetId=\"2\" state=\"hidden\" r:id=\"rId2\"/>\
                 </sheets><definedNames>\
                 <definedName name=\"Ставка\">Отчёт!$B$2</definedName>\
                 <definedName name=\"_xlnm.Print_Area\" localSheetId=\"0\">Отчёт!$A$1:$C$3</definedName>\
                 <definedName name=\"Порог\" localSheetId=\"1\">Служебный!$B$1</definedName>\
                 </definedNames></workbook>"
                    .as_bytes(),
            ),
            (
                "xl/_rels/workbook.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Target="worksheets/sheet2.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#,
            ),
            (
                "xl/styles.xml",
                br##"<styleSheet><numFmts><numFmt numFmtId="164" formatCode="#,##0.00&quot; &#8381;&quot;"/></numFmts><cellXfs count="4"><xf numFmtId="0"/><xf numFmtId="14"/><xf numFmtId="10"/><xf numFmtId="164"/></cellXfs></styleSheet>"##,
            ),
            (
                "xl/sharedStrings.xml",
                "<sst><si><t>Итого</t></si><si><t>Ставка</t></si><si><t>Сайт</t></si></sst>".as_bytes(),
            ),
            (
                "xl/worksheets/sheet1.xml",
                br#"<worksheet><cols><col min="3" max="3" hidden="1"/></cols><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c><c r="C1" s="1"><v>46300</v></c></row><row r="2"><c r="A2" t="s"><v>1</v></c><c r="B2" s="2"><v>0.125</v></c><c r="C2" s="3"><v>1234.5</v></c></row><row r="4"><c r="A4" t="s"><v>2</v></c><c r="B4" t="d" s="1"><v>2024-02-29T13:45:07Z</v></c></row></sheetData><mergeCells count="1"><mergeCell ref="A1:B1"/></mergeCells><hyperlinks><hyperlink ref="A4" r:id="rId1"/></hyperlinks></worksheet>"#,
            ),
            (
                "xl/worksheets/_rels/sheet1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://example.com" TargetMode="External"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments1.xml"/></Relationships>"#,
            ),
            (
                "xl/comments1.xml",
                "<comments><authors><author>Иван Петров</author></authors><commentList>\
                 <comment ref=\"B2\" authorId=\"0\"><text>Проверить ставку</text></comment>\
                 </commentList></comments>"
                    .as_bytes(),
            ),
            (
                "xl/worksheets/sheet2.xml",
                br#"<worksheet><sheetData><row r="1"><c r="B1"><v>1</v></c></row></sheetData></worksheet>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_xlsx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a well-formed xlsx");
        };
        assert_eq!(
            text,
            "Sheet \"Отчёт\":\n  (hidden columns: C)\n  A1: Итого (merged A1:B1)\n  \
             C1: 2026-10-05\n  A2: Ставка\n  B2: 12.50%\n  C2: 1,234.50 ₽\n  \
             A4: Сайт (link: https://example.com)\n  B4: 2024-02-29T13:45:07Z\n  \
             B2 comment (Иван Петров): Проверить ставку\n\n\
             Sheet \"Служебный\" (hidden):\n  B1: 1\n\n\
             Defined names:\n  Ставка: Отчёт!$B$2\n  Порог (sheet \"Служебный\"): Служебный!$B$1"
        );
        assert!(notes.is_empty(), "nothing was skipped: {notes:?}");
    }
}