mahbot 0.10.5

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
//! OOXML package reading: the ZIP half of document conversion.
//!
//! [`crate::document`] owns content-first detection and the PDF arm — the one non-ZIP
//! container; every other format it converts is an OOXML package, read here with the
//! shared `zip` + `quick_xml` plumbing. Word's reader is the module [`docx`], Excel's
//! [`xlsx`]; PowerPoint's slide walk (which also reaches each slide's layout, comments,
//! author registry and diagram data) still lives here. Charts are read by [`chart`].
//!
//! Invariants: a package that cannot be opened, or whose defining part is absent,
//! degrades to [`DocOutcome::Unreadable`], while a merely missing part costs its own
//! sheet or slide plus a note. Every entry is bounded by [`MAX_ZIP_ENTRY_BYTES`], and an
//! artifact is named from the entry's base name alone, so a crafted `../` path cannot
//! escape `out_dir`.

use crate::docgen::ppt_marks;
use crate::document::{
    DRAWING_EXTENSIONS, DocOutcome, SkipReason, SkippedImages, ensure_out_dir, write_picture,
};
use crate::reader_output::{
    Unshown, UnshownKind, slide_comments_header, slide_header, slide_notes_header, text_block,
    text_lines,
};
use crate::util::one_line;
use quick_xml::Reader;
use quick_xml::events::{BytesRef, BytesStart, Event};
use std::collections::{HashMap, HashSet};
use std::io::{Cursor, Read, Seek};
use std::path::{Path, PathBuf};
use zip::ZipArchive;

mod chart;
mod docx;
pub(crate) use docx::{DOCX_BODY_PART, convert_docx};
/// Excel's number formats, shared by the sheet and chart readers.
mod numfmt;
mod xlsx;
pub(crate) use xlsx::{WORKBOOK_PART, convert_xlsx};

/// Maximum bytes decompressed from a single ZIP entry, so a lying size header cannot
/// inflate the temp dir. Deliberately not an aggregate bound: the entry count is open.
const MAX_ZIP_ENTRY_BYTES: u64 = 64 * 1024 * 1024;

/// Part listing the presentation's slides in slide order.
pub(crate) const PPT_PRESENTATION_PART: &str = "ppt/presentation.xml";
/// Relationships of the presentation part: slide relationship id -> slide part.
const PPT_PRESENTATION_RELS_PART: &str = "ppt/_rels/presentation.xml.rels";
/// Base every presentation relationship target resolves against.
const PPT_BASE: &str = "ppt/";
const PPT_SLIDES_PREFIX: &str = "ppt/slides/";
const PPT_MEDIA_PREFIX: &str = "ppt/media/";
/// Charts in a PowerPoint package: `ppt/charts/chart<N>.xml`.
const PPT_CHARTS_PREFIX: &str = "ppt/charts/";
const PPT_EMBEDDINGS_PREFIX: &str = "ppt/embeddings/";
/// The deck's legacy comment-author registry (`p:cmAuthorLst`): a slide's `p:cm`
/// entries name their author by an `authorId` into it.
const PPT_COMMENT_AUTHORS_PART: &str = "ppt/commentAuthors.xml";
/// The deck's 2018 author registry (`p:188:authorLst`): a threaded comment's
/// `authorId` is a GUID into it.
const PPT_MODERN_AUTHORS_PART: &str = "ppt/authors.xml";

/// Extensions of the picture formats a package embeds that this pipeline does not convert and the
/// product's own picture list ([`crate::util::IMAGE_EXTENSIONS`]) does not name: named as images,
/// with the reason, because that is what they are.
const EMBEDDED_EXTRA_IMAGE_EXTENSIONS: &[&str] =
    &["gif", "bmp", "dib", "tif", "tiff", "ico", "jfif"];
const EMBEDDED_AUDIO_EXTENSIONS: &[&str] = &[
    "mp3", "wav", "m4a", "aac", "ogg", "oga", "wma", "flac", "mid", "midi",
];

/// Extensions accepted as OOXML Word packages (`docm` is a macro-enabled docx).
const DOCX_EXTENSIONS: &[&str] = &["docx", "docm"];
/// Extensions accepted as OOXML Excel packages (`xlsm` is macro-enabled xlsx).
const XLSX_EXTENSIONS: &[&str] = &["xlsx", "xlsm"];
/// Extensions accepted as OOXML PowerPoint packages (`pptm` is macro-enabled pptx).
const PPTX_EXTENSIONS: &[&str] = &["pptx", "pptm"];

/// The OOXML family a file name belongs to — the ZIP magic is shared by all three.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Family {
    Docx,
    Xlsx,
    Pptx,
}

impl Family {
    /// The family's name — the spelling the document kit's `format` field uses.
    #[must_use]
    pub(crate) const fn name(self) -> &'static str {
        match self {
            Self::Docx => "docx",
            Self::Xlsx => "xlsx",
            Self::Pptx => "pptx",
        }
    }
}

/// `path`'s OOXML family, or `None`. The extension lists above are the only home of
/// the families' extensions.
#[must_use]
pub(crate) fn family_of(path: &Path) -> Option<Family> {
    if crate::util::has_extension(path, DOCX_EXTENSIONS) {
        Some(Family::Docx)
    } else if crate::util::has_extension(path, XLSX_EXTENSIONS) {
        Some(Family::Xlsx)
    } else if crate::util::has_extension(path, PPTX_EXTENSIONS) {
        Some(Family::Pptx)
    } else {
        None
    }
}

/// Extract slide text, comments, speaker notes and embedded images from a
/// `.pptx`/`.pptm` package.
///
/// Comments and replies are read in both encodings a deck carries (legacy ISO 29500
/// `ppt/comments/comment<N>.xml` and the 2018 threaded one), each author resolved
/// through the deck's own registry. A slide's layout, master and theme are not read, so a
/// chart one of them draws is not described where it sits: the report counts it.
#[must_use]
pub(crate) fn convert_pptx(bytes: &[u8], out_dir: &Path) -> DocOutcome {
    let Ok(mut archive) = ZipArchive::new(Cursor::new(bytes)) else {
        return unreadable(Family::Pptx);
    };
    let presentation_rels = presentation_relationships(&mut archive);
    let Some(parts) = resolve_slide_parts(&mut archive, &presentation_rels) else {
        return unreadable(Family::Pptx);
    };
    let authors = read_ppt_authors(&mut archive, &presentation_rels);

    ensure_out_dir(out_dir);

    let charts = chart::read_charts(&mut archive, PPT_CHARTS_PREFIX, false);
    let mut notes = Vec::new();
    let mut unshown = Unshown::default();
    let mut skipped = SkippedImages::default();
    let images = write_media_parts(
        &mut archive,
        PPT_MEDIA_PREFIX,
        out_dir,
        &mut skipped,
        &mut unshown,
    );

    let mut blocks = Vec::new();
    // A deck's slides share a handful of layouts, so a layout's title indices and a
    // repeated diagram are read once for the whole presentation.
    let mut layouts: HashMap<String, HashSet<String>> = HashMap::new();
    let mut diagrams = DiagramCache::default();
    let mut described = HashSet::new();
    for (index, part) in parts.iter().enumerate() {
        let number = index + 1;
        // A slide the presentation declares but the package does not resolve costs its own
        // number and a note: dropping it would renumber the slides after it.
        let Some(part) = part.as_deref() else {
            notes.push(format!("slide {number} could not be read"));
            continue;
        };
        // Everything the slide owns — relationships, and the notes, layout and diagram parts
        // they name — resolves against the directory the package actually put the slide part
        // in, never the conventional `ppt/slides/`.
        let base = part_dir(part);
        let Some(xml) = read_zip_entry(&mut archive, part).bytes() else {
            notes.push(format!("slide {number} could not be read"));
            continue;
        };
        // A relationships part that could not be read loses the slide's notes, comments,
        // layout, diagrams and charts at once — the slide's own loss, said once here.
        let rels = read_relationships(&mut archive, &rels_part_of(part));
        if rels.is_none() {
            notes.push(format!("slide {number} relationships could not be read"));
        }
        let rels = rels.as_deref();
        // The layout is consulted for its title placeholders — its text never enters the answer.
        let layout_titles = layout_title_indices(&mut archive, base, rels, &mut layouts);
        // A title placeholder names its type on the slide or leaves it to the layout; one that
        // names neither takes the schema's defaults (`obj`, idx 0).
        let is_title = |kind: Option<&str>, idx: Option<&str>| match kind {
            Some("title" | "ctrTitle") => true,
            Some("obj") | None => {
                layout_titles.is_some_and(|titles| titles.contains(idx.unwrap_or("0")))
            }
            Some(_) => false,
        };
        let Some(content) = slide_text(&xml, is_title) else {
            notes.push(format!("slide {number} could not be read"));
            continue;
        };
        // The charts the slide draws are read where they sit: a chart whose content is printed
        // is no longer a loss. A slide whose block is not printed marks nothing, so nothing it
        // draws can vanish from both the reading and the report.
        let chart_lines = rels.map_or_else(Vec::new, |rels| {
            let parts = chart::chart_parts(&xml, rels, base);
            let (lines, chart_described) = chart::lines(&charts, &parts);
            described.extend(chart_described);
            lines
        });
        let shown = slide_diagrams(&mut archive, base, &xml, rels, &mut diagrams);
        blocks.push(slide_block(
            number,
            slide_hidden(&xml),
            &content,
            &shown,
            &chart_lines,
        ));
        // The comments block stands between the slide's own block and its notes.
        let comments = slide_comments(&mut archive, base, rels, &authors);
        comments.place(number, &mut blocks, &mut notes);
        match slide_notes(&mut archive, base, rels) {
            SlideNotes::Text(lines) if !lines.is_empty() => {
                blocks.push(text_lines(&slide_notes_header(number), &lines));
            }
            // The slide declares a notes part, so the text it holds is lost — said, rather than
            // reported as a slide with no notes.
            SlideNotes::Unreadable => {
                notes.push(format!("slide {number} notes could not be read"));
            }
            _ => {}
        }
    }
    unshown.merge(&unopened_parts(
        &mut archive,
        PPT_CHARTS_PREFIX,
        None, // a slide's diagrams are read: this arm prints their text
        PPT_EMBEDDINGS_PREFIX,
        None, // a PowerPoint package has no chart sheets
        &described,
    ));
    notes.extend(skipped.notes());
    let text = blocks.join("\n\n").trim_end().to_string();
    DocOutcome::text(text, images, notes, unshown)
}

/// The diagram text a reading shows for each slide, keyed by the slide part the
/// reading numbers: the text a slide's diagrams show — printed by the reader, never
/// changed by the editor.
///
/// Read here rather than by the document kit's own refusals, so what a diagram shows is
/// decided by the reader's rule alone (see [`slide_diagrams`]). A slide whose diagrams
/// read as lost, one whose diagrams show no text, and one whose part cannot be read are
/// absent from the map. Best effort: a package that is not a presentation, or whose
/// bytes do not open, yields an empty map.
pub(crate) fn pptx_diagram_text(bytes: &[u8]) -> HashMap<String, Vec<String>> {
    let Ok(mut archive) = ZipArchive::new(Cursor::new(bytes)) else {
        return HashMap::new();
    };
    let presentation_rels = presentation_relationships(&mut archive);
    let Some(parts) = resolve_slide_parts(&mut archive, &presentation_rels) else {
        return HashMap::new();
    };
    let mut cache = DiagramCache::default();
    let mut text = HashMap::new();
    for part in parts.into_iter().flatten() {
        let Some(xml) = read_zip_entry(&mut archive, &part).bytes() else {
            continue;
        };
        let rels = read_relationships(&mut archive, &rels_part_of(&part));
        let shown: Vec<String> = slide_diagrams(
            &mut archive,
            part_dir(&part),
            &xml,
            rels.as_deref(),
            &mut cache,
        )
        .into_iter()
        .filter_map(|diagram| match diagram {
            // One entry per diagram that shows text: its points' lines joined the way a reading
            // prints them.
            DiagramRead::Text(lines) if !lines.is_empty() => Some(lines.join("\n")),
            _ => None,
        })
        .collect();
        if !shown.is_empty() {
            text.insert(part, shown);
        }
    }
    text
}

/// The presentation part's relationships: they name the slide list and the
/// comment-author registries, so both are read from one list, once per deck. A part that
/// cannot be read names nothing, and each reader falls back to conventional names.
fn presentation_relationships<R: Read + Seek>(archive: &mut ZipArchive<R>) -> Vec<Relationship> {
    read_relationships(archive, PPT_PRESENTATION_RELS_PART).unwrap_or_default()
}

/// The slide parts in presentation order, one slot per slide the presentation declares;
/// a slot is `None` when that slide's relationship cannot be resolved, so the slides
/// after it keep their own number. `None` for the whole list when the package has
/// neither a presentation part nor conventionally named slide parts.
fn resolve_slide_parts<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    presentation_rels: &[Relationship],
) -> Option<Vec<Option<String>>> {
    let presentation = read_zip_entry(archive, PPT_PRESENTATION_PART).bytes();
    let parts = match presentation.as_deref() {
        Some(xml) => {
            let rels = relationship_targets(presentation_rels);
            let resolved = scan_elements(xml, b"sldId", rel_id).map(|ids| {
                ids.iter()
                    .map(|id| rels.get(id).map(|target| resolve_part(PPT_BASE, target)))
                    .collect::<Vec<_>>()
            });
            match resolved {
                // One resolvable slide is enough to trust the declared order; a
                // list with nothing resolvable at all falls back below.
                Some(parts) if parts.iter().any(Option::is_some) => parts,
                // A presentation whose own slide list cannot be resolved: the
                // parts are conventionally numbered, so their names are the order.
                _ => fallback_slide_parts(archive),
            }
        }
        None => fallback_slide_parts(archive),
    };
    if parts.is_empty() && presentation.is_none() {
        None
    } else {
        Some(parts)
    }
}

/// `ppt/slides/slide<N>.xml` in numeric order — the fallback order for a
/// presentation whose relationship list cannot be read.
fn fallback_slide_parts<R: Read + Seek>(archive: &ZipArchive<R>) -> Vec<Option<String>> {
    let mut slides: Vec<(u32, String)> = archive
        .file_names()
        .filter_map(|name| {
            let number = name
                .strip_prefix(PPT_SLIDES_PREFIX)?
                .strip_prefix("slide")?
                .strip_suffix(".xml")?
                .parse()
                .ok()?;
            Some((number, name.to_owned()))
        })
        .collect();
    slides.sort();
    slides.into_iter().map(|(_, name)| Some(name)).collect()
}

/// A slide's speaker notes, as resolved through the relationships beside the
/// slide part.
enum SlideNotes {
    /// The slide declares no notes part — the usual case.
    None,
    Text(Vec<String>),
    /// The slide declares a notes part, but it could not be read. A relationships
    /// part that could not be read names no notes part here: the caller states
    /// that wider loss once.
    Unreadable,
}

/// The speaker notes a slide declares through `rels` — their targets resolved
/// against the slide part's own directory (`base`) — as their non-empty text
/// lines: never by slide index, and never a note of another slide's. Only a notes part
/// the slide declares and the reading could not show is [`SlideNotes::Unreadable`].
fn slide_notes<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    base: &str,
    rels: Option<&[Relationship]>,
) -> SlideNotes {
    let Some(rels) = rels else {
        return SlideNotes::None;
    };
    let Some(target) = rels
        .iter()
        .find(|rel| rel.kind.ends_with("/notesSlide"))
        .map(|rel| rel.target.as_str())
    else {
        return SlideNotes::None;
    };
    match read_zip_entry(archive, &resolve_part(base, target)) {
        ZipEntry::Bytes(xml) => slide_text(&xml, |_, _| false)
            .map_or(SlideNotes::Unreadable, |text| SlideNotes::Text(text.lines)),
        ZipEntry::Missing | ZipEntry::Unreadable => SlideNotes::Unreadable,
    }
}

/// A slide's comments and their replies, as resolved through the relationships
/// beside the slide part.
#[derive(Default)]
struct SlideComments {
    /// One line per comment or reply, in document order: its author when the deck's
    /// registry resolves the entry's id, then its text flattened to one line.
    lines: Vec<String>,
    /// Whether a comments part the slide declares could not be read.
    lost: bool,
}

impl SlideComments {
    /// Put the slide's comments where they belong: its block of lines after the slide's own
    /// block, and the note naming an unread part among the slide's losses.
    fn place(&self, number: usize, blocks: &mut Vec<String>, notes: &mut Vec<String>) {
        if !self.lines.is_empty() {
            blocks.push(text_lines(&slide_comments_header(number), &self.lines));
        }
        if self.lost {
            notes.push(format!("slide {number} comments could not be read"));
        }
    }
}

/// The comments and replies a slide declares through `rels` — either encoding, in the
/// order its relationships name them — with each entry's author resolved through the
/// deck's registry. A declared part the package does not hold, or one unreadable whole,
/// sets [`SlideComments::lost`]: losing a comment is said, not passed off as a slide
/// with none. A slide declaring both encodings shows both.
fn slide_comments<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    base: &str,
    rels: Option<&[Relationship]>,
    authors: &HashMap<String, String>,
) -> SlideComments {
    let Some(rels) = rels else {
        return SlideComments::default();
    };
    let mut comments = SlideComments::default();
    // The legacy kind and the 2018 threaded one both end `/comments`; one walk
    // reads either, so both are collected here in the order the slide names them.
    for rel in rels.iter().filter(|rel| rel.kind.ends_with("/comments")) {
        match read_zip_entry(archive, &resolve_part(base, &rel.target))
            .bytes()
            .and_then(|xml| comment_lines(&xml, authors))
        {
            Some(lines) => comments.lines.extend(lines),
            None => comments.lost = true,
        }
    }
    comments
}

/// One comment or reply: the id of the author the deck's registry names it by, its
/// text, and its open position — a reply closes before the comment it answers, so the
/// open order is what puts it after.
struct CommentEntry {
    order: usize,
    author_id: Option<String>,
    text: String,
}

impl CommentEntry {
    /// The entry's line: its author followed by `: ` when the registry resolves the id,
    /// then its text as one line ([`flatten`]). Every entry the part holds is shown, so one
    /// with no words prints the no-text mark rather than passing for no comment. The text is
    /// document-controlled and never cut.
    fn line(&self, authors: &HashMap<String, String>) -> String {
        let text = flatten(&self.text);
        let text = if text.is_empty() {
            ppt_marks().no_text.clone()
        } else {
            text
        };
        match self.author_id.as_deref().and_then(|id| authors.get(id)) {
            Some(author) => format!("{author}: {text}"),
            None => text,
        }
    }
}

/// The comments and replies a comments part holds, one line each, in document order. One
/// walk serves both encodings: legacy ISO 29500 and the 2018 threaded one.
///
/// `None` when the part cannot be read as XML, is cut short, or is no comments document.
fn comment_lines(xml: &[u8], authors: &HashMap<String, String>) -> Option<Vec<String>> {
    let mut part = Part::new(xml);
    let mut reader = Reader::from_reader(xml);
    let mut buffer = Vec::new();
    let mut runs = TextRuns::default();
    // The entries open right now, innermost last: a reply is pushed onto the comment it
    // answers, and its text belongs to the innermost entry.
    let mut open: Vec<CommentEntry> = Vec::new();
    let mut entries: Vec<CommentEntry> = Vec::new();
    let mut order = 0usize;
    loop {
        let Ok(event) = reader.read_event_into(&mut buffer) else {
            return None;
        };
        part.note(&event);
        if runs.note(&event) {
            flush_paragraph(&mut runs, &mut open);
        }
        match event {
            Event::Start(event) => match event.local_name().as_ref() {
                b"cmLst" => part.kind_seen(),
                b"cm" | b"reply" => {
                    // Text read before the entry opened belongs to no comment: it
                    // is discarded here rather than leaking into this one.
                    runs.discard();
                    open.push(CommentEntry {
                        order,
                        author_id: attr(&event, b"authorId").map(|id| id.trim().to_owned()),
                        text: String::new(),
                    });
                    order += 1;
                }
                _ => {}
            },
            Event::Empty(event) if event.local_name().as_ref() == b"cmLst" => {
                part.kind_seen();
            }
            Event::End(event) => {
                if matches!(event.local_name().as_ref(), b"cm" | b"reply") {
                    flush_paragraph(&mut runs, &mut open);
                    if let Some(entry) = open.pop() {
                        entries.push(entry);
                    }
                }
            }
            Event::Eof => break,
            _ => {}
        }
        buffer.clear();
    }
    if part.unreadable() {
        return None;
    }
    entries.sort_by_key(|entry| entry.order);
    Some(entries.iter().map(|entry| entry.line(authors)).collect())
}

/// Hand the paragraph `runs` has finished to the innermost open entry, separated from
/// the one before, and clear it: text outside any entry belongs to no comment.
fn flush_paragraph(runs: &mut TextRuns, open: &mut [CommentEntry]) {
    if let Some(entry) = open.last_mut() {
        entry.text.push_str(&runs.paragraph);
        entry.text.push(' ');
    }
    runs.discard();
}

/// The names a deck's comments call their authors by, keyed by the ids they carry: the
/// legacy registry and the 2018 one, each falling back to its conventional part.
///
/// A registry the package lacks, or one unreadable, resolves no author.
fn read_ppt_authors<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    rels: &[Relationship],
) -> HashMap<String, String> {
    let legacy = related_part(
        rels,
        PPT_BASE,
        |kind| kind.ends_with("/commentAuthors"),
        PPT_COMMENT_AUTHORS_PART,
    );
    let modern = related_part(
        rels,
        PPT_BASE,
        |kind| kind.contains("2018/10") && kind.ends_with("/authors"),
        PPT_MODERN_AUTHORS_PART,
    );
    let mut authors = HashMap::new();
    for part in [legacy, modern] {
        if let Some(xml) = read_zip_entry(archive, &part).bytes() {
            authors.extend(author_names(&xml));
        }
    }
    authors
}

/// The ids and names an author registry part holds — legacy `p:cmAuthor` or 2018
/// `p:188:author` entries, which differ in element name alone. A part that will not parse
/// holds none.
fn author_names(xml: &[u8]) -> HashMap<String, String> {
    scan_names(xml, &[b"cmAuthor", b"author"], author_entry)
        .unwrap_or_default()
        .into_iter()
        .collect()
}

/// One author entry: the id its comments carry and the name they are shown by. One a
/// registry leaves empty names nobody, so the entry is left out and its comments print
/// without an author.
///
/// The name is the file's own text, printed whole as the comment it prefixes is: only what
/// a mark adds around a file's text is bounded here — a field instruction, a link's address
/// — never the text itself.
fn author_entry(event: &BytesStart<'_>) -> Option<(String, String)> {
    let id = attr(event, b"id")?.trim().to_owned();
    let name = one_line(attr(event, b"name")?);
    (!name.is_empty()).then_some((id, name))
}

/// A part's `<a:t>` text, split by whether it belongs to the title placeholder.
struct SlideText {
    /// The title placeholder's lines, in part order (empty when there is no title).
    title: Vec<String>,
    /// Every other line the part holds, unchanged from what the reader showed.
    lines: Vec<String>,
}

/// The text runs one part's walk accumulates: each `<a:t>`'s text joined into the
/// paragraph being read, `<a:br/>` breaking it into lines, and an `<a:fld>`'s text left
/// out. One implementation, so the slide, diagram and comments walks read a run alike —
/// a legacy comment's `<p:text>` is a run's text too.
#[derive(Default)]
struct TextRuns {
    /// The paragraph being read right now: the run text seen since the last paragraph
    /// end moved it out.
    paragraph: String,
    /// Whether a run's text element is open — `a:t`, or the `p:text` a legacy comment
    /// writes its body in.
    in_text: bool,
    /// How many `<a:fld>` elements are open: their text is the field's, not the run's.
    fields: usize,
}

impl TextRuns {
    /// Note one event of the walk, accumulating run text, and return `true` on the `</a:p>`
    /// that ends a paragraph.
    fn note(&mut self, event: &Event<'_>) -> bool {
        match event {
            Event::Start(event) => match event.local_name().as_ref() {
                b"t" | b"text" => self.in_text = true,
                b"br" => self.paragraph.push('\n'),
                b"fld" => self.fields += 1,
                _ => {}
            },
            Event::Empty(event) => {
                if event.local_name().as_ref() == b"br" {
                    self.paragraph.push('\n');
                }
            }
            Event::Text(event) if self.in_text && self.fields == 0 => {
                if let Ok(chunk) = event.xml10_content() {
                    self.paragraph.push_str(&chunk);
                }
            }
            Event::GeneralRef(event) if self.in_text && self.fields == 0 => {
                append_entity(&mut self.paragraph, event);
            }
            Event::End(event) => match event.local_name().as_ref() {
                b"t" | b"text" => self.in_text = false,
                b"fld" => self.fields = self.fields.saturating_sub(1),
                b"p" => return true,
                _ => {}
            },
            _ => {}
        }
        false
    }

    /// Clear the paragraph being read: the caller read it where it belongs to no block, so
    /// it must not leak into a later one.
    fn discard(&mut self) {
        self.paragraph.clear();
    }

    /// Move the paragraph being read into `lines` ([`push_lines`]), one line per
    /// `<a:br/>` break.
    fn lines(&mut self, lines: &mut Vec<String>) {
        push_lines(lines, &mut self.paragraph);
    }
}

/// A part's `<a:t>` text, in document order: a newline at `<a:br/>` and each `<a:p>`
/// end, nothing from an `<a:fld>`. Text of a `<p:sp>` whose `<p:ph>` declares the title
/// goes to [`SlideText::title`]; everything else to [`SlideText::lines`]. `is_title`
/// receives each `<p:ph>`'s `type` and `idx`. Empty paragraphs are dropped; `None` on any
/// XML error.
fn slide_text(
    xml: &[u8],
    is_title: impl Fn(Option<&str>, Option<&str>) -> bool,
) -> Option<SlideText> {
    let mut reader = Reader::from_reader(xml);
    let mut buffer = Vec::new();
    // One entry per open `<p:sp>`: whether that shape is the title. `<p:ph>` sets the
    // innermost entry, and a run with no shape open is body text.
    let mut shapes: Vec<bool> = Vec::new();
    let mut runs = TextRuns::default();
    let mut title = Vec::new();
    let mut lines = Vec::new();
    loop {
        let Ok(event) = reader.read_event_into(&mut buffer) else {
            return None;
        };
        // Note the runs first, so a paragraph accumulates whichever event is
        // the one that ends it.
        let ended = runs.note(&event);
        match event {
            Event::Start(event) => match event.local_name().as_ref() {
                b"sp" => shapes.push(false),
                b"ph" => mark_title(&mut shapes, &event, &is_title),
                _ => {}
            },
            Event::Empty(event) => {
                if event.local_name().as_ref() == b"ph" {
                    mark_title(&mut shapes, &event, &is_title);
                }
            }
            Event::End(event) => {
                if event.local_name().as_ref() == b"sp" {
                    shapes.pop();
                }
            }
            Event::Eof => break,
            _ => {}
        }
        if ended {
            // The shape a paragraph sits in is still open here, so its title flag is paragraph's
            // own.
            if shapes.last() == Some(&true) {
                runs.lines(&mut title);
            } else {
                runs.lines(&mut lines);
            }
        }
        buffer.clear();
    }
    Some(SlideText { title, lines })
}

/// Set the innermost open `<p:sp>`'s title flag from a `<p:ph>`; a no-op when no shape is open.
fn mark_title(
    shapes: &mut [bool],
    event: &BytesStart<'_>,
    is_title: &impl Fn(Option<&str>, Option<&str>) -> bool,
) {
    if let Some(shape) = shapes.last_mut() {
        *shape = is_title(
            attr(event, b"type").as_deref(),
            attr(event, b"idx").as_deref(),
        );
    }
}

/// Move `current` into `lines`, one trimmed line per `<a:br/>` break, dropping empties.
fn push_lines(lines: &mut Vec<String>, current: &mut String) {
    for line in std::mem::take(current).split('\n') {
        let line = line.trim();
        if !line.is_empty() {
            lines.push(line.to_owned());
        }
    }
}

/// Whether the presentation hides the slide (`<p:sld ... show="0">`). Matching the
/// attribute by local name keeps `showMasterSp`/`showMasterPhAnim` from being mistaken
/// for it.
fn slide_hidden(xml: &[u8]) -> bool {
    scan_elements(xml, b"sld", |event| attr(event, b"show"))
        .and_then(|values| values.into_iter().next())
        .is_some_and(|value| value == "0" || value.eq_ignore_ascii_case("false"))
}

/// The `idx` values of the title placeholders the slide's layout declares, memoized in
/// `cache` by layout part; the layout's target resolves against `base`.
///
/// A slide's title placeholder sometimes carries its type on the layout, so the layout is
/// consulted for its title indices and nothing else. `None` when the slide declares no
/// layout relationship; a missing, unreadable or unparsable layout reads as one
/// declaring nothing.
fn layout_title_indices<'a, R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    base: &str,
    rels: Option<&[Relationship]>,
    cache: &'a mut HashMap<String, HashSet<String>>,
) -> Option<&'a HashSet<String>> {
    let target = rels?
        .iter()
        .find(|rel| rel.kind.ends_with("/slideLayout"))
        .map(|rel| rel.target.as_str())?;
    let part = resolve_part(base, target);
    Some(
        cache
            .entry(part.clone())
            .or_insert_with(|| read_layout_title_indices(archive, &part)),
    )
}

/// [`layout_title_indices`] for one layout part, read from the package.
fn read_layout_title_indices<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    part: &str,
) -> HashSet<String> {
    let Some(xml) = read_zip_entry(archive, part).bytes() else {
        return HashSet::new();
    };
    scan_elements(&xml, b"ph", |event| {
        matches!(attr(event, b"type").as_deref(), Some("title" | "ctrTitle"))
            .then(|| attr(event, b"idx").unwrap_or_else(|| "0".to_owned()))
    })
    .unwrap_or_default()
    .into_iter()
    .collect()
}

/// What reading one diagram on a slide produced.
#[derive(Clone)]
enum DiagramRead {
    /// The visible text of the diagram's data model, in the model's own order.
    Text(Vec<String>),
    /// The diagram's data could not be read: its text is lost, and the slide says so.
    Lost,
}

/// The text a deck's diagram data parts were read as, keyed by part: a deck that shows
/// one diagram on several slides reads its part once, like its layouts.
#[derive(Default)]
struct DiagramCache(HashMap<String, DiagramRead>);

impl DiagramCache {
    /// The text of one diagram's data part, read on first use: `Lost` for a part
    /// the package does not hold or one [`diagram_text_lines`] cannot read.
    fn read<R: Read + Seek>(&mut self, archive: &mut ZipArchive<R>, part: &str) -> DiagramRead {
        if let Some(read) = self.0.get(part) {
            return read.clone();
        }
        let read = read_zip_entry(archive, part)
            .bytes()
            .and_then(|xml| diagram_text_lines(&xml))
            .map_or(DiagramRead::Lost, DiagramRead::Text);
        self.0.insert(part.to_owned(), read.clone());
        read
    }
}

/// The diagrams a slide shows, in the order its shapes present them, read from
/// their data parts through the relationships the slide part declares, targets resolved
/// against `base` and text taken from `cache`, so a part shown on several slides is
/// parsed once.
///
/// Every diagram is read once per slide: a data part a slide refers to several times
/// yields one section.
fn slide_diagrams<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    base: &str,
    xml: &[u8],
    rels: Option<&[Relationship]>,
    cache: &mut DiagramCache,
) -> Vec<DiagramRead> {
    // A `dgm:relIds` carries `r:dm`, `r:lo`, `r:qs` and `r:cs`; `attr` matches by local
    // name, so this is the data-model reference. Every `relIds` a slide holds is collected:
    // a frame whose `relIds` names no data model still shows a diagram, and one read as no
    // diagram would be dropped in silence.
    let Some(ids) = scan_elements(xml, b"relIds", |event| Some(attr(event, b"dm"))) else {
        return Vec::new();
    };
    let mut seen = HashSet::new();
    let mut diagrams = Vec::new();
    for id in ids {
        // No reference to resolve and nothing to dedupe on: the frame keeps its section, lost.
        let Some(id) = id else {
            diagrams.push(DiagramRead::Lost);
            continue;
        };
        let part = rels.and_then(|rels| {
            rels.iter()
                .find(|rel| rel.id == id)
                .map(|rel| resolve_part(base, &rel.target))
        });
        // Dedupe on the resolved part (never across slides, which read the same diagram with
        // their own block); an id nothing resolves keeps its own section.
        let key = part.clone().unwrap_or_else(|| format!("#{id}"));
        if !seen.insert(key) {
            continue;
        }
        let read = match part {
            Some(part) => cache.read(archive, &part),
            None => DiagramRead::Lost,
        };
        diagrams.push(read);
    }
    diagrams
}

/// One open `<dgm:pt>` of a diagram's data model: whether it is an editor
/// placeholder, and the text read from it so far.
struct DiagramPoint {
    placeholder: bool,
    lines: Vec<String>,
}

/// The text a person sees in a diagram's data model: the `<dgm:t>` text of every
/// `<dgm:pt>` not flagged as an editor placeholder, flat, in document order (the
/// `dgm:cxn` structure is not reproduced). `None` for a part that is not a diagram's data
/// model, one cut short, or one left empty — the slide states the loss.
fn diagram_text_lines(xml: &[u8]) -> Option<Vec<String>> {
    let mut part = Part::new(xml);
    let mut reader = Reader::from_reader(xml);
    let mut buffer = Vec::new();
    let mut points: Vec<DiagramPoint> = Vec::new();
    let mut runs = TextRuns::default();
    let mut lines = Vec::new();
    loop {
        let Ok(event) = reader.read_event_into(&mut buffer) else {
            return None;
        };
        part.note(&event);
        let ended = runs.note(&event);
        match event {
            Event::Start(event) => match event.local_name().as_ref() {
                b"dataModel" => part.kind_seen(),
                b"pt" => {
                    // Text read before the point opened belongs to no node: it is discarded here.
                    runs.discard();
                    points.push(DiagramPoint {
                        placeholder: false,
                        lines: Vec::new(),
                    });
                }
                b"prSet" => mark_placeholder(&mut points, &event),
                _ => {}
            },
            Event::Empty(event) => match event.local_name().as_ref() {
                // A self-closing `<dgm:dataModel/>` is a data model too.
                b"dataModel" => part.kind_seen(),
                b"prSet" => mark_placeholder(&mut points, &event),
                _ => {}
            },
            Event::End(event) => {
                if event.local_name().as_ref() == b"pt"
                    && let Some(point) = points.pop()
                    && !point.placeholder
                {
                    lines.extend(point.lines);
                }
            }
            Event::Eof => break,
            _ => {}
        }
        if ended {
            // A paragraph read with no point open belongs to no node and must not leak onward.
            match points.last_mut() {
                Some(point) => runs.lines(&mut point.lines),
                None => runs.discard(),
            }
        }
        buffer.clear();
    }
    (!part.unreadable() && !blank(xml)).then_some(lines)
}

/// Mark the innermost open `<dgm:pt>` a placeholder from its `<dgm:prSet>`: the point's
/// last `prSet` decides, and its `phldr` is `"1"`/`"true"`. A `phldrT` alone is not that
/// flag — ECMA-376 gives a point with its own text both the hint and the flag, and the
/// text is what a person sees.
fn mark_placeholder(points: &mut [DiagramPoint], event: &BytesStart<'_>) {
    let Some(point) = points.last_mut() else {
        return;
    };
    point.placeholder = attr(event, b"phldr")
        .is_some_and(|value| value == "1" || value.eq_ignore_ascii_case("true"));
}

/// One slide block: `Slide {n}:`, ` (hidden slide)` when hidden, then the title lines,
/// the body lines, one section per diagram (its `(diagram text)` heading over indented
/// node lines, or the unreadable mark) and one per chart, printed where it sits. A slide
/// holding a diagram or chart thus never reads `(no text)`, which [`text_block`] supplies
/// only for a slide holding nothing at all.
fn slide_block(
    number: usize,
    hidden: bool,
    content: &SlideText,
    diagrams: &[DiagramRead],
    charts: &[String],
) -> String {
    let marks = ppt_marks();
    let header = slide_header(number, hidden);
    let mut lines: Vec<String> = content
        .title
        .iter()
        .map(|line| format!("{} {line}", marks.title))
        .collect();
    lines.extend(content.lines.iter().cloned());
    for diagram in diagrams {
        match diagram {
            DiagramRead::Lost => lines.push(marks.diagram_text_lost.clone()),
            DiagramRead::Text(nodes) => {
                lines.push(marks.diagram_text.clone());
                lines.extend(nodes.iter().map(|node| format!("  {node}")));
            }
        }
    }
    lines.extend(charts.iter().cloned());
    text_block(&header, &marks.no_text, &lines)
}

/// What a package's media part is as far as its file name says: a picture this pipeline
/// converts or does not, or one of the kinds the report names. Calling a video or drawing
/// an unconvertible image would report a loss the file does not hold.
enum PackageMedia {
    /// A picture this pipeline does not convert: counted with its reason in
    /// [`SkippedImages`] rather than named by kind of its own, because that is what
    /// it is. The formats the pipeline does convert never reach here —
    /// [`write_media_parts`] writes those out first.
    Image,
    /// Content of a kind the report names as what it is.
    Unshown(UnshownKind),
}

/// [`PackageMedia`] for one media part's file name. A name of no recognized kind
/// is `Media` rather than an image: a package calling an unknown thing a media
/// part says no more about it than that, and the report repeats no more.
fn package_media(file_name: &str) -> PackageMedia {
    let path = Path::new(file_name);
    if crate::util::is_image_extension(path)
        || crate::util::has_extension(path, EMBEDDED_EXTRA_IMAGE_EXTENSIONS)
    {
        PackageMedia::Image
    } else if crate::util::has_extension(path, DRAWING_EXTENSIONS) {
        PackageMedia::Unshown(UnshownKind::Drawing)
    } else if crate::util::is_video_extension(path) {
        PackageMedia::Unshown(UnshownKind::Video)
    } else if crate::util::has_extension(path, EMBEDDED_AUDIO_EXTENSIONS) {
        PackageMedia::Unshown(UnshownKind::Audio)
    } else {
        PackageMedia::Unshown(UnshownKind::Media)
    }
}

/// Write every entry under `prefix` that names a raster file into `out_dir`, counting the
/// rest into `skipped` and non-picture media into `unshown`. A nested path takes a
/// suffixed output name rather than overwriting its predecessor (only the base name survives).
fn write_media_parts<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    prefix: &str,
    out_dir: &Path,
    skipped: &mut SkippedImages,
    unshown: &mut Unshown,
) -> Vec<PathBuf> {
    let entries: Vec<String> = archive
        .file_names()
        // A media part names a file, i.e. carries an extension: directory entries and
        // extension-less names would otherwise be ingested as a nonexistent image.
        .filter(|name| name.starts_with(prefix) && Path::new(name).extension().is_some())
        .map(str::to_owned)
        .collect();
    let mut images = Vec::new();
    let mut next_suffix = HashMap::new();
    for entry in entries {
        // Only the entry's file name is used: a crafted `../..` entry must
        // never escape `out_dir`.
        let file_name = crate::util::neutralized_name(crate::util::file_name_or_path(&entry));
        if !crate::util::has_extension(
            Path::new(&file_name),
            crate::util::DELIVERABLE_IMAGE_EXTENSIONS,
        ) {
            match package_media(&file_name) {
                PackageMedia::Image => skipped.add(SkipReason::Unsupported),
                PackageMedia::Unshown(kind) => unshown.add(kind, 1),
            }
            continue;
        }
        let Some(content) = read_zip_entry(archive, &entry).bytes() else {
            tracing::warn!(%file_name, "document: failed to read embedded image");
            skipped.add(SkipReason::Failed);
            continue;
        };
        images.extend(write_picture(
            out_dir,
            &file_name,
            &content,
            &mut next_suffix,
            skipped,
        ));
    }
    images
}

/// Count the chart parts under `prefix` (`chart<N>.xml`, modern `chartEx<N>.xml`) less
/// the ones a chart sheet draws and the ones a reading described: a chart whose content
/// was printed where it sits is no longer a loss. An undescribed part is still counted.
fn count_chart_parts<R: Read + Seek>(
    archive: &ZipArchive<R>,
    prefix: &str,
    chart_sheets: Option<&HashSet<String>>,
    described: &HashSet<String>,
) -> usize {
    archive
        .file_names()
        .filter(|name| {
            is_chart_part(name, prefix)
                && !chart_sheets.is_some_and(|sheets| sheets.contains(*name))
                && !described.contains(*name)
        })
        .count()
}

/// Whether `name` is a chart part one predicate for both the census and the scan for a
/// chart's companion parts, so the two cannot disagree.
fn is_chart_part(name: &str, prefix: &str) -> bool {
    is_numbered_part(name, prefix, "chart") || is_numbered_part(name, prefix, "chartEx")
}

/// Whether `name` is a `<stem><N>.xml` part — numbered parts a reading names by count.
/// A non-numeric remainder (`charts/colors1.xml`) is another kind of part.
fn is_numbered_part(name: &str, prefix: &str, stem: &str) -> bool {
    name.strip_prefix(prefix)
        .and_then(|rest| rest.strip_suffix(".xml"))
        .and_then(|rest| rest.strip_prefix(stem))
        .is_some_and(|number| !number.is_empty() && number.bytes().all(|b| b.is_ascii_digit()))
}

/// Count the `<stem><N>.xml` parts under `prefix`.
fn count_numbered_parts<R: Read + Seek>(
    archive: &ZipArchive<R>,
    prefix: &str,
    stem: &str,
) -> usize {
    archive
        .file_names()
        .filter(|name| is_numbered_part(name, prefix, stem))
        .count()
}

/// The parts a package keeps in one of its directories (`embeddings/`) that are parts of
/// their own: a directory entry or a relationships part only places one.
fn parts_under<R: Read + Seek>(archive: &ZipArchive<R>, prefix: &str) -> Vec<String> {
    archive
        .file_names()
        .filter(|name| {
            name.starts_with(prefix) && !name.ends_with('/') && !name.contains("/_rels/")
        })
        .map(str::to_owned)
        .collect()
}

/// How many of a package's `embedded` parts a chart keeps beside itself, read from the
/// chart's own relationships (never supposed from a name). Only a part the census counted
/// is taken, so a relationship to a part the package lacks takes nothing out.
fn chart_data_parts<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    charts: &str,
    embedded: &[String],
) -> usize {
    let chart_parts: Vec<String> = archive
        .file_names()
        .filter(|name| is_chart_part(name, charts))
        .map(str::to_owned)
        .collect();
    let counted: HashSet<&str> = embedded.iter().map(String::as_str).collect();
    let mut data_parts: HashSet<String> = HashSet::new();
    for part in chart_parts {
        let rels = read_zip_entry(archive, &rels_part_of(&part))
            .bytes()
            .and_then(|xml| relationships(&xml))
            .unwrap_or_default();
        for rel in rels {
            let target = resolve_part(part_dir(&part), &rel.target);
            if counted.contains(target.as_str()) {
                data_parts.insert(target);
            }
        }
    }
    data_parts.len()
}

/// The parts of one package that no reading opens, counted for the report: charts no
/// chart sheet draws and no reading described, the diagrams whose data parts sit under
/// `diagrams/`, and the `embeddings/` parts that are no other part's companion. Each
/// family passes its own directories, so the three arms cannot count one kind twice.
fn unopened_parts<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    charts: &str,
    diagrams: Option<&str>,
    embeddings: &str,
    chart_sheets: Option<&HashSet<String>>,
    described: &HashSet<String>,
) -> Unshown {
    let mut unshown = Unshown::default();
    unshown.add(
        UnshownKind::Chart,
        count_chart_parts(archive, charts, chart_sheets, described),
    );
    if let Some(diagrams) = diagrams {
        unshown.add(
            UnshownKind::Diagram,
            count_numbered_parts(archive, diagrams, "data"),
        );
    }
    let embedded = parts_under(archive, embeddings);
    // A chart's own data workbook is the chart's, not a part of its own: what is left of the
    // counted parts is what the document embeds.
    let data_parts = chart_data_parts(archive, charts, &embedded);
    unshown.add(UnshownKind::Object, embedded.len() - data_parts);
    unshown
}

/// One OOXML relationship: the `Id`, its `Type` (empty when absent), and the `Target`.
struct Relationship {
    id: String,
    kind: String,
    target: String,
}

/// The relationships a rels part declares, in document order, and whether the part is
/// whole: the walk that reads the list also says whether it ended inside an element, so a
/// cut part reports the loss rather than reading a shortened list as everything.
fn relationships_and_whole(xml: &[u8]) -> (Option<Vec<Relationship>>, bool) {
    let mut part = Part::new(xml);
    let mut reader = Reader::from_reader(xml);
    let mut buffer = Vec::new();
    let mut entries = Vec::new();
    let mut document = false;
    loop {
        let Ok(event) = reader.read_event_into(&mut buffer) else {
            return (None, false);
        };
        part.note(&event);
        match event {
            Event::Start(event) | Event::Empty(event) => match event.local_name().as_ref() {
                b"Relationships" => document = true,
                b"Relationship" => entries.extend(relationship_entry(&event)),
                _ => {}
            },
            Event::Eof => break,
            _ => {}
        }
        buffer.clear();
    }
    (document.then_some(entries), !part.cut())
}

/// The relationships a rels part declares, in document order. `None` when the part
/// cannot be read as XML, or is no relationships document at all: a part that is
/// something else (an error page a producer left in its place) parses as XML just as
/// happily, and reading it as a list with no entries would pass it off as one that
/// names none of its parts.
fn relationships(xml: &[u8]) -> Option<Vec<Relationship>> {
    relationships_and_whole(xml).0
}

/// 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 text as one line: every run of whitespace becomes one space — what a
/// comment prints, whichever family's reader found it.
fn flatten(text: &str) -> String {
    text.split_whitespace().collect::<Vec<_>>().join(" ")
}

/// One `<Relationship Id Type Target/>` entry, or `None` when it names no id or target.
fn relationship_entry(event: &BytesStart<'_>) -> Option<Relationship> {
    let (Some(id), Some(target)) = (attr(event, b"Id"), attr(event, b"Target")) else {
        return None;
    };
    Some(Relationship {
        id,
        kind: attr(event, b"Type").unwrap_or_default(),
        target,
    })
}

/// Relationship id -> target. The readers read the list themselves — notes parts are named
/// there by kind — and share the mapping.
fn relationship_targets(rels: &[Relationship]) -> HashMap<String, String> {
    rels.iter()
        .map(|rel| (rel.id.clone(), rel.target.clone()))
        .collect()
}

/// Resolve a relationship target against the part that owns it: a relative target is
/// appended to `base`, an absolute one is the package path minus its leading slash, and
/// `..` segments are normalized away so the result names the ZIP entry.
fn resolve_part(base: &str, target: &str) -> String {
    let path = match target.strip_prefix('/') {
        Some(absolute) => absolute.to_owned(),
        None => format!("{base}{target}"),
    };
    let mut parts: Vec<&str> = Vec::new();
    for segment in path.split('/') {
        match segment {
            "" | "." => {}
            ".." => {
                parts.pop();
            }
            other => parts.push(other),
        }
    }
    parts.join("/")
}

/// The part the first relationship whose kind `matches` names, resolved against `base`,
/// or `conventional` when the relationships declare none.
fn related_part(
    rels: &[Relationship],
    base: &str,
    matches: impl Fn(&str) -> bool,
    conventional: &str,
) -> String {
    rels.iter().find(|rel| matches(&rel.kind)).map_or_else(
        || conventional.to_owned(),
        |rel| resolve_part(base, &rel.target),
    )
}

/// The directory a part's own relationship targets resolve against
/// (`ppt/slides/slide1.xml` -> `ppt/slides/`).
fn part_dir(part: &str) -> &str {
    part.rfind('/').map_or("", |slash| &part[..=slash])
}

/// The relationships part of `part`, by the OPC convention the document kit's
/// `relsPartFor` follows: wherever the package put the part, its relationships are beside it.
fn rels_part_of(part: &str) -> String {
    let name = part.rsplit_once('/').map_or(part, |(_, name)| name);
    format!("{}_rels/{name}.rels", part_dir(part))
}

/// The relationship-id attribute of an element that refers to a part (`r:id="rId1"`).
///
/// Matched by local name plus a namespace prefix: a slide carries both its own unprefixed
/// `id` and the relationship one, and only the prefix tells them apart.
fn rel_id(event: &BytesStart<'_>) -> Option<String> {
    event
        .attributes()
        .with_checks(false)
        .flatten()
        .find(|attribute| {
            attribute.key.local_name().as_ref() == b"id" && attribute.key.prefix().is_some()
        })
        .and_then(|attribute| {
            attribute
                .unescape_value()
                .ok()
                .map(std::borrow::Cow::into_owned)
        })
}

/// The value of the attribute whose local name is `name` (after any prefix), unescaped.
fn attr(event: &BytesStart<'_>, name: &[u8]) -> Option<String> {
    event
        .attributes()
        .with_checks(false)
        .flatten()
        .find(|attribute| attribute.key.local_name().as_ref() == name)
        .and_then(|attribute| {
            attribute
                .unescape_value()
                .ok()
                .map(std::borrow::Cow::into_owned)
        })
}

fn scan_elements<T>(
    xml: &[u8],
    element: &[u8],
    collect: impl FnMut(&BytesStart<'_>) -> Option<T>,
) -> Option<Vec<T>> {
    scan_names(xml, &[element], collect)
}

/// [`scan_elements`] over several local names in one pass: a part may state one kind of
/// entry under the name its schema version gives it.
fn scan_names<T>(
    xml: &[u8],
    names: &[&[u8]],
    mut collect: impl FnMut(&BytesStart<'_>) -> Option<T>,
) -> Option<Vec<T>> {
    let mut reader = Reader::from_reader(xml);
    let mut buffer = Vec::new();
    let mut found = Vec::new();
    loop {
        match reader.read_event_into(&mut buffer) {
            Ok(Event::Start(event) | Event::Empty(event))
                if names.contains(&event.local_name().as_ref()) =>
            {
                if let Some(value) = collect(&event) {
                    found.push(value);
                }
            }
            Ok(Event::Eof) => break,
            Ok(_) => {}
            Err(_) => return None,
        }
        buffer.clear();
    }
    Some(found)
}

/// Whether a part's bytes carry nothing but whitespace (a byte-order mark included): an
/// entry the package left empty, which declares nothing rather than holding something unread.
fn blank(bytes: &[u8]) -> bool {
    let bytes = bytes.strip_prefix(b"\xEF\xBB\xBF").unwrap_or(bytes);
    bytes.iter().all(u8::is_ascii_whitespace)
}

/// What a part's own walk says about the part. Every reader notes each event here, so the
/// part's account of itself — whole, cut at an element boundary, or no document at all —
/// comes out of the read that reads its content.
struct Part<'a> {
    /// The part's bytes: what tells an entry the package left empty from one whose bytes
    /// are no document.
    bytes: &'a [u8],
    /// Elements open right now: what a part cut at an element boundary leaves behind.
    open: usize,
    /// Whether the walk found an element of the kind the reader reads the part for.
    kind: bool,
}

impl<'a> Part<'a> {
    fn new(bytes: &'a [u8]) -> Self {
        Self {
            bytes,
            open: 0,
            kind: false,
        }
    }

    /// Note one event of the walk: only the part's own elements' start and end events
    /// build the account.
    fn note(&mut self, event: &Event<'_>) {
        match event {
            Event::Start(_) => self.open += 1,
            Event::End(_) => self.open = self.open.saturating_sub(1),
            _ => {}
        }
    }

    /// Note that the walk found an element of the kind the reader reads the part for,
    /// wherever it stands: what tells a part holding what this reader came for from one
    /// holding something else.
    fn kind_seen(&mut self) {
        self.kind = true;
    }

    /// Whether the part stopped with an element still open: quick-xml reports a part cut at an
    /// element boundary as a document that simply ends, so only the reader's own walk can tell
    /// a half-written part from a finished one.
    fn cut(&self) -> bool {
        self.open > 0
    }

    /// Whether the part is not the document it is read as: cut short, or holding no element
    /// of its own kind — an error page a producer left in its place parses as happily as the
    /// real thing. An entry the package left empty ([`blank`]) declares nothing instead.
    fn unreadable(&self) -> bool {
        self.cut() || (!self.kind && !blank(self.bytes))
    }
}

/// Resolve one XML entity reference (`&amp;`, `&#x41;`) in run text. An
/// unresolvable reference is dropped rather than aborting the whole body.
fn append_entity(text: &mut String, reference: &BytesRef<'_>) {
    if let Ok(name) = reference.decode()
        && let Some(resolved) = quick_xml::escape::resolve_predefined_entity(&name)
    {
        text.push_str(resolved);
        return;
    }
    if let Ok(Some(character)) = reference.resolve_char_ref() {
        text.push(character);
    }
}

/// A named ZIP entry as the reader found it: absent, present but unreadable within
/// [`MAX_ZIP_ENTRY_BYTES`], or its bytes.
enum ZipEntry {
    Missing,
    /// The entry is there but cannot be read whole within [`MAX_ZIP_ENTRY_BYTES`] — a
    /// read failure, a rejected size, or a refused name.
    Unreadable,
    Bytes(Vec<u8>),
}

impl ZipEntry {
    /// The entry's bytes when read, `None` otherwise — for callers that treat absent and
    /// unreadable alike; a caller that must tell them apart matches on the variant.
    fn bytes(self) -> Option<Vec<u8>> {
        match self {
            Self::Bytes(bytes) => Some(bytes),
            Self::Missing | Self::Unreadable => None,
        }
    }
}

/// Read a named ZIP entry into memory, bounded by [`MAX_ZIP_ENTRY_BYTES`], as
/// [`ZipEntry`]. Neither `Missing` nor `Unreadable` is fatal — the caller skips the entry
/// (noting why) or reports the package unreadable.
fn read_zip_entry<R: Read + Seek>(archive: &mut ZipArchive<R>, name: &str) -> ZipEntry {
    let mut entry = match archive.by_name(name) {
        Ok(entry) => entry,
        Err(zip::result::ZipError::FileNotFound) => return ZipEntry::Missing,
        Err(_) => return ZipEntry::Unreadable,
    };
    // The declared size is checked first, so a bomb that admits its size is rejected before
    // any inflation; the read then goes through the same cap so a lying header cannot exceed
    // it either.
    if entry.size() > MAX_ZIP_ENTRY_BYTES {
        tracing::warn!(
            %name,
            declared_bytes = entry.size(),
            "document: ZIP entry exceeds the decompression bound"
        );
        return ZipEntry::Unreadable;
    }
    let mut bytes = Vec::new();
    // Read one byte past the bound so an entry exactly at the limit is accepted whole while a
    // larger one is detected instead of silently truncated.
    if entry
        .by_ref()
        .take(MAX_ZIP_ENTRY_BYTES + 1)
        .read_to_end(&mut bytes)
        .is_err()
    {
        return ZipEntry::Unreadable;
    }
    if bytes.len() as u64 > MAX_ZIP_ENTRY_BYTES {
        tracing::warn!(%name, "document: ZIP entry exceeds the decompression bound");
        return ZipEntry::Unreadable;
    }
    ZipEntry::Bytes(bytes)
}

/// The reason a reading gives a recognized OOXML package it cannot open — the container
/// unreadable, or a needed part missing or unparsable. The `document` tool's refusal quotes
/// it verbatim, so a reading and a refusal cannot name the same failure differently.
pub(crate) fn corrupt_reason(family: Family) -> String {
    format!("corrupt or unsupported .{}", family.name())
}

/// The shared `recognized OOXML package of this kind that cannot be read` outcome.
fn unreadable(family: Family) -> DocOutcome {
    DocOutcome::Unreadable {
        reason: corrupt_reason(family),
    }
}

#[cfg(test)]
pub(crate) mod test_fixtures {
    use super::*;
    use std::io::Write;

    pub(crate) const DOCX_BODY: &[u8] = br#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>First paragraph</w:t></w:r></w:p><w:p><w:r><w:t>Second paragraph</w:t></w:r></w:p></w:body></w:document>"#;

    pub(crate) const COLUMN_CHART: &str = r#"<c:chartSpace xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><c:chart><c:title><c:tx><c:rich><a:p><a:r><a:t>Sales</a:t></a:r></a:p></c:rich></c:tx></c:title><c:plotArea><c:barChart><c:barDir val="col"/><c:ser><c:tx><c:strRef><c:f>Sheet1!$B$1</c:f><c:strCache><c:pt idx="0"><c:v>Revenue</c:v></c:pt></c:strCache></c:strRef></c:tx><c:cat><c:strCache><c:pt idx="0"><c:v>Jan</c:v></c:pt><c:pt idx="1"><c:v>Feb</c:v></c:pt></c:strCache></c:cat><c:val><c:numCache><c:pt idx="0"><c:v>10</c:v></c:pt><c:pt idx="1"><c:v>20</c:v></c:pt></c:numCache></c:val></c:ser></c:barChart></c:plotArea></c:chart></c:chartSpace>"#;

    pub(crate) fn zip_fixture(entries: &[(&str, &[u8])]) -> Vec<u8> {
        let mut writer = zip::ZipWriter::new(Cursor::new(Vec::new()));
        let options = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Deflated);
        for (path, contents) in entries {
            writer.start_file(*path, options).expect("start zip entry");
            writer.write_all(contents).expect("write zip entry");
        }
        writer.finish().expect("finish zip").into_inner()
    }
}

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

    #[test]
    fn pptx_reads_slides_with_notes_and_media() {
        let bytes = zip_fixture(&[
            (
                "ppt/presentation.xml",
                br#"<p:presentation><p:sldIdLst><p:sldId id="256" r:id="rId1"/><p:sldId id="257" r:id="rId2"/><p:sldId id="258" r:id="rId3"/></p:sldIdLst></p:presentation>"#,
            ),
            (
                "ppt/_rels/presentation.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="slides/slide2.xml"/><Relationship Id="rId2" Target="slides/slide1.xml"/><Relationship Id="rId3" Target="slides/slide3.xml"/></Relationships>"#,
            ),
            (
                "ppt/slides/slide2.xml",
                br"<p:sld><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>Second slide text</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>",
            ),
            (
                "ppt/slides/slide1.xml",
                br#"<p:sld><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>First slide </a:t></a:r><a:br/><a:r><a:t>continued</a:t></a:r></a:p><a:p><a:fld id="1" type="slidenum"><a:t>7</a:t></a:fld></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>"#,
            ),
            (
                "ppt/slides/slide3.xml",
                br#"<p:sld><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:fld id="2" type="datetime"><a:t>2026-01-01</a:t></a:fld></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>"#,
            ),
            // The notes relationship is not the first one, so resolving by position would pick the
            // image.
            (
                "ppt/slides/_rels/slide2.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image1.png"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide" Target="../notesSlides/notesSlide1.xml"/></Relationships>"#,
            ),
            (
                "ppt/notesSlides/notesSlide1.xml",
                br"<p:notes><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>Notes for the second slide</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:notes>",
            ),
            ("ppt/media/pic.png", b"\x89PNG\r\n\x1a\nfake image bytes"),
            ("ppt/charts/chart1.xml", b"<chart/>"),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text {
            text,
            images,
            notes,
            unshown,
            ..
        } = convert_pptx(&bytes, dir.path())
        else {
            panic!("expected Text outcome for a well-formed pptx");
        };
        assert_eq!(
            text,
            "Slide 1:\n  Second slide text\n\nSlide 1 notes:\n  Notes for the second slide\n\nSlide 2:\n  First slide\n  continued\n\nSlide 3: (no text)"
        );
        assert_eq!(images, vec![dir.path().join("pic.png")]);
        assert_eq!(unshown.lines(), ["1 chart(s) not shown"]);
        assert!(notes.is_empty(), "the report carries the chart loss");
    }

    #[test]
    fn pptx_reads_a_slide_outside_the_conventional_slides_directory() {
        let bytes = zip_fixture(&[
            (
                "ppt/presentation.xml",
                br#"<p:presentation><p:sldIdLst><p:sldId r:id="rId1"/></p:sldIdLst></p:presentation>"#,
            ),
            (
                "ppt/_rels/presentation.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="slides2/slide1.xml"/></Relationships>"#,
            ),
            (
                "ppt/slides2/slide1.xml",
                br#"<p:sld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>Out-of-tree text</a:t></a:r></a:p></p:txBody></p:sp><p:graphicFrame><a:graphic><a:graphicData><dgm:relIds r:dm="rId2"/></a:graphicData></a:graphic></p:graphicFrame></p:spTree></p:sld>"#,
            ),
            (
                "ppt/slides2/_rels/slide1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide" Target="../notesSlides/notesSlide1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData" Target="../diagrams/data1.xml"/></Relationships>"#,
            ),
            (
                "ppt/notesSlides/notesSlide1.xml",
                br"<p:notes><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>Notes for the out-of-tree slide</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:notes>",
            ),
            (
                "ppt/diagrams/data1.xml",
                br#"<dgm:dataModel><dgm:ptLst><dgm:pt type="node"><dgm:t><a:p><a:r><a:t>North</a:t></a:r></a:p></dgm:t></dgm:pt></dgm:ptLst></dgm:dataModel>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, notes, .. } = convert_pptx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a readable presentation");
        };
        assert_eq!(
            text,
            "Slide 1:\n  Out-of-tree text\n  (diagram text)\n    North\n\n\
             Slide 1 notes:\n  Notes for the out-of-tree slide"
        );
        assert!(notes.is_empty(), "no part may be reported lost: {notes:?}");
    }

    /// A hidden slide is marked, keeps its number, and still says it is hidden when it holds
    /// nothing; `showMasterSp` is not the slide's own show flag.
    #[test]
    fn pptx_marks_a_hidden_slide_and_keeps_its_number() {
        let bytes = zip_fixture(&[
            (
                "ppt/presentation.xml",
                br#"<p:presentation><p:sldIdLst><p:sldId r:id="rId1"/><p:sldId r:id="rId2"/><p:sldId r:id="rId3"/></p:sldIdLst></p:presentation>"#,
            ),
            (
                "ppt/_rels/presentation.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/><Relationship Id="rId2" Target="slides/slide2.xml"/><Relationship Id="rId3" Target="slides/slide3.xml"/></Relationships>"#,
            ),
            (
                "ppt/slides/slide1.xml",
                br#"<p:sld show="0"><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>Hidden</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>"#,
            ),
            (
                "ppt/slides/slide2.xml",
                br#"<p:sld showMasterSp="0"><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>Shown</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>"#,
            ),
            (
                "ppt/slides/slide3.xml",
                br#"<p:sld show="0"><p:spTree/></p:sld>"#,
            ),
        ]);
        let dir = tempfile::tempdir().expect("tempdir");
        let DocOutcome::Text { text, .. } = convert_pptx(&bytes, dir.path()) else {
            panic!("expected Text outcome for a readable presentation");
        };
        assert_eq!(
            text,
            "Slide 1: (hidden slide)\n  Hidden\n\n\
             Slide 2:\n  Shown\n\n\
             Slide 3: (hidden slide) (no text)"
        );
    }

    #[test]
    fn pptx_reports_the_slide_parts_it_cannot_read() {
        let presentation: &'static [u8] = br#"<p:presentation><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst></p:presentation>"#;
        let presentation_rels: &'static [u8] = br#"<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/></Relationships>"#;
        let slide: &'static [u8] = br"<p:sld><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>Slide text</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>";
        let notes_rels: &'static [u8] = br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide" Target="../notesSlides/notesSlide1.xml"/></Relationships>"#;
        let comments_rels: &'static [u8] = br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments/comment1.xml"/></Relationships>"#;
        // One slide, with its own relationships part and whatever parts the case says the package
        // holds beside it.
        let deck = |rels: &'static [u8], extra: &[(&'static str, &'static [u8])]| {
            let mut entries: Vec<(&'static str, &'static [u8])> = vec![
                ("ppt/presentation.xml", presentation),
                ("ppt/_rels/presentation.xml.rels", presentation_rels),
                ("ppt/slides/slide1.xml", slide),
                ("ppt/slides/_rels/slide1.xml.rels", rels),
            ];
            entries.extend_from_slice(extra);
            entries
        };
        // A deck declaring three slides, the second of them never declared in
        // the presentation's relationships.
        let three_slides: &'static [u8] = br#"<p:presentation><p:sldIdLst><p:sldId id="256" r:id="rId1"/><p:sldId id="257" r:id="rId2"/><p:sldId id="258" r:id="rId3"/></p:sldIdLst></p:presentation>"#;
        let three_slides_rels: &'static [u8] = br#"<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/><Relationship Id="rId3" Target="slides/slide2.xml"/></Relationships>"#;
        let first_slide: &'static [u8] = br"<p:sld><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>First</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>";
        let third_slide: &'static [u8] = br"<p:sld><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>Third</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>";
        let readable = "Slide 1:\n  Slide text";
        for (label, fixture, expected) in [
            (
                "a notes part the package does not hold",
                deck(notes_rels, &[]),
                (readable, ["slide 1 notes could not be read"].as_slice()),
            ),
            (
                "a relationships part cut mid-tag",
                deck(br#"<Relationships><Relationship Id="rId"#, &[]),
                (
                    readable,
                    ["slide 1 relationships could not be read"].as_slice(),
                ),
            ),
            (
                "a relationships part cut at an element boundary",
                deck(
                    br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide" Target="../notesSlides/notesSlide1.xml"/>"#,
                    &[],
                ),
                (
                    readable,
                    ["slide 1 relationships could not be read"].as_slice(),
                ),
            ),
            (
                "a comments part the package does not hold",
                deck(comments_rels, &[]),
                (readable, ["slide 1 comments could not be read"].as_slice()),
            ),
            (
                "a comments part holding an error page",
                deck(
                    comments_rels,
                    &[("ppt/comments/comment1.xml", b"<html>404</html>")],
                ),
                (readable, ["slide 1 comments could not be read"].as_slice()),
            ),
            (
                "a slide whose own relationship never resolves",
                vec![
                    ("ppt/presentation.xml", three_slides),
                    ("ppt/_rels/presentation.xml.rels", three_slides_rels),
                    ("ppt/slides/slide1.xml", first_slide),
                    ("ppt/slides/slide2.xml", third_slide),
                ],
                (
                    "Slide 1:\n  First\n\nSlide 3:\n  Third",
                    ["slide 2 could not be read"].as_slice(),
                ),
            ),
        ] {
            let (expected_text, expected_notes) = expected;
            let bytes = zip_fixture(&fixture);
            let dir = tempfile::tempdir().expect("tempdir");
            let DocOutcome::Text { text, notes, .. } = convert_pptx(&bytes, dir.path()) else {
                panic!("{label}: expected Text outcome for a readable presentation");
            };
            assert_eq!(text, expected_text, "{label}: the slides it could read");
            assert_eq!(
                notes, expected_notes,
                "{label}: the part it could not read is named once"
            );
        }
    }

    #[test]
    fn pptx_reads_comments_with_their_authors() {
        let legacy = zip_fixture(&[
            (
                "ppt/presentation.xml",
                br#"<p:presentation><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst></p:presentation>"#,
            ),
            (
                "ppt/_rels/presentation.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/commentAuthors" Target="commentAuthors.xml"/></Relationships>"#,
            ),
            (
                "ppt/commentAuthors.xml",
                br#"<p:cmAuthorLst><p:cmAuthor id="0" name="Anna Petrova" initials="AP"/><p:cmAuthor id="1" name="Ivan Petrov" initials="IP"/></p:cmAuthorLst>"#,
            ),
            (
                "ppt/slides/slide1.xml",
                br"<p:sld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>Slide text</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>",
            ),
            (
                "ppt/slides/_rels/slide1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments/comment1.xml"/></Relationships>"#,
            ),
            (
                "ppt/comments/comment1.xml",
                br#"<p:cmLst><p:cm authorId="0" idx="1" dt="2026-01-01T00:00:00"><p:pos x="1" y="2"/><p:text>needs a citation</p:text></p:cm><p:cm authorId="7" idx="2"><p:text>agreed</p:text></p:cm><p:cm authorId="1" idx="3"><p:text>  </p:text></p:cm><p:cm authorId="7" idx="4"><p:text/></p:cm></p:cmLst>"#,
            ),
        ]);
        let threaded = zip_fixture(&[
            (
                "ppt/presentation.xml",
                br#"<p:presentation><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst></p:presentation>"#,
            ),
            (
                "ppt/_rels/presentation.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/><Relationship Id="rId2" Type="http://schemas.microsoft.com/office/2018/10/relationships/authors" Target="authors.xml"/></Relationships>"#,
            ),
            (
                "ppt/authors.xml",
                br#"<p188:authorLst><p188:author id="{5C4A1C0A-0000-0000-0000-000000000001}" name="Anna Petrova" initials="AP" userId="anna@example.com" providerId="AD"/><p188:author id="{5C4A1C0A-0000-0000-0000-000000000002}" name="Ivan Petrov" initials="IP" userId="ivan@example.com" providerId="AD"/></p188:authorLst>"#,
            ),
            (
                "ppt/slides/slide1.xml",
                br"<p:sld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>Slide text</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>",
            ),
            (
                "ppt/slides/_rels/slide1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.microsoft.com/office/2018/10/relationships/comments" Target="../comments/modernComment_1.xml"/></Relationships>"#,
            ),
            (
                "ppt/comments/modernComment_1.xml",
                br#"<p188:cmLst><p188:cm authorId="{5C4A1C0A-0000-0000-0000-000000000001}" idx="1"><p188:txBody><a:p><a:r><a:t>Needs</a:t></a:r><a:br/><a:r><a:t>a citation</a:t></a:r><a:fld id="1" type="slidenum"><a:t>7</a:t></a:fld></a:p><a:p><a:r><a:t>for the figure</a:t></a:r></a:p></p188:txBody><p188:replyLst><p188:reply authorId="{5C4A1C0A-0000-0000-0000-000000000002}"><p188:txBody><a:p><a:r><a:t>Agreed</a:t></a:r></a:p></p188:txBody></p188:reply></p188:replyLst></p188:cm></p188:cmLst>"#,
            ),
        ]);
        for (label, fixture, expected) in [
            (
                "legacy comments",
                legacy,
                "Slide 1:\n  Slide text\n\n\
                 Slide 1 comments:\n  Anna Petrova: needs a citation\n  agreed\n  \
                 Ivan Petrov: (no text)\n  (no text)",
            ),
            (
                "2018 threaded comments",
                threaded,
                "Slide 1:\n  Slide text\n\n\
                 Slide 1 comments:\n  Anna Petrova: Needs a citation for the figure\n  Ivan Petrov: Agreed",
            ),
        ] {
            let dir = tempfile::tempdir().expect("tempdir");
            let DocOutcome::Text { text, notes, .. } = convert_pptx(&fixture, dir.path()) else {
                panic!("{label}: expected Text outcome for a readable presentation");
            };
            assert_eq!(
                text, expected,
                "{label}: the comments printed under the slide"
            );
            assert!(
                notes.is_empty(),
                "{label}: the comments are shown: {notes:?}"
            );
        }
    }

    fn own_title_placeholders() -> Vec<u8> {
        zip_fixture(&[
            (
                "ppt/presentation.xml",
                br#"<p:presentation><p:sldIdLst><p:sldId r:id="rId1"/><p:sldId r:id="rId2"/><p:sldId r:id="rId3"/><p:sldId r:id="rId4"/></p:sldIdLst></p:presentation>"#,
            ),
            (
                "ppt/_rels/presentation.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/><Relationship Id="rId2" Target="slides/slide2.xml"/><Relationship Id="rId3" Target="slides/slide3.xml"/><Relationship Id="rId4" Target="slides/slide4.xml"/></Relationships>"#,
            ),
            // An own `type="title"` marks the placeholder; a shape with no
            // `<p:ph>` (a heading written as a plain text box) is ordinary text.
            (
                "ppt/slides/slide1.xml",
                br#"<p:sld><p:spTree><p:sp><p:nvSpPr><p:nvPr><p:ph type="title"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>Quarterly review</a:t></a:r></a:p></p:txBody></p:sp><p:sp><p:txBody><a:p><a:r><a:t>Heading as a text box</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>"#,
            ),
            // `ctrTitle` is a title too.
            (
                "ppt/slides/slide2.xml",
                br#"<p:sld><p:spTree><p:sp><p:nvSpPr><p:nvPr><p:ph type="ctrTitle"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>Centred title</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>"#,
            ),
            // An empty title placeholder adds no mark; the body still reads.
            (
                "ppt/slides/slide3.xml",
                br#"<p:sld><p:spTree><p:sp><p:nvSpPr><p:nvPr><p:ph type="title"/></p:nvPr></p:nvSpPr><p:txBody><a:p/></p:txBody></p:sp><p:sp><p:txBody><a:p><a:r><a:t>Only body</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>"#,
            ),
            // A slide that holds only its title must not read `(no text)`.
            (
                "ppt/slides/slide4.xml",
                br#"<p:sld><p:spTree><p:sp><p:nvSpPr><p:nvPr><p:ph type="title"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>Title only</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>"#,
            ),
        ])
    }

    /// A deck whose five slides inherit their titles from the layout parts their
    /// own relationships name.
    fn layout_inherited_titles() -> Vec<u8> {
        zip_fixture(&[
            (
                "ppt/presentation.xml",
                br#"<p:presentation><p:sldIdLst><p:sldId r:id="rId1"/><p:sldId r:id="rId2"/><p:sldId r:id="rId3"/><p:sldId r:id="rId4"/><p:sldId r:id="rId5"/></p:sldIdLst></p:presentation>"#,
            ),
            (
                "ppt/_rels/presentation.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/><Relationship Id="rId2" Target="slides/slide2.xml"/><Relationship Id="rId3" Target="slides/slide3.xml"/><Relationship Id="rId4" Target="slides/slide4.xml"/><Relationship Id="rId5" Target="slides/slide5.xml"/></Relationships>"#,
            ),
            (
                "ppt/slides/slide1.xml",
                br#"<p:sld><p:spTree><p:sp><p:nvSpPr><p:nvPr><p:ph idx="10"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>Inherited title</a:t></a:r></a:p></p:txBody></p:sp><p:sp><p:nvSpPr><p:nvPr><p:ph idx="11"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>Body</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>"#,
            ),
            // The same `idx` under a layout that declares its title elsewhere.
            (
                "ppt/slides/slide2.xml",
                br#"<p:sld><p:spTree><p:sp><p:nvSpPr><p:nvPr><p:ph idx="10"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>Not a title here</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>"#,
            ),
            // A second slide of the layout the first one uses.
            (
                "ppt/slides/slide3.xml",
                br#"<p:sld><p:spTree><p:sp><p:nvSpPr><p:nvPr><p:ph idx="10"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>Shared layout title</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>"#,
            ),
            // A placeholder naming neither type nor `idx`: the schema defaults
            // it to the layout's `idx` 0, which that layout declares a title.
            (
                "ppt/slides/slide4.xml",
                br"<p:sld><p:spTree><p:sp><p:nvSpPr><p:nvPr><p:ph/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>Defaulted title</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>",
            ),
            // ...but the same layout's `idx` 1 is its body, whatever the type
            // defaults to.
            (
                "ppt/slides/slide5.xml",
                br#"<p:sld><p:spTree><p:sp><p:nvSpPr><p:nvPr><p:ph type="obj" idx="1"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>Defaulted body</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>"#,
            ),
            (
                "ppt/slides/_rels/slide1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout1.xml"/></Relationships>"#,
            ),
            (
                "ppt/slides/_rels/slide2.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout2.xml"/></Relationships>"#,
            ),
            (
                "ppt/slides/_rels/slide3.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout1.xml"/></Relationships>"#,
            ),
            (
                "ppt/slides/_rels/slide4.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout3.xml"/></Relationships>"#,
            ),
            (
                "ppt/slides/_rels/slide5.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout3.xml"/></Relationships>"#,
            ),
            (
                "ppt/slideLayouts/slideLayout1.xml",
                br#"<p:sldLayout><p:spTree><p:sp><p:nvSpPr><p:nvPr><p:ph type="title" idx="10"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>LAYOUT TITLE</a:t></a:r></a:p></p:txBody></p:sp><p:sp><p:nvSpPr><p:nvPr><p:ph type="body" idx="11"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>LAYOUT BODY</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sldLayout>"#,
            ),
            (
                "ppt/slideLayouts/slideLayout2.xml",
                br#"<p:sldLayout><p:spTree><p:sp><p:nvSpPr><p:nvPr><p:ph type="title" idx="20"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>OTHER LAYOUT TITLE</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sldLayout>"#,
            ),
            // Its title carries no `idx`, which the schema defaults to 0.
            (
                "ppt/slideLayouts/slideLayout3.xml",
                br#"<p:sldLayout><p:spTree><p:sp><p:nvSpPr><p:nvPr><p:ph type="title"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>DEFAULTED LAYOUT TITLE</a:t></a:r></a:p></p:txBody></p:sp><p:sp><p:nvSpPr><p:nvPr><p:ph type="body" idx="1"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>LAYOUT BODY</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sldLayout>"#,
            ),
        ])
    }

    #[test]
    fn pptx_marks_a_title_from_the_slide_or_its_layout() {
        for (label, fixture, expected) in [
            (
                "titles the slide itself declares",
                own_title_placeholders(),
                "Slide 1:\n  (title) Quarterly review\n  Heading as a text box\n\n\
                 Slide 2:\n  (title) Centred title\n\n\
                 Slide 3:\n  Only body\n\n\
                 Slide 4:\n  (title) Title only",
            ),
            (
                "titles inherited from the slide's layout",
                layout_inherited_titles(),
                "Slide 1:\n  (title) Inherited title\n  Body\n\n\
                 Slide 2:\n  Not a title here\n\n\
                 Slide 3:\n  (title) Shared layout title\n\n\
                 Slide 4:\n  (title) Defaulted title\n\n\
                 Slide 5:\n  Defaulted body",
            ),
        ] {
            let dir = tempfile::tempdir().expect("tempdir");
            let DocOutcome::Text { text, .. } = convert_pptx(&fixture, dir.path()) else {
                panic!("{label}: expected Text outcome for a readable presentation");
            };
            assert_eq!(text, expected, "{label}: the title marks printed");
        }
    }

    #[test]
    fn pptx_reads_diagram_text_from_the_data_model() {
        let points = zip_fixture(&[
            (
                "ppt/presentation.xml",
                br#"<p:presentation><p:sldIdLst><p:sldId r:id="rId1"/><p:sldId r:id="rId2"/></p:sldIdLst></p:presentation>"#,
            ),
            (
                "ppt/_rels/presentation.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/><Relationship Id="rId2" Target="slides/slide2.xml"/></Relationships>"#,
            ),
            (
                "ppt/slides/slide1.xml",
                br#"<p:sld><p:spTree><p:graphicFrame><a:graphic><a:graphicData><dgm:relIds r:dm="rId1"/></a:graphicData></a:graphic></p:graphicFrame></p:spTree></p:sld>"#,
            ),
            (
                "ppt/slides/_rels/slide1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData" Target="../diagrams/data1.xml"/></Relationships>"#,
            ),
            (
                "ppt/diagrams/data1.xml",
                br#"<dgm:dataModel><dgm:ptLst><dgm:pt type="node"><dgm:prSet phldrT="[Text]"/><dgm:t><a:p><a:r><a:t>North</a:t></a:r></a:p><a:p><a:fld id="1" type="slidenum"><a:t>7</a:t></a:fld></a:p></dgm:t></dgm:pt><dgm:pt type="node"><dgm:prSet phldr="1" phldrT="[Text]"/><dgm:t><a:p><a:r><a:t>Placeholder</a:t></a:r></a:p></dgm:t></dgm:pt><dgm:pt type="asst"><dgm:t><a:p><a:r><a:t>Assistant</a:t></a:r></a:p></dgm:t></dgm:pt><dgm:pt><dgm:t><a:p><a:r><a:t>South</a:t></a:r></a:p></dgm:t></dgm:pt></dgm:ptLst></dgm:dataModel>"#,
            ),
            // A slide whose only diagram is read but shows no text still holds
            // its diagram: it must not read as a slide with no text at all.
            (
                "ppt/slides/slide2.xml",
                br#"<p:sld><p:spTree><p:graphicFrame><a:graphic><a:graphicData><dgm:relIds r:dm="rId1"/></a:graphicData></a:graphic></p:graphicFrame></p:spTree></p:sld>"#,
            ),
            (
                "ppt/slides/_rels/slide2.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData" Target="../diagrams/data2.xml"/></Relationships>"#,
            ),
            (
                "ppt/diagrams/data2.xml",
                br#"<dgm:dataModel><dgm:ptLst><dgm:pt type="doc"><dgm:prSet phldr="1"/><dgm:t><a:p><a:r><a:t>Hint</a:t></a:r></a:p></dgm:t></dgm:pt></dgm:ptLst></dgm:dataModel>"#,
            ),
        ]);
        let once_per_slide = zip_fixture(&[
            (
                "ppt/presentation.xml",
                br#"<p:presentation><p:sldIdLst><p:sldId r:id="rId1"/><p:sldId r:id="rId2"/></p:sldIdLst></p:presentation>"#,
            ),
            (
                "ppt/_rels/presentation.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/><Relationship Id="rId2" Target="slides/slide2.xml"/></Relationships>"#,
            ),
            // `rId1` twice, in shapes either side of `rId2`: two sections, not
            // three, and in shape order.
            (
                "ppt/slides/slide1.xml",
                br#"<p:sld><p:spTree><p:graphicFrame><a:graphic><a:graphicData><dgm:relIds r:dm="rId1"/></a:graphicData></a:graphic></p:graphicFrame><p:graphicFrame><a:graphic><a:graphicData><dgm:relIds r:dm="rId2"/></a:graphicData></a:graphic></p:graphicFrame><p:graphicFrame><a:graphic><a:graphicData><dgm:relIds r:dm="rId1"/></a:graphicData></a:graphic></p:graphicFrame></p:spTree></p:sld>"#,
            ),
            // The same data part reads again on its own slide.
            (
                "ppt/slides/slide2.xml",
                br#"<p:sld><p:spTree><p:graphicFrame><a:graphic><a:graphicData><dgm:relIds r:dm="rId1"/></a:graphicData></a:graphic></p:graphicFrame></p:spTree></p:sld>"#,
            ),
            (
                "ppt/slides/_rels/slide1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData" Target="../diagrams/data1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData" Target="../diagrams/data2.xml"/></Relationships>"#,
            ),
            (
                "ppt/slides/_rels/slide2.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData" Target="../diagrams/data1.xml"/></Relationships>"#,
            ),
            (
                "ppt/diagrams/data1.xml",
                br#"<dgm:dataModel><dgm:ptLst><dgm:pt type="node"><dgm:t><a:p><a:r><a:t>North</a:t></a:r></a:p></dgm:t></dgm:pt><dgm:pt type="node"><dgm:t><a:p><a:r><a:t>South</a:t></a:r></a:p></dgm:t></dgm:pt></dgm:ptLst></dgm:dataModel>"#,
            ),
            (
                "ppt/diagrams/data2.xml",
                br#"<dgm:dataModel><dgm:ptLst><dgm:pt type="node"><dgm:t><a:p><a:r><a:t>Alpha</a:t></a:r></a:p></dgm:t></dgm:pt></dgm:ptLst></dgm:dataModel>"#,
            ),
        ]);
        for (label, fixture, expected) in [
            (
                "points, their types and the placeholders among them",
                points,
                "Slide 1:\n  (diagram text)\n    North\n    Assistant\n    South\n\n\
                 Slide 2:\n  (diagram text)",
            ),
            (
                "each diagram once per slide, in shape order",
                once_per_slide,
                "Slide 1:\n  (diagram text)\n    North\n    South\n  (diagram text)\n    Alpha\n\n\
                 Slide 2:\n  (diagram text)\n    North\n    South",
            ),
        ] {
            let dir = tempfile::tempdir().expect("tempdir");
            let DocOutcome::Text { text, .. } = convert_pptx(&fixture, dir.path()) else {
                panic!("{label}: expected Text outcome for a readable presentation");
            };
            assert_eq!(text, expected, "{label}: the diagram text printed");
        }
    }

    #[test]
    fn pptx_diagram_text_is_the_text_a_reading_showed_per_slide() {
        let bytes = zip_fixture(&[
            (
                "ppt/presentation.xml",
                br#"<p:presentation><p:sldIdLst><p:sldId r:id="rId1"/><p:sldId r:id="rId2"/><p:sldId r:id="rId3"/><p:sldId r:id="rId4"/></p:sldIdLst></p:presentation>"#,
            ),
            (
                "ppt/_rels/presentation.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/><Relationship Id="rId2" Target="slides/slide2.xml"/><Relationship Id="rId3" Target="slides/slide3.xml"/><Relationship Id="rId4" Target="slides/slide4.xml"/></Relationships>"#,
            ),
            (
                "ppt/slides/slide1.xml",
                br#"<p:sld><p:spTree><p:graphicFrame><a:graphic><a:graphicData><dgm:relIds r:dm="rId1"/></a:graphicData></a:graphic></p:graphicFrame><p:graphicFrame><a:graphic><a:graphicData><dgm:relIds r:dm="rId2"/></a:graphicData></a:graphic></p:graphicFrame></p:spTree></p:sld>"#,
            ),
            (
                "ppt/slides/_rels/slide1.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData" Target="../diagrams/data1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData" Target="../diagrams/data2.xml"/></Relationships>"#,
            ),
            // A placeholder point is skipped and a hint alone is not: the same
            // rule the reading marks its diagram text by.
            (
                "ppt/diagrams/data1.xml",
                br#"<dgm:dataModel><dgm:ptLst><dgm:pt><dgm:t><a:p><a:r><a:t>North</a:t></a:r></a:p><a:p><a:r><a:t>Second line</a:t></a:r></a:p></dgm:t></dgm:pt><dgm:pt><dgm:prSet phldr="1"/><dgm:t><a:p><a:r><a:t>Placeholder</a:t></a:r></a:p></dgm:t></dgm:pt><dgm:pt><dgm:prSet phldrT="[Text]"/><dgm:t><a:p><a:r><a:t>Hinted</a:t></a:r></a:p></dgm:t></dgm:pt></dgm:ptLst></dgm:dataModel>"#,
            ),
            (
                "ppt/diagrams/data2.xml",
                br"<dgm:dataModel><dgm:ptLst><dgm:pt><dgm:t><a:p><a:r><a:t>South</a:t></a:r></a:p></dgm:t></dgm:pt></dgm:ptLst></dgm:dataModel>",
            ),
            // A diagram the package cut short: its text is lost, so the slide is
            // absent rather than holding text a refusal could quote.
            (
                "ppt/slides/slide2.xml",
                br#"<p:sld><p:spTree><p:graphicFrame><a:graphic><a:graphicData><dgm:relIds r:dm="rId1"/></a:graphicData></a:graphic></p:graphicFrame></p:spTree></p:sld>"#,
            ),
            (
                "ppt/slides/_rels/slide2.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData" Target="../diagrams/data3.xml"/></Relationships>"#,
            ),
            (
                "ppt/diagrams/data3.xml",
                br"<dgm:dataModel><dgm:ptLst><dgm:pt><dgm:t><a:p><a:r><a:t>Cut off</a:t>",
            ),
            // A slide with no diagram at all, and one whose diagram is only
            // placeholders, both contribute nothing.
            (
                "ppt/slides/slide3.xml",
                br"<p:sld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>Plain</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:sld>",
            ),
            (
                "ppt/slides/slide4.xml",
                br#"<p:sld><p:spTree><p:graphicFrame><a:graphic><a:graphicData><dgm:relIds r:dm="rId1"/></a:graphicData></a:graphic></p:graphicFrame></p:spTree></p:sld>"#,
            ),
            (
                "ppt/slides/_rels/slide4.xml.rels",
                br#"<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData" Target="../diagrams/data4.xml"/></Relationships>"#,
            ),
            (
                "ppt/diagrams/data4.xml",
                br#"<dgm:dataModel><dgm:ptLst><dgm:pt><dgm:prSet phldr="1"/><dgm:t><a:p><a:r><a:t>Placeholder</a:t></a:r></a:p></dgm:t></dgm:pt></dgm:ptLst></dgm:dataModel>"#,
            ),
        ]);
        let text = pptx_diagram_text(&bytes);
        assert_eq!(text.len(), 1, "only slide 1 shows text: {text:?}");
        assert_eq!(
            text.get("ppt/slides/slide1.xml"),
            Some(&vec![
                "North\nSecond line\nHinted".to_owned(),
                "South".to_owned()
            ]),
            "one entry per diagram that shows text, in shape order: {text:?}"
        );

        // A package that is not a presentation holds no diagram text, and one
        // whose bytes are not a package at all neither.
        assert!(pptx_diagram_text(b"not a package").is_empty());
        assert!(pptx_diagram_text(&zip_fixture(&[("word/document.xml", DOCX_BODY)])).is_empty());
    }
}