mahbot 0.8.0

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
//! The content of a chart, as text, printed where the chart sits.
//!
//! A chart part carries the values its last rendering drew beside the table it
//! points at: a classic chart (`c:chartSpace`) caches each series' name, its
//! category labels and its numbers in `c:strCache`/`c:numCache`/`c:numLit`, with
//! the number format the value was shown under. This module reads that content —
//! the chart's kind, its title and other text, and one line per series of category
//! and value — so a reading tells a model what a chart says instead of only that
//! one is there. Nothing is drawn, and nothing beside the chart part is opened.
//!
//! # What is not described
//!
//! A chart part that carries only references to its attached table (no caches)
//! says per series that its values are in that table, and a part that cannot be
//! read says so: neither is passed off as a chart with no content, and both keep
//! their place in the package's report ([`crate::reader_output::Unshown`]). The
//! modern extended parts (`chartEx`) have a schema of their own — `cx:` rather
//! than `c:` — which this reading does not parse, so they are that same case.
//!
//! A chart whose values are printed is no longer a loss the report names; the
//! readers exclude every part this module describes from their census. The parts
//! read are the ones that census names — `charts/chart<N>.xml` and the
//! `chartEx<N>.xml` beside them — so a part a producer files under another name is
//! outside the reading and its report alike, as it was before this module.
//!
//! # Invariants
//!
//! - **A value is shown the way the reading shows a spreadsheet cell**: a cached
//!   point keeps the format its cache states, through the same formatter
//!   ([`numfmt`]), and a number under a format that cannot be reproduced is shown
//!   as stored, under the same mark ([`MARK_STORED`]), never invented.
//! - **Bounded rather than document-decided**: the walk keeps at most
//!   [`MAX_SERIES`] series and [`MAX_POINTS`] points of each list — the rest is
//!   counted, not allocated or formatted — and prints at most [`MAX_TEXTS`] of
//!   the chart's other texts, saying how much more it holds rather than cutting
//!   it off silently. At most [`MAX_CHARTS`] of a package's parts are read, and
//!   the ones left out stay in the report.
//! - **The notation the model reads is documented in `src/prompt/tool/read.md`**:
//!   the block's header, its field labels, the marks standing in for values and
//!   the annotations a value carries are stated once in [`crate::reader_output`],
//!   which that description and this module both take them from, and the counts
//!   standing in for what the bounds above leave out are what the description
//!   calls an excess counted.

use super::{
    Relationship, ZipEntry, append_entity, attr, is_chart_part, numfmt, read_zip_entry, rel_id,
    resolve_part, scan_elements,
};
use crate::reader_output::{
    CHART_EXTENDED, CHART_LABEL, CHART_NO_VALUES, CHART_TEXT, CHART_TITLE, CHART_UNREADABLE,
    CHART_VALUES_ATTACHED, MARK_STORED, chart_excess, chart_header, chart_series, chart_size,
};
use crate::util::one_line;
use quick_xml::Reader;
use quick_xml::events::{BytesRef, BytesStart, Event};
use std::collections::{BTreeMap, HashMap};
use std::fmt::Write as _;
use std::io::{Read, Seek};
use zip::ZipArchive;

// ── Bounds ──────────────────────────────────────────────────────

/// How many chart parts of one package are read. A package that holds more keeps
/// the rest as a loss its census still names, so the number of parts a crafted
/// package carries cannot decide the reading's cost.
const MAX_CHARTS: usize = 64;
/// How many series of one chart are read and printed.
const MAX_SERIES: usize = 8;
/// How many points of one cached list are read and printed.
const MAX_POINTS: usize = 25;
/// How many of a chart's other texts — an axis title and a data label alike — are
/// printed, the two lists together.
const MAX_TEXTS: usize = 6;
/// How deep a part's elements may nest before it is read as unreadable: the walk
/// keeps one stack entry per level, so a crafted part must not decide its size.
const MAX_DEPTH: usize = 128;

// ── What a reading prints ───────────────────────────────────────

/// One chart part's content, as a reading prints it where the chart sits.
#[derive(Debug)]
pub(crate) enum ChartRead {
    /// The part is a chart this reading read: its lines say what it carries —
    /// its title, its other text and one line per series, ready to print.
    Content {
        /// The chart's kind, as the part states it (`column chart`, `pie chart`,
        /// `stacked line chart`), or `None` when the part names none.
        kind: Option<String>,
        /// The `Title: …`, `Text: …`, `Label: …` and `Series …` lines,
        /// unindented.
        body: Vec<String>,
        /// Whether the part carried the values its lines show. When it did not,
        /// each series' line says where its values are instead of printing
        /// them, and the chart stays in the report because what it says did not
        /// reach the answer.
        values: bool,
    },
    /// The part cannot be read, or is a kind whose own schema this reading does
    /// not parse.
    Unreadable(ChartLoss),
}

/// Why a chart part's content is not described.
#[derive(Debug, Clone, Copy)]
pub(crate) enum ChartLoss {
    /// The part is unreadable, cut short, nested past [`MAX_DEPTH`] or carries no
    /// chart element.
    Part,
    /// The part is a modern extended chart, whose `cx:` schema this reading does
    /// not parse.
    Extended,
}

impl ChartLoss {
    /// The mark this loss prints in place of the chart's content.
    const fn mark(self) -> &'static str {
        match self {
            Self::Part => CHART_UNREADABLE,
            Self::Extended => CHART_EXTENDED,
        }
    }
}

impl ChartRead {
    /// Whether the reading printed this chart's values — and so whether the chart
    /// is no longer a loss the package's report names.
    pub(crate) fn described(&self) -> bool {
        matches!(self, Self::Content { values: true, .. })
    }

    /// The chart's lines: a `Chart (<kind>):` header over its content indented by
    /// two spaces — or the one line naming why there is no content.
    pub(crate) fn lines(&self) -> Vec<String> {
        match self {
            Self::Content { kind, body, .. } => {
                let mut lines = vec![chart_header(kind.as_deref())];
                lines.extend(body.iter().map(|line| format!("  {line}")));
                lines
            }
            Self::Unreadable(loss) => vec![format!("{} {}", chart_header(None), loss.mark())],
        }
    }
}

// ── Reading the package's chart parts ───────────────────────────

/// Every chart part under `prefix` — the same parts the package's census counts —
/// read into the content a reading prints where the chart sits. `date1904` is the
/// workbook's date system, used for a part that does not state its own.
pub(crate) fn read_charts<R: Read + Seek>(
    archive: &mut ZipArchive<R>,
    prefix: &str,
    date1904: bool,
) -> HashMap<String, ChartRead> {
    let parts: Vec<String> = archive
        .file_names()
        .filter(|name| is_chart_part(name, prefix))
        .take(MAX_CHARTS)
        .map(str::to_owned)
        .collect();
    parts
        .into_iter()
        .map(|part| {
            let read = match read_zip_entry(archive, &part) {
                ZipEntry::Bytes(xml) => read_part(&xml, date1904),
                ZipEntry::Missing | ZipEntry::Unreadable => ChartRead::Unreadable(ChartLoss::Part),
            };
            (part, read)
        })
        .collect()
}

/// The chart parts `xml` names through `rels`, resolved against the part's own
/// directory (`base`) — in the order the part draws them, and never the same part
/// twice. The reference is the `chart` element of a slide, a worksheet's drawing
/// or a Word body, whichever namespace wraps it.
pub(crate) fn chart_parts(xml: &[u8], rels: &[Relationship], base: &str) -> Vec<String> {
    let mut parts = Vec::new();
    for id in scan_elements(xml, b"chart", rel_id).unwrap_or_default() {
        let Some(rel) = rels.iter().find(|rel| rel.id == id) else {
            continue;
        };
        let part = resolve_part(base, &rel.target);
        if !parts.contains(&part) {
            parts.push(part);
        }
    }
    parts
}

/// The content of every chart among `parts` the package holds — printed lines,
/// ready to place where the charts sit — and the parts whose content those lines
/// are, which the package's census leaves out. A caller that does not deliver its
/// block (a slide or a sheet it could not read) records none of them, so no chart
/// leaves both the reading and the report.
pub(crate) fn lines(
    charts: &HashMap<String, ChartRead>,
    parts: &[String],
) -> (Vec<String>, Vec<String>) {
    let mut lines = Vec::new();
    let mut described = Vec::new();
    for part in parts {
        let Some(read) = charts.get(part) else {
            continue;
        };
        if read.described() {
            described.push(part.clone());
        }
        lines.extend(read.lines());
    }
    (lines, described)
}

// ── The part's own walk ─────────────────────────────────────────

/// Read one chart part's content. A part that is not a classic chart — an
/// extended one, or one with no chart space at all — is [`ChartRead::Unreadable`],
/// never a chart with no content.
fn read_part(xml: &[u8], date1904: bool) -> ChartRead {
    let mut parser = Parser::new(date1904);
    match parser.run(xml) {
        Ok(()) => parser.finish(),
        Err(loss) => ChartRead::Unreadable(loss),
    }
}

/// The cached points of one list: a series' values, one category level, or a
/// bubble's sizes.
#[derive(Default)]
struct Cache {
    /// Whether the points are numbers a format applies to (`numCache`/`numLit`).
    numeric: bool,
    /// The number format the cache states, when it states one.
    format: Option<String>,
    /// The points, by the index the part gives each.
    points: BTreeMap<usize, String>,
    /// How many points the cache held past [`MAX_POINTS`]: counted, never kept.
    more: usize,
}

/// One series while its element is open.
#[derive(Default)]
struct Series {
    /// The series' name, when the part caches or states one.
    name: Option<String>,
    /// Its category levels: one cache, or one per level of a multi-level list.
    categories: Vec<Cache>,
    /// Its values.
    values: Vec<Cache>,
    /// A bubble chart's sizes, aligned with the values by index.
    sizes: Vec<Cache>,
}

impl Series {
    fn slot(&mut self, slot: Slot) -> &mut Vec<Cache> {
        match slot {
            Slot::Category => &mut self.categories,
            Slot::Value => &mut self.values,
            Slot::Size => &mut self.sizes,
        }
    }
}

/// What a cached list being read belongs to.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Slot {
    Category,
    Value,
    Size,
}

/// A run of text being collected: the chart's title, an axis title or a data
/// label.
struct Capture {
    /// The stack depth the element was pushed at, so its end is recognised.
    depth: usize,
    kind: CaptureKind,
    text: String,
}

/// What a collected run of text is.
#[derive(Clone, Copy, PartialEq, Eq)]
enum CaptureKind {
    /// The chart's own title.
    Title,
    /// An axis title — the chart's own text, printed with the title.
    Axis,
    /// A data label's own text.
    Label,
}

/// The chart type element being read and the qualifiers its own children state.
struct KindBuilder {
    depth: usize,
    kind: Kind,
    /// A bar chart's direction (`bar` is horizontal, `col` vertical).
    bar_dir: Option<String>,
    /// A bar, line or area chart's grouping (`stacked`, `percentStacked`).
    grouping: Option<String>,
    /// A pie-of-pie chart's second form (`pie` or `bar`).
    of_pie: Option<String>,
}

impl KindBuilder {
    /// The kind as a reading names it: the grouping leads, then the chart's form.
    fn name(&self) -> String {
        let grouping = match self.grouping.as_deref() {
            Some("stacked") => "stacked ",
            Some("percentStacked") => "100% stacked ",
            _ => "",
        };
        match self.kind {
            Kind::Area => format!("{grouping}area chart"),
            Kind::Area3D => format!("{grouping}3-D area chart"),
            Kind::Bar => {
                let bars = if self.bar_dir.as_deref() == Some("bar") {
                    "bar"
                } else {
                    "column"
                };
                format!("{grouping}{bars} chart")
            }
            Kind::Bar3D => {
                let bars = if self.bar_dir.as_deref() == Some("bar") {
                    "bar"
                } else {
                    "column"
                };
                format!("{grouping}3-D {bars} chart")
            }
            Kind::Bubble => "bubble chart".to_owned(),
            Kind::Doughnut => "doughnut chart".to_owned(),
            Kind::Line => format!("{grouping}line chart"),
            Kind::Line3D => format!("{grouping}3-D line chart"),
            Kind::OfPie if self.of_pie.as_deref() == Some("bar") => "bar-of-pie chart".to_owned(),
            Kind::OfPie => "pie-of-pie chart".to_owned(),
            Kind::Pie => "pie chart".to_owned(),
            Kind::Pie3D => "3-D pie chart".to_owned(),
            Kind::Radar => "radar chart".to_owned(),
            Kind::Scatter => "scatter chart".to_owned(),
            Kind::Stock => "stock chart".to_owned(),
            Kind::Surface => "surface chart".to_owned(),
            Kind::Surface3D => "3-D surface chart".to_owned(),
        }
    }
}

/// The classic chart kinds, each named by the element that declares it.
#[derive(Clone, Copy)]
enum Kind {
    Area,
    Area3D,
    Bar,
    Bar3D,
    Bubble,
    Doughnut,
    Line,
    Line3D,
    OfPie,
    Pie,
    Pie3D,
    Radar,
    Scatter,
    Stock,
    Surface,
    Surface3D,
}

/// [`Kind`] for a plot area element's local name.
fn kind_of(name: &[u8]) -> Option<Kind> {
    Some(match name {
        b"areaChart" => Kind::Area,
        b"area3DChart" => Kind::Area3D,
        b"barChart" => Kind::Bar,
        b"bar3DChart" => Kind::Bar3D,
        b"bubbleChart" => Kind::Bubble,
        b"doughnutChart" => Kind::Doughnut,
        b"lineChart" => Kind::Line,
        b"line3DChart" => Kind::Line3D,
        b"ofPieChart" => Kind::OfPie,
        b"pieChart" => Kind::Pie,
        b"pie3DChart" => Kind::Pie3D,
        b"radarChart" => Kind::Radar,
        b"scatterChart" => Kind::Scatter,
        b"stockChart" => Kind::Stock,
        b"surfaceChart" => Kind::Surface,
        b"surface3DChart" => Kind::Surface3D,
        _ => return None,
    })
}

/// The state of one chart part's walk.
struct Parser {
    /// The workbook's date system, for a part that does not state its own.
    date1904: bool,
    /// The part's own date system, when it states one.
    part_date1904: Option<bool>,
    /// Whether the part's root was a chart space — a part that is no chart at all
    /// is not a chart with no values.
    root: bool,
    /// The open elements' local names, outermost first.
    stack: Vec<Vec<u8>>,
    /// The chart kinds the plot area declares, in part order and each named once:
    /// a kind is collapsed as it is read, so a part repeating one element costs
    /// nothing.
    kinds: Vec<String>,
    /// The chart type element being read.
    kind: Option<KindBuilder>,
    /// The title or other text being read.
    capture: Option<Capture>,
    /// The chart's own title, once read.
    title: Option<String>,
    /// The chart's axis titles, in part order.
    texts: Vec<String>,
    /// The chart's data label texts, in part order.
    labels: Vec<String>,
    /// How many of the chart's texts were past their cap.
    more_texts: usize,
    /// The series whose element is open.
    series: Option<Series>,
    /// The depth the open series element was pushed at.
    series_depth: Option<usize>,
    /// The series completed, in part order — at most [`MAX_SERIES`] of them.
    series_done: Vec<Series>,
    /// How many series the part held past [`MAX_SERIES`]: counted, never kept.
    more_series: usize,
    /// The series slot whose caches are being read, and the depth its element was
    /// pushed at.
    slot: Option<(Slot, usize)>,
    /// The point being read, and the depth its element was pushed at.
    point: Option<(usize, usize)>,
    /// The depth a series' own name element was pushed at.
    tx_depth: Option<usize>,
    /// Whether the part attaches the table its values live in.
    attached: bool,
}

impl Parser {
    fn new(date1904: bool) -> Self {
        Self {
            date1904,
            part_date1904: None,
            root: false,
            stack: Vec::new(),
            kinds: Vec::new(),
            kind: None,
            capture: None,
            title: None,
            texts: Vec::new(),
            labels: Vec::new(),
            more_texts: 0,
            series: None,
            series_depth: None,
            series_done: Vec::new(),
            more_series: 0,
            slot: None,
            point: None,
            tx_depth: None,
            attached: false,
        }
    }

    fn run(&mut self, xml: &[u8]) -> Result<(), ChartLoss> {
        let mut reader = Reader::from_reader(xml);
        let mut buffer = Vec::new();
        loop {
            match reader.read_event_into(&mut buffer) {
                Ok(Event::Start(event)) => {
                    let name = event.local_name().as_ref().to_vec();
                    self.stack.push(name.clone());
                    self.start(&event, &name)?;
                }
                Ok(Event::Empty(event)) => {
                    let name = event.local_name().as_ref().to_vec();
                    self.stack.push(name.clone());
                    self.start(&event, &name)?;
                    self.stack.pop();
                    self.end();
                }
                Ok(Event::Text(event)) => {
                    if let Ok(text) = event.xml10_content() {
                        self.text(&text);
                    }
                }
                Ok(Event::CData(event)) => {
                    if let Ok(text) = event.xml10_content() {
                        self.text(&text);
                    }
                }
                Ok(Event::GeneralRef(reference)) => self.entity(&reference),
                Ok(Event::End(_)) => {
                    self.stack.pop();
                    self.end();
                }
                Ok(Event::Eof) => break,
                Err(_) => return Err(ChartLoss::Part),
                Ok(_) => {}
            }
            buffer.clear();
        }
        // A part that ended inside an element was cut short, and what it did not
        // reach is a loss rather than a chart with nothing else.
        if !self.root || !self.stack.is_empty() {
            return Err(ChartLoss::Part);
        }
        Ok(())
    }

    /// The local name of the element `back` levels up (`2` is the parent of the
    /// innermost open element).
    fn ancestor(&self, back: usize) -> Option<&[u8]> {
        let index = self.stack.len().checked_sub(back)?;
        self.stack.get(index).map(Vec::as_slice)
    }

    fn start(&mut self, event: &BytesStart<'_>, name: &[u8]) -> Result<(), ChartLoss> {
        let depth = self.stack.len();
        if depth > MAX_DEPTH {
            return Err(ChartLoss::Part);
        }
        if depth == 1 {
            if name != b"chartSpace" {
                return Err(ChartLoss::Part);
            }
            // An extended chart's own root: a schema this reading does not parse,
            // said plainly rather than read as a chart with none.
            if event.name().prefix().is_some_and(|p| p.as_ref() == b"cx") {
                return Err(ChartLoss::Extended);
            }
            self.root = true;
        }
        match name {
            b"chartData" => return Err(ChartLoss::Extended),
            b"title" => {
                let kind = if self.ancestor(2) == Some(b"chart") {
                    CaptureKind::Title
                } else {
                    CaptureKind::Axis
                };
                if self.capture.is_none() {
                    self.capture = Some(Capture {
                        depth,
                        kind,
                        text: String::new(),
                    });
                }
            }
            // A paragraph or a line break inside a captured run starts a new
            // line of it: rich text is joined with a space, never glued.
            b"p" | b"br" => self.separate(),
            b"ser" if self.series.is_none() => {
                if self.series_done.len() < MAX_SERIES {
                    self.series = Some(Series::default());
                    self.series_depth = Some(depth);
                } else {
                    self.more_series += 1;
                }
            }
            b"tx" => match self.ancestor(2) {
                Some(b"ser") => self.tx_depth = Some(depth),
                Some(b"dLbl") if self.capture.is_none() => {
                    self.capture = Some(Capture {
                        depth,
                        kind: CaptureKind::Label,
                        text: String::new(),
                    });
                }
                _ => {}
            },
            b"cat" | b"val" | b"xVal" | b"yVal" | b"bubbleSize" => {
                if self.series.is_some() {
                    let slot = match name {
                        b"cat" | b"xVal" => Slot::Category,
                        b"val" | b"yVal" => Slot::Value,
                        _ => Slot::Size,
                    };
                    self.slot = Some((slot, depth));
                }
            }
            b"numCache" | b"strCache" | b"numLit" | b"strLit" => {
                self.open_cache(matches!(name, b"numCache" | b"numLit"));
            }
            // A multi-level category list states one cache per level.
            b"lvl" => self.open_cache(false),
            b"pt" => {
                if self.slot.is_some()
                    && let Some(index) = attr(event, b"idx").and_then(|idx| idx.trim().parse().ok())
                {
                    self.keep_point(index, depth);
                }
            }
            b"externalData" => self.attached = true,
            b"date1904" => {
                self.part_date1904 =
                    Some(matches!(attr(event, b"val").as_deref(), Some("1" | "true")));
            }
            // A chart type's own qualifiers are attributes of its child elements.
            b"barDir" | b"grouping" | b"ofPieType" => {
                if let Some(kind) = self.kind.as_mut() {
                    let value = attr(event, b"val");
                    match name {
                        b"barDir" => kind.bar_dir = value,
                        b"grouping" => kind.grouping = value,
                        _ => kind.of_pie = value,
                    }
                }
            }
            _ => {
                if self.ancestor(2) == Some(b"plotArea")
                    && let Some(kind) = kind_of(name)
                {
                    self.kind = Some(KindBuilder {
                        depth,
                        kind,
                        bar_dir: None,
                        grouping: None,
                        of_pie: None,
                    });
                }
            }
        }
        Ok(())
    }

    /// Open the next cache of the series slot being read; a cache outside one (a
    /// series' own name, a title's) is not a value this reading prints.
    fn open_cache(&mut self, numeric: bool) {
        let Some((slot, _)) = self.slot else {
            return;
        };
        let Some(series) = self.series.as_mut() else {
            return;
        };
        series.slot(slot).push(Cache {
            numeric,
            ..Cache::default()
        });
    }

    /// Keep the point at `index` of the cache now being read — unless the cache
    /// already holds [`MAX_POINTS`] of them, which the cache counts instead of
    /// allocating and formatting. A point past the cap is read but not kept, so
    /// its value reaches nothing.
    fn keep_point(&mut self, index: usize, depth: usize) {
        let Some((slot, _)) = self.slot else {
            return;
        };
        let kept = match self
            .series
            .as_mut()
            .and_then(|series| series.slot(slot).last_mut())
        {
            Some(cache) if cache.points.contains_key(&index) || cache.points.len() < MAX_POINTS => {
                true
            }
            Some(cache) => {
                cache.more += 1;
                false
            }
            None => false,
        };
        if kept {
            self.point = Some((index, depth));
        }
    }

    /// Start a new line of a captured run of text: a rich-text paragraph and a
    /// line break both separate what came before from what follows.
    fn separate(&mut self) {
        if let Some(capture) = self.capture.as_mut() {
            push_break(&mut capture.text);
        } else if self.tx_depth.is_some()
            && let Some(series) = self.series.as_mut()
            && let Some(name) = series.name.as_mut()
        {
            push_break(name);
        }
    }

    /// The string the text at the current position is collected into, if any.
    fn target(&mut self) -> Option<&mut String> {
        let depth = self.stack.len();
        let leaf = self.stack.last()?.as_slice();
        // A title, an axis title or a data label: its rich text runs and its
        // cached string, never the reference it points at.
        if let Some(capture) = self.capture.as_mut()
            && capture.depth < depth
        {
            return matches!(leaf, b"t" | b"v").then_some(&mut capture.text);
        }
        if let Some(tx) = self.tx_depth
            && tx < depth
        {
            if !matches!(leaf, b"t" | b"v") {
                return None;
            }
            let series = self.series.as_mut()?;
            return Some(series.name.get_or_insert_with(String::new));
        }
        if leaf == b"v"
            && let (Some((index, _)), Some((slot, _))) = (self.point, self.slot)
        {
            let series = self.series.as_mut()?;
            let cache = series.slot(slot).last_mut()?;
            return Some(cache.points.entry(index).or_default());
        }
        match leaf {
            b"formatCode" => {
                let (slot, _) = self.slot?;
                let series = self.series.as_mut()?;
                let cache = series.slot(slot).last_mut()?;
                Some(cache.format.get_or_insert_with(String::new))
            }
            _ => None,
        }
    }

    fn text(&mut self, text: &str) {
        if let Some(target) = self.target() {
            target.push_str(text);
        }
    }

    /// File one finished run of text: the title, an axis title or a data label.
    /// The last two are bounded together, so a label per point cannot crowd the
    /// axis titles out or grow the description without limit.
    fn text_read(&mut self, kind: CaptureKind, text: String) {
        if kind == CaptureKind::Title {
            self.title = Some(text);
            return;
        }
        if self.texts.len() + self.labels.len() >= MAX_TEXTS {
            self.more_texts += 1;
            return;
        }
        if kind == CaptureKind::Axis {
            self.texts.push(text);
        } else {
            self.labels.push(text);
        }
    }

    fn entity(&mut self, reference: &BytesRef<'_>) {
        if let Some(target) = self.target() {
            append_entity(target, reference);
        }
    }

    fn end(&mut self) {
        let depth = self.stack.len();
        if self
            .capture
            .as_ref()
            .is_some_and(|capture| capture.depth == depth + 1)
            && let Some(capture) = self.capture.take()
        {
            let text = one_line(capture.text.trim().to_owned());
            if !text.is_empty() {
                self.text_read(capture.kind, text);
            }
        }
        if self.tx_depth == Some(depth + 1) {
            self.tx_depth = None;
        }
        if self
            .slot
            .as_ref()
            .is_some_and(|(_, slot_depth)| *slot_depth == depth + 1)
        {
            self.slot = None;
        }
        if self
            .point
            .as_ref()
            .is_some_and(|(_, point_depth)| *point_depth == depth + 1)
        {
            self.point = None;
        }
        if self
            .kind
            .as_ref()
            .is_some_and(|kind| kind.depth == depth + 1)
            && let Some(kind) = self.kind.take()
        {
            let name = kind.name();
            if !self.kinds.contains(&name) {
                self.kinds.push(name);
            }
        }
        if self.series_depth == Some(depth + 1) {
            self.series_depth = None;
            if let Some(mut series) = self.series.take() {
                series.name = series.name.map(one_line).filter(|name| !name.is_empty());
                self.series_done.push(series);
            }
        }
    }

    /// The part's content: the header's kind and the lines under it — its title,
    /// its other text and one line per series, or the mark standing in for the
    /// values a series does not carry.
    fn finish(self) -> ChartRead {
        let date1904 = self.part_date1904.unwrap_or(self.date1904);
        let kind = (!self.kinds.is_empty()).then(|| self.kinds.join(" + "));
        let mut body = Vec::new();
        if let Some(title) = &self.title {
            body.push(format!("{CHART_TITLE} {title}"));
        }
        for text in &self.texts {
            body.push(format!("{CHART_TEXT} {text}"));
        }
        for label in &self.labels {
            body.push(format!("{CHART_LABEL} {label}"));
        }
        if self.more_texts > 0 {
            body.push(chart_excess(self.more_texts, "text(s)"));
        }
        let mut described = false;
        for (index, series) in self.series_done.iter().enumerate() {
            let (line, values) = series_line(index, series, date1904, self.attached);
            described |= values;
            body.push(line);
        }
        if self.more_series > 0 {
            body.push(chart_excess(self.more_series, "series"));
        }
        if self.series_done.is_empty() {
            // A part naming no series at all says what it does not carry the way a
            // series without values does.
            body.push(note(self.attached).to_owned());
        }
        ChartRead::Content {
            kind,
            body,
            values: described,
        }
    }
}

/// The mark standing in for values a chart part does not carry.
const fn note(attached: bool) -> &'static str {
    if attached {
        CHART_VALUES_ATTACHED
    } else {
        CHART_NO_VALUES
    }
}

/// A break between two runs of a captured text, unless `text` is empty or already
/// ends in one.
fn push_break(text: &mut String) {
    if !text.is_empty() && !text.ends_with(' ') {
        text.push(' ');
    }
}

/// One series' line: its label, then its categories and values — `Jan: 10` where
/// the part carries a category label for the value, the value alone where it does
/// not, and a bubble's size beside its value — or, when the part carries none of
/// its values, the categories it does carry and the mark standing in for them.
/// `index` numbers an unnamed series, and the second value says whether the part
/// carried a value for it at all.
fn series_line(index: usize, series: &Series, date1904: bool, attached: bool) -> (String, bool) {
    let label = chart_series(series.name.as_deref(), index);
    let mut stored = false;
    let Some(values) = series
        .values
        .first()
        .filter(|cache| !cache.points.is_empty())
    else {
        // The labels the missing values would be keyed by are the chart's own
        // content, so the categories the part carries are still printed.
        let mut categories = Vec::new();
        for point_index in series
            .categories
            .first()
            .map(|cache| cache.points.keys())
            .into_iter()
            .flatten()
        {
            let (category, category_stored) =
                category_label(&series.categories, *point_index, date1904);
            stored |= category_stored;
            categories.extend(category);
        }
        let mut line = label;
        if !categories.is_empty() {
            let _ = write!(line, " {}", categories.join(", "));
        }
        let _ = write!(line, " {}", note(attached));
        if stored {
            line.push_str(MARK_STORED);
        }
        return (line, false);
    };
    let mut points = Vec::new();
    for point_index in values.points.keys() {
        let Some((value, value_stored)) = point_text(values, *point_index, date1904) else {
            continue;
        };
        stored |= value_stored;
        let (category, category_stored) =
            category_label(&series.categories, *point_index, date1904);
        stored |= category_stored;
        let mut point = match category {
            Some(category) => format!("{category}: {value}"),
            None => value,
        };
        if let Some((size, size_stored)) = series
            .sizes
            .first()
            .and_then(|cache| point_text(cache, *point_index, date1904))
        {
            stored |= size_stored;
            let _ = write!(point, " {}", chart_size(&size));
        }
        points.push(point);
    }
    let mut line = format!("{label} {}", points.join(", "));
    if values.more > 0 {
        let _ = write!(line, ", {}", chart_excess(values.more, "value(s)"));
    }
    if stored {
        line.push_str(MARK_STORED);
    }
    (line, true)
}

/// One cached point as the reading shows it: a number through [`numfmt`] under
/// the cache's own format — the stored text where that format cannot be
/// reproduced, which the second value says — or the text the part wrote.
fn point_text(cache: &Cache, index: usize, date1904: bool) -> Option<(String, bool)> {
    let stored = cache.points.get(&index)?;
    let written = || one_line(stored.trim().to_owned());
    if !cache.numeric {
        return Some((written(), false));
    }
    let format = cache.format.as_deref();
    if numfmt::is_general(0, format) {
        return Some((written(), false));
    }
    let display = stored
        .trim()
        .parse::<f64>()
        .ok()
        .and_then(|value| numfmt::display(value, 0, format, date1904));
    Some(display.map_or_else(|| (written(), true), |text| (text, false)))
}

/// The label of the point at `index`: the innermost level's own entry for it, with
/// each coarser level's label — the last one at or before the index, since a group
/// label spans the points under it — in front of it (`East / Q1`). A single level
/// is just its own entry, and an empty label is no label. The second value says
/// whether a label is shown as stored.
fn category_label(levels: &[Cache], index: usize, date1904: bool) -> (Option<String>, bool) {
    let Some((innermost, coarse)) = levels.split_first() else {
        return (None, false);
    };
    let mut parts: Vec<String> = Vec::new();
    let mut stored = false;
    for level in coarse.iter().rev() {
        if let Some((group, _)) = level.points.range(..=index).next_back()
            && let Some((text, level_stored)) = point_text(level, *group, date1904)
            && !text.is_empty()
        {
            stored |= level_stored;
            parts.push(text);
        }
    }
    if let Some((text, level_stored)) = point_text(innermost, index, date1904)
        && !text.is_empty()
    {
        stored |= level_stored;
        parts.push(text);
    }
    if parts.is_empty() {
        (None, stored)
    } else {
        (Some(parts.join(" / ")), stored)
    }
}

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

    /// A classic chart part around `plot`; `chart` sits inside `c:chart` ahead of
    /// its plot area, `space` inside `c:chartSpace`.
    fn chart_space(plot: &str, chart: &str, space: &str) -> String {
        format!(
            r#"<c:chartSpace xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">{space}<c:chart>{chart}<c:plotArea>{plot}</c:plotArea></c:chart></c:chartSpace>"#
        )
    }

    /// One `c:ser` of a bar chart: its name, categories and values.
    fn series(name: &str, categories: &[&str], values: &[&str]) -> String {
        let categories = cache("strCache", None, categories);
        let values = cache("numCache", None, values);
        format!(
            r#"<c:ser><c:tx><c:strRef><c:f>Sheet1!$B$1</c:f><c:strCache><c:pt idx="0"><c:v>{name}</c:v></c:pt></c:strCache></c:strRef></c:tx><c:cat>{categories}</c:cat><c:val>{values}</c:val></c:ser>"#
        )
    }

    /// A `c:<kind>` cache over `points`, with `format` when one is given.
    fn cache(kind: &str, format: Option<&str>, points: &[&str]) -> String {
        let format = format.map_or_else(String::new, |format| {
            format!("<c:formatCode>{format}</c:formatCode>")
        });
        let mut cached = String::new();
        for (index, value) in points.iter().enumerate() {
            let _ = write!(cached, r#"<c:pt idx="{index}"><c:v>{value}</c:v></c:pt>"#);
        }
        format!(r"<c:{kind}>{format}{cached}</c:{kind}>")
    }

    /// The lines a chart part is read to.
    fn lines_of(xml: &str) -> Vec<String> {
        read_part(xml.as_bytes(), false).lines()
    }

    /// A bar chart's content is printed as its kind, its title, its other text and
    /// one line per series of category and value — several series each, and the
    /// category label paired with its value.
    #[test]
    fn a_chart_is_described_by_its_kind_title_and_series() {
        let plot = format!(
            r#"<c:barChart><c:barDir val="col"/><c:grouping val="clustered"/><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:cat><c:val>{}</c:val></c:ser>{}</c:barChart><c:catAx><c:title><c:tx><c:rich><a:p><a:r><a:t>Month</a:t></a:r></a:p></c:rich></c:tx></c:title></c:catAx>"#,
            cache("strCache", None, &["Jan", "Feb"]),
            cache("numCache", Some("General"), &["10", "20"]),
            series("Cost", &["Jan", "Feb"], &["4", "8"]),
        );
        let title = r"<c:title><c:tx><c:rich><a:p><a:r><a:t>Sales by region</a:t></a:r></a:p></c:rich></c:tx></c:title>";
        let xml = chart_space(&plot, title, "");
        assert_eq!(
            lines_of(&xml),
            [
                "Chart (column chart):",
                "  Title: Sales by region",
                "  Text: Month",
                "  Series \"Revenue\": Jan: 10, Feb: 20",
                "  Series \"Cost\": Jan: 4, Feb: 8",
            ]
        );
    }

    /// A rich-text title keeps its paragraphs apart, and a data label's own text
    /// prints under its own label, so a model can tell it from an axis title.
    #[test]
    fn rich_text_paragraphs_and_data_labels_are_kept_apart() {
        let plot = format!(
            r#"<c:barChart><c:barDir val="col"/><c:ser>{}</c:ser><c:dLbls><c:dLbl><c:tx><c:rich><a:p><a:r><a:t>Peak</a:t></a:r></a:p></c:rich></c:tx></c:dLbl></c:dLbls></c:barChart>"#,
            r#"<c:val><c:numCache><c:pt idx="0"><c:v>10</c:v></c:pt></c:numCache></c:val>"#,
        );
        let title = r"<c:title><c:tx><c:rich><a:p><a:r><a:t>Sales</a:t></a:r></a:p><a:p><a:r><a:t>by region</a:t></a:r></a:p></c:rich></c:tx></c:title>";
        assert_eq!(
            lines_of(&chart_space(&plot, title, "")),
            [
                "Chart (column chart):",
                "  Title: Sales by region",
                "  Label: Peak",
                "  Series 1: 10",
            ]
        );
    }

    /// The cache's own format decides how a value is shown, so a date reads as a
    /// date; a format the reading cannot reproduce shows the stored number, under
    /// the mark a spreadsheet cell carries, once for the series.
    #[test]
    fn a_cached_value_keeps_its_own_number_format() {
        let dates = cache("numCache", Some("mmm-yy"), &["45200", "45231"]);
        let shares = cache("numCache", Some("0.00%"), &["0.25", "0.5"]);
        let plot = format!(
            r#"<c:lineChart><c:ser><c:tx><c:strRef><c:strCache><c:pt idx="0"><c:v>Sales</c:v></c:pt></c:strCache></c:strRef></c:tx><c:cat>{dates}</c:cat><c:val>{shares}</c:val></c:ser></c:lineChart>"#
        );
        assert_eq!(
            lines_of(&chart_space(&plot, "", "")),
            [
                "Chart (line chart):",
                "  Series \"Sales\": 2023-10-01: 25.00%, 2023-11-01: 50.00%",
            ]
        );

        // A format this reading does not reproduce is said once, with the number
        // as stored rather than a display this reader would have to invent.
        let values = cache("numCache", Some("# ?/?"), &["1.5"]);
        let plot = format!(
            r#"<c:pieChart><c:ser><c:tx><c:strRef><c:strCache><c:pt idx="0"><c:v>Share</c:v></c:pt></c:strCache></c:strRef></c:tx><c:val>{values}</c:val></c:ser></c:pieChart>"#
        );
        assert_eq!(
            lines_of(&chart_space(&plot, "", "")),
            [
                "Chart (pie chart):",
                "  Series \"Share\": 1.5 (stored number, format not shown)",
            ]
        );
    }

    /// A series without a name is numbered, a chart without a plot type prints no
    /// kind, several plot types are named together, and a scatter chart's x values
    /// are the labels its y values are paired with.
    #[test]
    fn kinds_names_and_scatter_are_all_printed() {
        let plot = format!(
            r#"<c:barChart><c:barDir val="bar"/><c:grouping val="stacked"/><c:ser><c:cat>{}</c:cat><c:val>{}</c:val></c:ser></c:barChart><c:scatterChart><c:ser><c:tx><c:strRef><c:strCache><c:pt idx="0"><c:v>Points</c:v></c:pt></c:strCache></c:strRef></c:tx><c:xVal>{}</c:xVal><c:yVal>{}</c:yVal></c:ser></c:scatterChart>"#,
            cache("strCache", None, &["A"]),
            cache("numCache", None, &["1"]),
            cache("numCache", None, &["1", "2"]),
            cache("numCache", None, &["10", "20"]),
        );
        assert_eq!(
            lines_of(&chart_space(&plot, "", "")),
            [
                "Chart (stacked bar chart + scatter chart):",
                "  Series 1: A: 1",
                "  Series \"Points\": 1: 10, 2: 20",
            ]
        );
        assert_eq!(
            lines_of(&chart_space("", "", "")),
            ["Chart:", "  (no values in the chart part)"]
        );
    }

    /// A multi-level category list prints its levels root first, a coarser level's
    /// label spanning the points under it, and a bubble chart prints each value's
    /// size beside it.
    #[test]
    fn multi_level_categories_and_bubble_sizes_are_printed() {
        // The levels are stated innermost first, as a part writes them.
        let two_levels = format!(
            r"<c:multiLvlStrRef><c:multiLvlStrCache>{}{}</c:multiLvlStrCache></c:multiLvlStrRef>",
            cache("lvl", None, &["Q1", "Q2"]),
            cache("lvl", None, &["East", "West"]),
        );
        let spanning = format!(
            r"<c:multiLvlStrRef><c:multiLvlStrCache>{}{}</c:multiLvlStrCache></c:multiLvlStrRef>",
            cache("lvl", None, &["Q1", "Q2"]),
            cache("lvl", None, &["East"]),
        );
        let sizes = cache("numCache", None, &["5", "7"]);
        let values = cache("numCache", None, &["10", "20"]);
        let plot = |categories: &str| {
            format!(
                r"<c:bubbleChart><c:ser><c:cat>{categories}</c:cat><c:val>{values}</c:val><c:bubbleSize>{sizes}</c:bubbleSize></c:ser></c:bubbleChart>"
            )
        };
        assert_eq!(
            lines_of(&chart_space(&plot(&two_levels), "", "")),
            [
                "Chart (bubble chart):",
                "  Series 1: East / Q1: 10 (size 5), West / Q2: 20 (size 7)",
            ]
        );
        // A group label stated once covers every point of the group.
        assert_eq!(
            lines_of(&chart_space(&plot(&spanning), "", "")),
            [
                "Chart (bubble chart):",
                "  Series 1: East / Q1: 10 (size 5), East / Q2: 20 (size 7)",
            ]
        );
    }

    /// A cut chart part is a loss, not a chart whose content happens to end where
    /// the package stopped.
    #[test]
    fn a_cut_part_is_a_loss() {
        let cut = r#"<c:chartSpace xmlns:c="x"><c:chart><c:plotArea><c:barChart><c:ser><c:tx><c:strRef><c:strCache><c:pt idx="0"><c:v>Revenue</c:v>"#;
        assert_eq!(
            read_part(cut.as_bytes(), false).lines(),
            ["Chart: (the chart part could not be read)"]
        );
    }

    /// A point index far beyond any chart allocates nothing: the cache is sparse,
    /// so the index the part gives a point only names it.
    #[test]
    fn a_wild_point_index_allocates_nothing() {
        let wild = r#"<c:chartSpace xmlns:c="x"><c:chart><c:plotArea><c:barChart><c:ser><c:val><c:numCache><c:pt idx="18446744073709551615"><c:v>1</c:v></c:pt></c:numCache></c:val></c:ser></c:barChart></c:plotArea></c:chart></c:chartSpace>"#;
        assert_eq!(
            read_part(wild.as_bytes(), false).lines(),
            ["Chart (column chart):", "  Series 1: 1"]
        );
    }

    /// A part nested past the depth bound is a loss rather than a walk that keeps
    /// a stack entry per level for a document to decide.
    #[test]
    fn a_part_nested_past_the_bound_is_a_loss() {
        let deep = format!(
            r#"<c:chartSpace xmlns:c="x"><c:chart><c:plotArea><c:barChart>{}{}</c:barChart></c:plotArea></c:chart></c:chartSpace>"#,
            "<c:x>".repeat(MAX_DEPTH),
            "</c:x>".repeat(MAX_DEPTH),
        );
        assert_eq!(
            read_part(deep.as_bytes(), false).lines(),
            ["Chart: (the chart part could not be read)"]
        );
    }

    /// A chart whose values are only referenced states that for the series that
    /// carries none, keeps the names the part does carry, and one whose table is
    /// attached says where its values are instead of claiming the file holds none.
    #[test]
    fn a_chart_without_cached_values_says_where_its_numbers_are() {
        let plot = r#"<c:barChart><c:barDir val="col"/><c:ser><c:tx><c:strRef><c:f>Sheet1!$B$1</c:f></c:strRef></c:tx><c:val><c:numRef><c:f>Sheet1!$B$2:$B$3</c:f></c:numRef></c:val></c:ser></c:barChart>"#;
        assert_eq!(
            lines_of(&chart_space(plot, "", "")),
            [
                "Chart (column chart):",
                "  Series 1: (no values in the chart part)",
            ]
        );
        let external = r#"<c:externalData r:id="rId1"/>"#;
        assert_eq!(
            lines_of(&chart_space(plot, "", external)),
            [
                "Chart (column chart):",
                "  Series 1: (values only in the attached table)",
            ]
        );
        // The title and the series' name the part does carry are still shown.
        let title = r"<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>";
        let named = r#"<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:val><c:numRef><c:f>Sheet1!$B$2:$B$3</c:f></c:numRef></c:val></c:ser></c:barChart>"#;
        assert_eq!(
            lines_of(&chart_space(named, title, external)),
            [
                "Chart (column chart):",
                "  Title: Sales",
                "  Series \"Revenue\": (values only in the attached table)",
            ]
        );
        // The labels the missing values would be keyed by are the part's own
        // content too, so the categories it caches are still printed.
        let categories = cache("strCache", None, &["Jan", "Feb"]);
        let labelled = format!(
            r#"<c:barChart><c:barDir val="col"/><c:ser><c:cat>{categories}</c:cat><c:val><c:numRef><c:f>Sheet1!$B$2:$B$3</c:f></c:numRef></c:val></c:ser></c:barChart>"#
        );
        assert_eq!(
            lines_of(&chart_space(&labelled, "", "")),
            [
                "Chart (column chart):",
                "  Series 1: Jan, Feb (no values in the chart part)",
            ]
        );
        // A chart with one series cached beside one that is not says so for the
        // series it could not print, rather than dropping it.
        let uncached = r"<c:ser><c:tx><c:strRef><c:f>Sheet1!$C$1</c:f></c:strRef></c:tx><c:val><c:numRef><c:f>Sheet1!$C$2:$C$3</c:f></c:numRef></c:val></c:ser>";
        let mixed = format!(
            r#"<c:barChart><c:barDir val="col"/><c:ser><c:tx><c:strRef><c:strCache><c:pt idx="0"><c:v>Revenue</c:v></c:pt></c:strCache></c:strRef></c:tx><c:val><c:numCache><c:pt idx="0"><c:v>10</c:v></c:pt></c:numCache></c:val></c:ser>{uncached}</c:barChart>"#
        );
        assert_eq!(
            lines_of(&chart_space(&mixed, "", "")),
            [
                "Chart (column chart):",
                "  Series \"Revenue\": 10",
                "  Series 2: (no values in the chart part)",
            ]
        );
        // A chart whose every series is uncached keeps its place in the report.
        let read = read_part(chart_space(plot, "", "").as_bytes(), false);
        assert!(!read.described());
    }

    /// A part this reading cannot parse is said to be unreadable, and an extended
    /// `chartEx` part — a schema of its own — is named as that rather than read as
    /// a chart with no content.
    #[test]
    fn parts_of_another_schema_are_said_not_read() {
        assert_eq!(
            read_part(b"<chart/>", false).lines(),
            ["Chart: (the chart part could not be read)"]
        );
        assert_eq!(
            read_part(
                br#"<cx:chartSpace xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex"><cx:chartData/></cx:chartSpace>"#,
                false
            )
            .lines(),
            ["Chart: (an extended chart kind, not read)"]
        );
    }

    /// The description is bounded: the series, the points and the other texts past
    /// their caps are counted, never silently dropped.
    #[test]
    fn the_description_is_bounded_and_says_what_it_left_out() {
        let values: Vec<String> = (0..(MAX_POINTS + 3)).map(|i| i.to_string()).collect();
        let values: Vec<&str> = values.iter().map(String::as_str).collect();
        let sers: String = (0..(MAX_SERIES + 2))
            .map(|i| series(&format!("S{i}"), &[], &values))
            .collect();
        let axes = (0..=MAX_TEXTS).fold(String::new(), |mut axes, i| {
            let _ = write!(
                axes,
                r"<c:catAx><c:title><c:tx><c:rich><a:p><a:r><a:t>T{i}</a:t></a:r></a:p></c:rich></c:tx></c:title></c:catAx>"
            );
            axes
        });
        let plot = format!(r"<c:lineChart>{sers}{axes}</c:lineChart>");
        let lines = lines_of(&chart_space(&plot, "", ""));
        assert_eq!(lines[0], "Chart (line chart):");
        assert_eq!(lines.len(), 1 + MAX_TEXTS + 1 + MAX_SERIES + 1);
        assert_eq!(lines[MAX_TEXTS + 1], format!("  {} more text(s)", 1));
        assert_eq!(lines.last().map(String::as_str), Some("  2 more series"));
        assert!(
            lines[MAX_TEXTS + 2].ends_with(", 3 more value(s)"),
            "{:?}",
            lines[MAX_TEXTS + 2]
        );
    }

    /// The chart block's header, its series label and every mark and annotation a
    /// chart's lines carry are names the document kit's refusals know, so a find
    /// copied out of a reading that carries one is refused for what it is rather
    /// than searched for as the document's own text.
    #[test]
    fn the_marks_the_kit_refuses_are_the_ones_a_reading_prints() {
        let patterns: Vec<regex::Regex> = crate::docgen::docx_marks()
            .iter()
            .map(|mark| regex::Regex::new(&mark.pattern).expect("a mark's own pattern"))
            .collect();
        for mark in [
            chart_header(Some("column chart")),
            chart_series(Some("Revenue"), 0),
            chart_size("12"),
            chart_excess(3, "value(s)"),
            chart_excess(1, "series"),
            chart_excess(1, "text(s)"),
            MARK_STORED.trim().to_owned(),
            CHART_NO_VALUES.to_owned(),
            CHART_VALUES_ATTACHED.to_owned(),
            CHART_UNREADABLE.to_owned(),
            CHART_EXTENDED.to_owned(),
        ] {
            assert!(
                patterns.iter().any(|pattern| pattern.is_match(&mark)),
                "{mark:?} is a mark a chart reading prints, which no entry of the shared list \
                 names — a mark the kit refuses belongs in assets/docgen/rules.json's docx_marks"
            );
        }
        // The bare header a part naming no kind prints stays out: it is an
        // ordinary word a document's own text opens a sentence with.
        assert!(
            !patterns
                .iter()
                .any(|pattern| pattern.is_match(&chart_header(None))),
            "a bare `Chart:` is a document's own word, never a refusal"
        );
    }

    /// The chart parts a part names are the `chart` elements' relationship
    /// targets, in part order, without repeats, and only the ones the part's own
    /// relationships resolve.
    #[test]
    fn chart_references_resolve_in_part_order() {
        let xml = br#"<p:sld><p:graphicFrame><a:graphic><a:graphicData><c:chart r:id="rId2"/></a:graphicData></a:graphic></p:graphicFrame><p:graphicFrame><a:graphic><a:graphicData><c:chart r:id="rId1"/></a:graphicData></a:graphic></p:graphicFrame><p:graphicFrame><a:graphic><a:graphicData><c:chart r:id="rId2"/></a:graphicData></a:graphic></p:graphicFrame>"#;
        let rels = vec![
            Relationship {
                id: "rId1".to_owned(),
                kind: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"
                    .to_owned(),
                target: "../charts/chart1.xml".to_owned(),
            },
            Relationship {
                id: "rId2".to_owned(),
                kind: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"
                    .to_owned(),
                target: "../charts/chart2.xml".to_owned(),
            },
        ];
        assert_eq!(
            chart_parts(xml, &rels, "ppt/slides/"),
            ["ppt/charts/chart2.xml", "ppt/charts/chart1.xml"]
        );
    }
}