pspp 0.6.1

Statistical analysis software
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
// PSPP - a program for statistical analysis.
// Copyright (C) 2025 Free Software Foundation, Inc.
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program.  If not, see <http://www.gnu.org/licenses/>.

//! Pivot tables.
//!
//! Pivot tables are PSPP's primary form of output.  They are analogous to the
//! pivot tables you might be familiar with from spreadsheets and databases.
//! See <https://en.wikipedia.org/wiki/Pivot_table> for a brief introduction to
//! the overall concept of a pivot table.
//!
//! In PSPP, the most important internal pieces of a pivot table are:
//!
//! - Title.  Every pivot table has a title that is displayed above it.  It also
//!   has an optional caption (displayed below it) and corner text (displayed in
//!   the upper left corner).
//!
//! - Dimensions.  A dimension consists of zero or more categories.  A category
//!   has a label, such as "df" or "Asymp. Sig." or 123 or a variable name.  The
//!   categories are the leaves of a tree whose non-leaf nodes form groups of
//!   categories.  The tree always has a root group whose label is the name of
//!   the dimension.
//!
//! - Axes.  A table has three axes: column, row, and layer.  Each dimension is
//!   assigned to an axis, and each axis has zero or more dimensions.  When an
//!   axis has more than one dimension, they are ordered from innermost to
//!   outermost.
//!
//! - Data.  A table's data consists of zero or more cells.  Each cell maps from
//!   a category for each dimension to a value, which is commonly a number but
//!   could also be a variable name or an arbitrary text string.

// Warn about missing docs, but not for items declared with `#[cfg(test)]`.
#![cfg_attr(not(test), warn(missing_docs))]

use std::{
    collections::HashMap,
    fmt::{Debug, Display},
    iter::{FusedIterator, once, repeat_n, zip},
    ops::{Index, IndexMut, Not, Range},
    sync::Arc,
};

use chrono::{NaiveDateTime, Utc};
use enum_iterator::Sequence;
use enum_map::{Enum, EnumMap, enum_map};
use itertools::Itertools;
pub use look_xml::{Length, TableProperties};
use serde::{Deserialize, Serialize, ser::SerializeMap};
use smallvec::{SmallVec, smallvec};

use crate::{
    format::{Decimal, F40, F40_2, F40_3, Format, PCT40_1, Settings as FormatSettings},
    output::pivot::{
        look::{Look, Sizing},
        value::{BareValue, Value, ValueFormat, ValueOptions},
    },
    settings::{Settings, Show},
    variable::Variable,
};
pub(crate) use tlo::parse_bool;

mod output;
pub use output::OutputTables;
mod look_xml;
mod tlo;
pub mod value;

#[cfg(test)]
pub mod tests;

pub mod look;

/// A 3-dimensional axis.
#[derive(Copy, Clone, Debug, Enum, PartialEq, Eq, Sequence, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Axis3 {
    /// X axis.
    X,
    /// Y axis.
    Y,
    /// Z axis.
    Z,
}

impl Axis3 {
    /// Transposes the X and Y axes.  Returns `None` if this represents the Z
    /// axis.
    pub fn transpose(&self) -> Option<Self> {
        match self {
            Axis3::X => Some(Axis3::Y),
            Axis3::Y => Some(Axis3::X),
            Axis3::Z => None,
        }
    }
}

impl From<Axis2> for Axis3 {
    fn from(axis2: Axis2) -> Self {
        match axis2 {
            Axis2::X => Self::X,
            Axis2::Y => Self::Y,
        }
    }
}

/// An axis within a pivot table.
#[derive(Clone, Debug, Default, Serialize)]
pub struct Axis {
    /// `dimensions[0]` is the innermost dimension.
    pub dimensions: Vec<usize>,
}

/// Iterator over one of the [Axis3] axes in a [PivotTable].
///
/// The items for this iterator are the index values for each of the dimensions
/// along the axis, each along `0..n` where `n` is the number of leaves in the
/// dimension.
///
/// Use [PivotTable::axis_values] to construct an `AxisIterator`.
struct AxisIterator {
    indexes: SmallVec<[usize; 4]>,
    lengths: SmallVec<[usize; 4]>,
    done: bool,
}

impl FusedIterator for AxisIterator {}
impl Iterator for AxisIterator {
    type Item = SmallVec<[usize; 4]>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.done {
            None
        } else {
            let retval = self.indexes.clone();
            for (index, len) in self.indexes.iter_mut().zip(self.lengths.iter().copied()) {
                *index += 1;
                if *index < len {
                    return Some(retval);
                };
                *index = 0;
            }
            self.done = true;
            Some(retval)
        }
    }
}

impl PivotTable {
    /// Constructs a new `PivotTable` with the given `dimensions` along the
    /// specified axes.
    ///
    /// The caller should add a title to the pivot table using [with_title] and
    /// add data with [with_data] or [insert].
    ///
    /// [with_title]: Self::with_title
    /// [with_data]: Self::with_data
    /// [insert]: Self::insert
    pub fn new(structure: impl Into<Structure>) -> Self {
        let structure = structure.into();
        Self {
            style: PivotTableStyle::default().with_look(Settings::global().look.clone()),
            layer: repeat_n(0, structure.axes[Axis3::Z].dimensions.len()).collect(),
            structure,
            ..Self::default()
        }
    }

    /// Returns this pivot table with the given `title`.
    ///
    /// The title is displayed above the pivot table.  Every pivot table should
    /// have a title.
    pub fn with_title(mut self, title: impl Into<Value>) -> Self {
        self.metadata.title = Some(Box::new(title.into()));
        self.style.show_title = true;
        self
    }

    /// Returns this pivot table with the given `title`, if it is non-`None`.
    pub fn with_optional_title(self, title: Option<Value>) -> Self {
        if let Some(title) = title {
            self.with_title(title)
        } else {
            self
        }
    }

    /// Returns this pivot table with the given `caption`.
    ///
    /// The caption is displayed below the pivot table.  Captions are optional.
    pub fn with_caption(mut self, caption: impl Into<Value>) -> Self {
        self.metadata.caption = Some(Box::new(caption.into()));
        self.style.show_caption = true;
        self
    }

    /// Returns this pivot table with the given `caption`, if it is non-`None`.
    pub fn with_optional_caption(self, caption: Option<Value>) -> Self {
        if let Some(caption) = caption {
            self.with_caption(caption)
        } else {
            self
        }
    }

    /// Returns this pivot table with the given `corner_text`.
    ///
    /// The corner text is displayed in the top-left corner of the pivot table,
    /// above the row headings and to the left of the column headings.  The
    /// space used by corner text can also be used for [Dimension] titles.
    pub fn with_corner_text(mut self, corner_text: impl Into<Value>) -> Self {
        self.metadata.corner_text = Some(Box::new(corner_text.into()));
        self
    }

    /// Returns this pivot table with the given `footnotes`.
    pub fn with_footnotes(mut self, footnotes: Footnotes) -> Self {
        debug_assert!(self.footnotes.is_empty());
        self.footnotes = footnotes;
        self
    }

    /// Returns this pivot table with the given `look`.
    pub fn with_look(self, look: Arc<Look>) -> Self {
        Self {
            style: self.style.with_look(look),
            ..self
        }
    }

    /// Returns this pivot table with the given `style`.
    pub fn with_style(self, style: PivotTableStyle) -> Self {
        Self { style, ..self }
    }

    /// Returns this pivot table with the given `metadata`.
    pub fn with_metadata(self, metadata: PivotTableMetadata) -> Self {
        Self { metadata, ..self }
    }

    /// Returns this pivot table with the given `subtype`.
    ///
    /// A subtype is a locale-invariant command ID for the particular kind of
    /// output that this table represents in the procedure.  This can be the
    /// same as the command name, e.g. `Frequencies`, or different, e.g. `Case
    /// Processing Summary`.
    ///
    /// `Notes` and `Warnings` are common generic subtypes.
    pub fn with_subtype(self, subtype: impl Into<Value>) -> Self {
        Self {
            metadata: self.metadata.with_subtype(subtype),
            ..self
        }
    }

    /// Returns this pivot table with the given `show_values`.
    pub fn with_show_values(self, show_values: Option<Show>) -> Self {
        Self {
            style: self.style.with_show_values(show_values),
            ..self
        }
    }

    /// Returns this pivot table with the given `show_variables`.
    pub fn with_show_variables(self, show_variables: Option<Show>) -> Self {
        Self {
            style: self.style.with_show_variables(show_variables),
            ..self
        }
    }

    /// Returns this pivot table with the given `show_title`.
    pub fn with_show_title(self, show_title: bool) -> Self {
        Self {
            style: self.style.with_show_title(show_title),
            ..self
        }
    }

    /// Returns this pivot table with the given `show_caption`.
    pub fn with_show_caption(self, show_caption: bool) -> Self {
        Self {
            style: self.style.with_show_caption(show_caption),
            ..self
        }
    }

    /// Returns this pivot table with its [Look] modified to show empty rows and
    /// columns.
    pub fn with_show_empty(mut self) -> Self {
        if self.style.look.hide_empty {
            self.look_mut().hide_empty = false;
        }
        self
    }

    /// Returns this pivot table with its [Look] modified to hide empty rows and
    /// columns.
    pub fn with_hide_empty(mut self) -> Self {
        if !self.style.look.hide_empty {
            self.look_mut().hide_empty = true;
        }
        self
    }

    /// Returns this pivot table with the current layer set to `layer`.
    ///
    /// The caller might want to modify the table's look to print just a single
    /// layer.
    pub fn with_layer(mut self, layer: &[usize]) -> Self {
        assert!(self.is_valid_layer(layer));
        self.layer.clear();
        self.layer.extend_from_slice(layer);
        self
    }

    /// Returns true if `layer` is a valid argument for
    /// [PivotTable::with_layer].
    pub fn is_valid_layer(&mut self, layer: &[usize]) -> bool {
        let layer_dimensions = self.axis_dimensions(Axis3::Z);
        layer.len() == layer_dimensions.len()
            && zip(layer, layer_dimensions).all(|(value, dimension)| *value < dimension.len())
    }

    /// Returns this pivot table set to print all layers.
    pub fn with_all_layers(mut self) -> Self {
        if !self.style.look.print_all_layers {
            self.look_mut().print_all_layers = true;
        }
        self
    }

    /// Returns this pivot table with the specified decimal point.
    pub fn with_decimal(mut self, decimal: Decimal) -> Self {
        self.style.settings.decimal = decimal;
        self
    }

    /// Returns this pivot table with the date set as specified.
    pub fn with_date(mut self, date: Option<NaiveDateTime>) -> Self {
        self.metadata.date = date;
        self
    }

    /// Inserts `number` into the cell with the given `data_indexes`, drawing
    /// its format from `class`.
    pub fn insert_number(&mut self, data_indexes: &[usize], number: Option<f64>, class: Class) {
        let format = match class {
            Class::Other => Settings::global().default_format,
            Class::Integer => F40,
            Class::Correlations => F40_3,
            Class::Significance => F40_3,
            Class::Percent => PCT40_1,
            Class::Residual => F40_2,
            Class::Count => F40, // XXX
        };
        let value = Value::new_number(number).with_format(match class {
            Class::Other => ValueFormat::SmallE(format),
            _ => ValueFormat::Other(format),
        });
        self.insert(data_indexes, value);
    }

    /// Returns an iterator for all the values along `axis`.
    fn axis_values(&self, axis: Axis3) -> AxisIterator {
        self.structure.axis_values(axis)
    }

    fn axis_extent(&self, axis: Axis3) -> usize {
        self.structure.axis_extent(axis)
    }

    /// Returns the indexes for the layer to be printed or display on-screen.
    pub fn layer(&self) -> &[usize] {
        &self.layer
    }

    /// Returns the pivot table's dimensions.
    pub fn dimensions(&self) -> &[Dimension] {
        &self.structure.dimensions
    }

    /// Returns the pivot table's axes.
    pub fn axes(&self) -> &EnumMap<Axis3, Axis> {
        &self.structure.axes
    }

    /// Returns the pivot table's [Look], for modification.
    pub fn look_mut(&mut self) -> &mut Look {
        self.style.look_mut()
    }

    /// Returns a label for the table, which is either its title or a default
    /// string.
    pub fn label(&self) -> String {
        match &self.metadata.title {
            Some(title) => title.display(self).to_string(),
            None => String::from("Table"),
        }
    }

    /// Returns the table's title, or an empty value if it doesn't have one.
    pub fn title(&self) -> &Value {
        match &self.metadata.title {
            Some(title) => title,
            None => Value::static_empty(),
        }
    }

    /// Returns the table's subtype, or an empty value if it doesn't have one.
    pub fn subtype(&self) -> &Value {
        match &self.metadata.subtype {
            Some(subtype) => subtype,
            None => Value::static_empty(),
        }
    }

    /// Returns the `HashMap` for cells.  Indexes in the map can be computed
    /// with [cell_index](Self::cell_index).
    pub fn cells(&self) -> &HashMap<usize, Value> {
        &self.cells
    }

    /// Returns the number of cells in the pivot table, which is the product of
    /// the number of categories in each dimension.  (If any of the dimensions
    /// have zero categories, or if the table has no dimensions, the result is zero.)
    pub fn n_cells(&self) -> usize {
        self.structure.n_cells()
    }

    /// Computes an index into the `cells` `HashMap` for `cell_index`.
    pub fn cell_index<C>(&self, cell_index: C) -> usize
    where
        C: CellIndex,
    {
        cell_index.cell_index(self.dimensions().iter().map(|d| d.len()))
    }

    /// Inserts a cell with the given `value` and `cell_index`.
    pub fn insert<C>(&mut self, cell_index: C, value: impl Into<Value>)
    where
        C: CellIndex,
    {
        self.cells.insert(self.cell_index(cell_index), value.into());
    }

    /// Returns the cell with the given `cell_index`, if there is one.
    pub fn get<C>(&self, cell_index: C) -> Option<&Value>
    where
        C: CellIndex,
    {
        self.cells.get(&self.cell_index(cell_index))
    }

    /// Returns the pivot table with cell indexes and values from `iter`
    /// inserted as data.
    pub fn with_data<C>(mut self, iter: impl IntoIterator<Item = (C, Value)>) -> Self
    where
        C: CellIndex,
    {
        self.extend(iter);
        self
    }

    /// Converts per-axis presentation-order indexes in `presentation_indexes`,
    /// into data indexes for each dimension.
    fn convert_indexes_ptod(
        &self,
        presentation_indexes: EnumMap<Axis3, &[usize]>,
    ) -> SmallVec<[usize; 4]> {
        let mut data_indexes = SmallVec::from_elem(0, self.dimensions().len());
        for (axis, presentation_indexes) in presentation_indexes {
            for (&dim_index, &pindex) in self.structure.axes[axis]
                .dimensions
                .iter()
                .zip(presentation_indexes.iter())
            {
                data_indexes[dim_index] = self.structure.dimensions[dim_index].ptod[pindex];
            }
        }
        data_indexes
    }

    /// Returns an iterator for the layer axis:
    ///
    /// - If `print` is true and `self.look.print_all_layers`, then the iterator
    ///   will visit all values of the layer axis.
    ///
    /// - Otherwise, the iterator will just visit `self.current_layer`.
    pub fn layers(&self, print: bool) -> Box<dyn Iterator<Item = SmallVec<[usize; 4]>>> {
        if print && self.style.look.print_all_layers {
            Box::new(self.axis_values(Axis3::Z))
        } else {
            Box::new(once(SmallVec::from_slice(&self.layer)))
        }
    }

    /// Transposes row and columns.
    pub fn transpose(&mut self) {
        self.structure.axes.swap(Axis3::X, Axis3::Y);
    }

    /// Returns an iterator through dimensions on the given `axis`.
    pub fn axis_dimensions(
        &self,
        axis: Axis3,
    ) -> impl DoubleEndedIterator<Item = &Dimension> + ExactSizeIterator {
        self.structure.axis_dimensions(axis)
    }

    fn find_dimension(&self, dim_index: usize) -> Option<(Axis3, usize)> {
        debug_assert!(dim_index < self.structure.dimensions.len());
        for axis in enum_iterator::all::<Axis3>() {
            for (position, dimension) in self.axes()[axis].dimensions.iter().copied().enumerate() {
                if dimension == dim_index {
                    return Some((axis, position));
                }
            }
        }
        None
    }

    /// Moves dimension with index `dim_index` from its current axis to
    /// `new_axis` in position `new_position`.
    ///
    /// `dim_index` is an overall dimension index, in the order passed to
    /// [PivotTable::new] and returned by [PivotTable::dimensions].  This method
    /// doesn't change these indexes.
    ///
    /// `new_position` is an index within `new_axis`, in the range `0..n` where
    /// `n` is the final number of dimensions along that axis.
    ///
    /// # Panic
    ///
    /// Panics if `dim_index` or `new_position` is outside the valid range.
    pub fn move_dimension(&mut self, dim_index: usize, new_axis: Axis3, new_position: usize) {
        let (old_axis, old_position) = self.find_dimension(dim_index).unwrap();
        if old_axis == new_axis && old_position == new_position {
            return;
        }

        // Update the current layer, if necessary.  If we're moving within the
        // layer axis, preserve the current layer.
        match (old_axis, new_axis) {
            (Axis3::Z, Axis3::Z) => {
                // Rearrange the layer axis.
                if old_position < new_position {
                    self.layer[old_position..=new_position].rotate_left(1);
                } else {
                    self.layer[new_position..=old_position].rotate_right(1);
                }
            }
            (Axis3::Z, _) => {
                // A layer is becoming a row or column.
                self.layer.remove(old_position);
            }
            (_, Axis3::Z) => {
                // A row or column is becoming a layer.
                self.layer.insert(new_position, 0);
            }
            _ => (),
        }

        self.structure.axes[old_axis]
            .dimensions
            .remove(old_position);
        self.structure.axes[new_axis]
            .dimensions
            .insert(new_position, dim_index);
    }
}

impl From<&PivotTable> for ValueOptions {
    fn from(value: &PivotTable) -> Self {
        ValueOptions {
            show_values: value.style.show_values,
            show_variables: value.style.show_variables,
            small: value.style.small,
            footnote_marker_type: value.style.look.footnote_marker_type,
            settings: value.style.settings.clone().with_leading_zero_pct(true),
        }
    }
}

/// A dimension.
///
/// A [Dimension] identifies the categories associated with a single dimension
/// within a multidimensional pivot table.
///
/// A dimension contains a collection of categories, which are the leaves in a
/// tree of groups.
///
/// (A dimension or a group can contain zero categories, but this is unusual.
/// If a dimension contains no categories, then its table cannot contain any
/// data.)
#[derive(Clone, Debug, Serialize)]
pub struct Dimension {
    /// Hierarchy of categories within the dimension.  The groups and categories
    /// are sorted in the order that should be used for display.  This might be
    /// different from the original order produced for output if the user
    /// adjusted it.
    ///
    /// The root must always be a group, although it is allowed to have no
    /// subcategories.
    root: Group,

    /// Maps from an index in presentation order to a data index ("p" to "d").
    ///
    /// This is a permutation of `0..n` where `n` is the number of leaves.
    /// Given a [Leaf] that can be found as via `dimension.nth_leaf(leaf_idx)`,
    /// the corresponding data index is `ptod[leaf_idx]`.
    ptod: Vec<usize>,

    /// Display.
    pub hide_all_labels: bool,
}

/// A vector of references to [Group]s.
///
/// Used to represent a sequence of groups along a [Path].  This is a [SmallVec]
/// because groups are usually not deeply nested.
pub type GroupVec<'a> = SmallVec<[&'a Group; 4]>;

/// A path from the root of a [Dimension] to a [Leaf].
pub struct Path<'a> {
    /// Groups along the path.
    ///
    /// There will be at least one group along any valid path, because the
    /// dimension itself is a group.
    pub groups: GroupVec<'a>,

    /// The leaf.
    ///
    /// This is a child of the last [Group].
    pub leaf: &'a Leaf,
}

impl<'a> Path<'a> {
    /// Breaks the path into a vector of [Group]s and a [Leaf].
    pub fn into_parts(self) -> (GroupVec<'a>, &'a Leaf) {
        (self.groups, self.leaf)
    }
}

/// Group indexes visited along a [Path].
pub type IndexVec = SmallVec<[usize; 4]>;

/// Indicates that the argument to [Dimension::set_ptod] was not a valid
/// permutation.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct InvalidPermutation;

impl Dimension {
    /// Constructs a new [Dimension] with the given `root`.
    pub fn new(root: Group) -> Self {
        Dimension {
            ptod: (0..root.len()).collect(),
            root,
            hide_all_labels: false,
        }
    }

    /// Returns the root [Group] of the dimension.
    pub fn root(&self) -> &Group {
        &self.root
    }

    /// Returns the presentation-to-data index mapping.
    pub fn ptod(&self) -> &[usize] {
        &self.ptod
    }

    /// Returns this dimension with its presentation-to-data index mapping
    /// replaced by `ptod`, which must be a permutation of `0..self.len()`.
    pub fn set_ptod(&mut self, ptod: Vec<usize>) -> Result<(), InvalidPermutation> {
        if ptod.len() != self.ptod.len() {
            return Err(InvalidPermutation);
        }

        let mut seen = vec![false; ptod.len()];
        for element in ptod.iter().copied() {
            if element >= ptod.len() || seen[element] {
                return Err(InvalidPermutation);
            }
            seen[element] = true;
        }

        self.ptod = ptod;
        Ok(())
    }

    /// Returns this dimension with [Dimension::hide_all_labels] set to true.
    pub fn with_all_labels_hidden(self) -> Self {
        self.with_hide_all_labels(true)
    }

    /// Returns this dimension with [Dimension::hide_all_labels] set to
    /// `hide_all_labels`.
    pub fn with_hide_all_labels(self, hide_all_labels: bool) -> Self {
        Self {
            hide_all_labels,
            ..self
        }
    }

    /// Returns true if [Dimension] has no leaf categories.
    ///
    /// A dimension without leaf categories might still contain a hierarchy of
    /// groups, just none of them with leaves.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the number of leaf categories in this dimension.
    pub fn len(&self) -> usize {
        self.root.len()
    }

    /// Returns the leaf with the given 0-based `index`, or `None` if `index >=
    /// self.len()`.
    pub fn nth_leaf(&self, index: usize) -> Option<&Leaf> {
        self.root.nth_leaf(index)
    }

    /// Returns the path to the leaf with the given 0-based `index`, or `None`
    /// if `index >= self.len()`.
    pub fn leaf_path(&self, index: usize) -> Option<Path<'_>> {
        self.root.leaf_path(index, SmallVec::new())
    }

    /// Returns the series of group child indexes followed to the leaf with the
    /// given 0-based `index`, or `None` if `index >= self.len()`.
    pub fn index_path(&self, index: usize) -> Option<IndexVec> {
        self.root.index_path(index, SmallVec::new())
    }
}

/// Specifies how to find a [Category] within a [Group].
#[derive(Copy, Clone, Debug)]
pub struct CategoryLocator {
    /// The index of the leaf to start from.
    pub leaf_index: usize,

    /// The number of times to go up a level from the leaf.  If this category is
    /// a leaf, this is 0, otherwise it is positive.
    pub level: usize,
}

impl CategoryLocator {
    /// Constructs a `CategoryLocator` for the leaf with the given 0-based
    /// index.
    pub fn new_leaf(leaf_index: usize) -> Self {
        Self {
            leaf_index,
            level: 0,
        }
    }

    /// Returns a `CategoryLocator` for the parent category of this one.
    pub fn parent(&self) -> Self {
        Self {
            leaf_index: self.leaf_index,
            level: self.level + 1,
        }
    }

    /// If this is a leaf, returns its 0-based index; otherwise, returns `None`.
    pub fn as_leaf(&self) -> Option<usize> {
        (self.level == 0).then_some(self.leaf_index)
    }
}

/// A group of categories within a dimension.
#[derive(Clone, Debug, Serialize)]
pub struct Group {
    /// Number of leaves contained by this group.
    #[serde(skip)]
    len: usize,

    /// The label displayed for the group.
    name: Box<Value>,

    /// Whether to show the label.
    pub show_label: bool,

    /// The child categories.
    ///
    /// A group usually has multiple children, but it is allowed to have
    /// only one or even (pathologically) none.
    children: Vec<Category>,
}

impl Group {
    /// Constructs a new `Group` with the given name.  The group initially has
    /// no children.
    pub fn new(name: impl Into<Value>) -> Self {
        Self::with_capacity(name, 0)
    }

    /// Returns the group's child categories.
    pub fn children(&self) -> &[Category] {
        &self.children
    }

    /// Constructs a new `Group` with the given name and initial child
    /// `capacity`.  The group initially has no children.
    pub fn with_capacity(name: impl Into<Value>, capacity: usize) -> Self {
        Self {
            len: 0,
            name: Box::new(name.into()),
            children: Vec::with_capacity(capacity),
            show_label: false,
        }
    }

    /// Appends `child` to the group's categories.
    pub fn push(&mut self, child: impl Into<Category>) {
        let mut child = child.into();
        if let Some(group) = child.as_group_mut() {
            group.show_label = true;
        }
        self.len += child.len();
        self.children.push(child);
    }

    /// Returns this group with the given `child` appended to its categories.
    pub fn with(mut self, child: impl Into<Category>) -> Self {
        self.push(child);
        self
    }

    /// Returns this group with the given `children` appended to its categories.
    pub fn with_multiple<C>(mut self, children: impl IntoIterator<Item = C>) -> Self
    where
        C: Into<Category>,
    {
        self.extend(children);
        self
    }

    /// Returns this group with `show_label` set to true.
    pub fn with_label_shown(self) -> Self {
        self.with_show_label(true)
    }

    /// Returns this group with `show_label` set as specified.
    pub fn with_show_label(mut self, show_label: bool) -> Self {
        self.show_label = show_label;
        self
    }

    /// Returns the leaf with the given 0-based `index`, or `None` if `index >=
    /// self.len()`.
    fn nth_leaf(&self, mut index: usize) -> Option<&Leaf> {
        for child in &self.children {
            let len = child.len();
            if index < len {
                return child.nth_leaf(index);
            }
            index -= len;
        }
        None
    }

    /// Returns the path to the leaf with the given 0-based `index`, or `None`
    /// if `index >= self.len()`.  `groups` must be the groups already traversed
    /// to arrive at this group.
    fn leaf_path<'a>(&'a self, mut index: usize, mut groups: GroupVec<'a>) -> Option<Path<'a>> {
        for child in &self.children {
            let len = child.len();
            if index < len {
                groups.push(self);
                return child.leaf_path(index, groups);
            }
            index -= len;
        }
        None
    }

    fn index_path(&self, mut index: usize, mut path: IndexVec) -> Option<IndexVec> {
        for (i, child) in self.children.iter().enumerate() {
            let len = child.len();
            if index < len {
                path.push(i);
                return child.index_path(index, path);
            }
            index -= len;
        }
        None
    }

    fn locator_path(&self, locator: CategoryLocator) -> Option<IndexVec> {
        let mut path = self.index_path(locator.leaf_index, IndexVec::new())?;
        path.truncate(path.len().checked_sub(locator.level)?);
        Some(path)
    }

    /// Returns the category corresponding to `locator`.  Returns `None` if
    /// `locator` is invalid (that is, if `locator.leaf_idx >= self.len` or
    /// `locator.level` is greater than the depth of the leaf) or if `locator`
    /// designates `self`.
    pub fn category(&self, locator: CategoryLocator) -> Option<&Category> {
        let path = self.locator_path(locator)?;
        let mut this = &self.children[*path.get(0)?];
        for index in path[1..].iter().copied() {
            this = &this.as_group().unwrap().children[index];
        }
        Some(this)
    }

    /// Returns the category corresponding to `locator`.  Returns `None` if
    /// `locator` is invalid (that is, if `locator.leaf_idx >= self.len` or
    /// `locator.level` is greater than the depth of the leaf) or if `locator`
    /// designates `self`.
    pub fn category_mut(&mut self, locator: CategoryLocator) -> Option<&mut Category> {
        let path = self.locator_path(locator)?;
        let mut this = &mut self.children[*path.get(0)?];
        for index in path[1..].iter().copied() {
            this = &mut this.as_group_mut().unwrap().children[index];
        }
        Some(this)
    }

    /// Returns the number of leaves contained within this group.
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns true if this group contains no leaves.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns this group's label.
    pub fn name(&self) -> &Value {
        &self.name
    }
}

impl<C> Extend<C> for Group
where
    C: Into<Category>,
{
    fn extend<T: IntoIterator<Item = C>>(&mut self, children: T) {
        let children = children.into_iter();
        self.children.reserve(children.size_hint().0);
        for child in children {
            self.push(child);
        }
    }
}

/// A collection of [Footnote]s for a [PivotTable].
///
/// Any [Value] in a pivot table can refer to a footnote.  All of the footnotes
/// used in any [Value] within a given pivot table must be collected into the
/// [Footnotes] attached to the pivot table.  (Footnotes used but not collected
/// might not display as intended, but it's not a safety issue.)
#[derive(Clone, Debug, Default, Serialize)]
pub struct Footnotes(Vec<Arc<Footnote>>);

impl Footnotes {
    /// Constructs a new, empty collection of footnotes.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the number of footnotes in the collection.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns true if the collection contains no footnotes.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Adds `footnote` to the collection.
    pub fn push(&mut self, footnote: Footnote) -> Arc<Footnote> {
        let footnote = Arc::new(footnote.with_index(self.0.len()));
        self.0.push(footnote.clone());
        footnote
    }

    /// Returns the footnote with 0-based index `index`, or `None` if `index >=
    /// self.len()`.
    pub fn get(&self, index: usize) -> Option<&Arc<Footnote>> {
        self.0.get(index)
    }
}

impl Index<usize> for Footnotes {
    type Output = Arc<Footnote>;

    fn index(&self, index: usize) -> &Self::Output {
        &self.0[index]
    }
}

impl<'a> IntoIterator for &'a Footnotes {
    type Item = &'a Arc<Footnote>;

    type IntoIter = std::slice::Iter<'a, Arc<Footnote>>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

impl FromIterator<Footnote> for Footnotes {
    fn from_iter<T: IntoIterator<Item = Footnote>>(iter: T) -> Self {
        Self(
            iter.into_iter()
                .enumerate()
                .map(|(index, footnote)| Arc::new(footnote.with_index(index)))
                .collect(),
        )
    }
}

/// A leaf category within a [Group] and ultimately within a [Dimension].
#[derive(Clone, Debug)]
pub struct Leaf {
    data_index: usize,
    name: Box<Value>,
}

impl Leaf {
    /// Constructs a new `Leaf` with the given label.
    pub fn new(name: Value) -> Self {
        Self {
            data_index: 0,
            name: Box::new(name),
        }
    }

    /// Returns the leaf's label.
    pub fn name(&self) -> &Value {
        &self.name
    }

    /// Returns a sequence of `Leaf`s for the given range, for use with
    /// [Group::with_multiple] or [Group::extend].
    ///
    /// This is useful for constructing dimensions that aren't meant to be
    /// shown.
    pub fn numbers(range: Range<usize>) -> impl Iterator<Item = Leaf> {
        range.map(|i| Self::new(Value::new_integer(Some(i as f64))))
    }
}

impl Serialize for Leaf {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.name.serialize(serializer)
    }
}

/// Pivot result classes.
///
/// Used by [PivotTable::insert_number].
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Class {
    /// An integer value.
    Integer,
    /// A correlation value.
    Correlations,
    /// A measure of significance.
    Significance,
    /// A percentage.
    Percent,
    /// A residual value.
    Residual,
    /// A count.
    ///
    /// With a weighted dataset, these are by default displayed with the weight
    /// variable's format.
    Count,
    /// A value that doesn't fit in one of the other categories.
    Other,
}

/// A leaf category or a group of them.
#[derive(Clone, Debug, Serialize)]
pub enum Category {
    /// A group.
    Group(
        /// The group.
        Group,
    ),
    /// A leaf.
    Leaf(
        /// The leaf.
        Leaf,
    ),
}

impl Category {
    /// Returns the [Group] in this `Category`, if there is one.
    pub fn as_group(&self) -> Option<&Group> {
        match self {
            Category::Group(group) => Some(group),
            Category::Leaf(_) => None,
        }
    }

    /// Returns the [Group] in this `Category`, if there is one, for
    /// modification.
    pub fn as_group_mut(&mut self) -> Option<&mut Group> {
        match self {
            Category::Group(group) => Some(group),
            Category::Leaf(_) => None,
        }
    }

    /// Returns the [Leaf] in this `Category`, if there is one.
    pub fn as_leaf(&self) -> Option<&Leaf> {
        match self {
            Category::Leaf(leaf) => Some(leaf),
            Category::Group(_) => None,
        }
    }

    /// Returns the [Leaf] in this `Category`, if there is one, for
    /// modification.
    pub fn as_leaf_mut(&mut self) -> Option<&mut Leaf> {
        match self {
            Category::Leaf(leaf) => Some(leaf),
            Category::Group(_) => None,
        }
    }

    /// Returns the category's label.
    pub fn name(&self) -> &Value {
        match self {
            Category::Group(group) => &group.name,
            Category::Leaf(leaf) => &leaf.name,
        }
    }

    /// Returns the category's label, for modification.
    pub fn name_mut(&mut self) -> &mut Value {
        match self {
            Category::Group(group) => &mut group.name,
            Category::Leaf(leaf) => &mut leaf.name,
        }
    }

    /// Returns true if this category's label should be shown.
    ///
    /// A leaf category's label is always shown, unless
    /// [Dimension::hide_all_labels] is true.
    pub fn show_label(&self) -> bool {
        match self {
            Category::Group(group) => group.show_label,
            Category::Leaf(_) => true,
        }
    }
    /// Returns true if the category contains no leaves.
    ///
    /// If this is a leaf category, this always returns false.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the number of leaves that this category contains.
    pub fn len(&self) -> usize {
        match self {
            Category::Group(group) => group.len,
            Category::Leaf(_) => 1,
        }
    }

    fn nth_leaf(&self, index: usize) -> Option<&Leaf> {
        match self {
            Category::Group(group) => group.nth_leaf(index),
            Category::Leaf(leaf) if index == 0 => Some(leaf),
            _ => None,
        }
    }

    fn leaf_path<'a>(&'a self, index: usize, groups: GroupVec<'a>) -> Option<Path<'a>> {
        match self {
            Category::Group(group) => group.leaf_path(index, groups),
            Category::Leaf(leaf) if index == 0 => Some(Path { groups, leaf }),
            _ => None,
        }
    }

    fn index_path(&self, index: usize, path: IndexVec) -> Option<IndexVec> {
        match self {
            Category::Group(group) => group.index_path(index, path),
            Category::Leaf(_) if index == 0 => Some(path),
            _ => None,
        }
    }

    fn locator_path(&self, locator: CategoryLocator) -> Option<IndexVec> {
        let mut path = self.index_path(locator.leaf_index, IndexVec::new())?;
        path.truncate(path.len().checked_sub(locator.level)?);
        Some(path)
    }

    /// Returns `None` if `locator` is invalid (that is, if `locator.leaf_idx >=
    /// self.len` or `locator.level` is greater than the depth of the leaf).
    fn category(&self, locator: CategoryLocator) -> Option<&Category> {
        let mut this = self;
        for index in this.locator_path(locator)? {
            this = &this.as_group().unwrap().children[index];
        }
        Some(this)
    }

    fn category_mut(&mut self, locator: CategoryLocator) -> Option<&mut Category> {
        let mut this = self;
        for index in this.locator_path(locator)? {
            this = &mut this.as_group_mut().unwrap().children[index];
        }
        Some(this)
    }
}

impl From<Group> for Category {
    fn from(group: Group) -> Self {
        Self::Group(group)
    }
}

impl From<Leaf> for Category {
    fn from(group: Leaf) -> Self {
        Self::Leaf(group)
    }
}

impl From<Value> for Category {
    fn from(name: Value) -> Self {
        Leaf::new(name).into()
    }
}

impl From<&Variable> for Category {
    fn from(variable: &Variable) -> Self {
        Value::new_variable(variable).into()
    }
}

impl From<&str> for Category {
    fn from(name: &str) -> Self {
        Self::Leaf(Leaf::new(Value::new_text(name)))
    }
}

impl From<String> for Category {
    fn from(name: String) -> Self {
        Self::Leaf(Leaf::new(Value::new_text(name)))
    }
}

impl From<&String> for Category {
    fn from(name: &String) -> Self {
        Self::Leaf(Leaf::new(Value::new_text(name)))
    }
}

/// An axis of a 2-dimensional table.
#[derive(Copy, Clone, Debug, Enum, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Axis2 {
    /// X axis.
    X,
    /// Y axis.
    Y,
}

impl Axis2 {
    /// Returns a map from [Axis2::X] to `x` and from [Axis2::Y] to `y`.
    pub fn new_enum<T>(x: T, y: T) -> EnumMap<Axis2, T> {
        EnumMap::from_array([x, y])
    }

    /// Returns the axis's name, in lowercase, as a static string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Axis2::X => "x",
            Axis2::Y => "y",
        }
    }
}

impl Display for Axis2 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl Not for Axis2 {
    type Output = Self;

    fn not(self) -> Self::Output {
        match self {
            Self::X => Self::Y,
            Self::Y => Self::X,
        }
    }
}

/// Error converting [Axis3::Z] to [Axis2].
#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("Can't convert `Axis3::Z` to `Axis2`.")]
pub struct ZAxis;

impl TryFrom<Axis3> for Axis2 {
    type Error = ZAxis;

    fn try_from(value: Axis3) -> Result<Self, Self::Error> {
        match value {
            Axis3::X => Ok(Axis2::X),
            Axis3::Y => Ok(Axis2::Y),
            Axis3::Z => Err(ZAxis),
        }
    }
}

/// A 2-dimensional `(x,y)` pair.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct Coord2(pub EnumMap<Axis2, isize>);

impl Coord2 {
    /// Constructs a new `Coord2` with the given `x` and `y` coordinates.
    pub fn new(x: isize, y: isize) -> Self {
        use Axis2::*;
        Self(enum_map! {
            X => x,
            Y => y
        })
    }

    /// Constructs a new `Coord2` with coordinate `az` on axis `a` and
    /// coordinate `bz` on the other axis.
    pub fn for_axis((a, az): (Axis2, isize), bz: isize) -> Self {
        let mut coord = Self::default();
        coord[a] = az;
        coord[!a] = bz;
        coord
    }

    /// Constructs a new `Coord2` with coordinates from function `f`.
    pub fn from_fn<F>(f: F) -> Self
    where
        F: FnMut(Axis2) -> isize,
    {
        Self(EnumMap::from_fn(f))
    }

    /// Returns the X coordinate.
    pub fn x(&self) -> isize {
        self.0[Axis2::X]
    }

    /// Returns the Y coordinate.
    pub fn y(&self) -> isize {
        self.0[Axis2::Y]
    }

    /// Returns the coordinate on the given `axis`.
    pub fn get(&self, axis: Axis2) -> isize {
        self.0[axis]
    }
}

impl From<EnumMap<Axis2, isize>> for Coord2 {
    fn from(value: EnumMap<Axis2, isize>) -> Self {
        Self(value)
    }
}

impl Index<Axis2> for Coord2 {
    type Output = isize;

    fn index(&self, index: Axis2) -> &Self::Output {
        &self.0[index]
    }
}

impl IndexMut<Axis2> for Coord2 {
    fn index_mut(&mut self, index: Axis2) -> &mut Self::Output {
        &mut self.0[index]
    }
}

/// A 2-dimensional rectangle.
#[derive(Clone, Debug, Default)]
pub struct Rect2(
    /// The range along each axis.
    pub EnumMap<Axis2, Range<isize>>,
);

impl Rect2 {
    /// Constructs a new `Rect2` that covers the given X and Y ranges.
    pub fn new(x_range: Range<isize>, y_range: Range<isize>) -> Self {
        Self(enum_map! {
            Axis2::X => x_range.clone(),
            Axis2::Y => y_range.clone(),
        })
    }

    /// Construct a new `Rect2` that covers `a_range` along axis `a` and
    /// `b_range` along the other axis.
    pub fn for_ranges((a, a_range): (Axis2, Range<isize>), b_range: Range<isize>) -> Self {
        let b = !a;
        let mut ranges = EnumMap::default();
        ranges[a] = a_range;
        ranges[b] = b_range;
        Self(ranges)
    }

    /// Returns the top-left corner of the rectangle.
    pub fn top_left(&self) -> Coord2 {
        use Axis2::*;
        Coord2::new(self[X].start, self[Y].start)
    }

    /// Construct a new `Rect2` that covers the ranges returned by `f`.
    pub fn from_fn<F>(f: F) -> Self
    where
        F: FnMut(Axis2) -> Range<isize>,
    {
        Self(EnumMap::from_fn(f))
    }

    /// Shifts the rectangle by `offset` along its axes.
    pub fn translate(self, offset: Coord2) -> Rect2 {
        Self::from_fn(|axis| self[axis].start + offset[axis]..self[axis].end + offset[axis])
    }

    /// Returns true if the rectangle is empty.
    pub fn is_empty(&self) -> bool {
        self[Axis2::X].is_empty() || self[Axis2::Y].is_empty()
    }
}

impl From<EnumMap<Axis2, Range<isize>>> for Rect2 {
    fn from(value: EnumMap<Axis2, Range<isize>>) -> Self {
        Self(value)
    }
}

impl Index<Axis2> for Rect2 {
    type Output = Range<isize>;

    fn index(&self, index: Axis2) -> &Self::Output {
        &self.0[index]
    }
}

impl IndexMut<Axis2> for Rect2 {
    fn index_mut(&mut self, index: Axis2) -> &mut Self::Output {
        &mut self.0[index]
    }
}

/// What to show in a footnote marker.
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum FootnoteMarkerType {
    /// a, b, c, ...
    #[default]
    Alphabetic,

    /// 1, 2, 3, ...
    Numeric,
}

/// How to show a footnote marker.
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum FootnoteMarkerPosition {
    /// Subscripts.
    Subscript,

    /// Superscripts.
    #[default]
    Superscript,
}

/// A [Look] and other styling for a [PivotTable].
#[derive(Clone, Debug, Serialize)]
pub struct PivotTableStyle {
    /// The [Look].
    ///
    /// The division between [Look] and the rest of the styling in this
    /// structure is fairly arbitrary.  The ultimate reason for the division is
    /// simply because that's how SPSS documentation and file formats do it.
    pub look: Arc<Look>,

    /// Display inner column labels as vertical text?
    pub rotate_inner_column_labels: bool,

    /// Display outer row labels as vertical text?
    pub rotate_outer_row_labels: bool,

    /// Show grid lines between data cells?
    pub show_grid_lines: bool,

    /// Display the title?
    pub show_title: bool,

    /// Display the caption?
    pub show_caption: bool,

    /// Default [Show] value for showing values in [Value]s that don't specify
    /// their own.
    ///
    /// If this is `None` then a global default is used.
    pub show_values: Option<Show>,

    /// Default [Show] value for showing variables in [Value]s that don't
    /// specify their own.
    ///
    /// If this is `None` then a global default is used.
    pub show_variables: Option<Show>,

    /// Column and row sizing and page breaks:
    ///
    /// - `sizing[Axis2::X]` is sizes for columns.
    /// - `sizing[Axis2::Y]` is sizes for rows.
    pub sizing: EnumMap<Axis2, Option<Box<Sizing>>>,

    /// Format settings.
    pub settings: FormatSettings,

    /// Numeric grouping character (usually `.` or `,`).
    pub grouping: Option<char>,

    /// The threshold for [ValueFormat::SmallE].
    pub small: f64,

    /// The format to use for weight and count variables.
    pub weight_format: Format,
}

impl Default for PivotTableStyle {
    fn default() -> Self {
        Self {
            look: Look::shared_default(),
            rotate_inner_column_labels: false,
            rotate_outer_row_labels: false,
            show_grid_lines: false,
            show_title: true,
            show_caption: true,
            show_values: None,
            show_variables: None,
            sizing: EnumMap::default(),
            settings: Default::default(), // XXX from settings
            grouping: None,
            small: 0.0001, // XXX from settings.
            weight_format: F40,
        }
    }
}

impl PivotTableStyle {
    /// Returns this style with the given `look`.
    pub fn with_look(self, look: Arc<Look>) -> Self {
        Self { look, ..self }
    }

    /// Returns this style with the given `show_values`.
    pub fn with_show_values(self, show_values: Option<Show>) -> Self {
        Self {
            show_values,
            ..self
        }
    }

    /// Returns this style with the given `show_variables`.
    pub fn with_show_variables(self, show_variables: Option<Show>) -> Self {
        Self {
            show_variables,
            ..self
        }
    }

    /// Returns this style with the given `show_title`.
    pub fn with_show_title(self, show_title: bool) -> Self {
        Self { show_title, ..self }
    }

    /// Returns this style with the given `show_caption`.
    pub fn with_show_caption(self, show_caption: bool) -> Self {
        Self {
            show_caption,
            ..self
        }
    }

    /// Returns a mutable reference to this style's [Look].
    ///
    /// This unshares the [Arc] used to refer to the [Look].
    pub fn look_mut(&mut self) -> &mut Look {
        Arc::make_mut(&mut self.look)
    }
}

/// Metadata for a [PivotTable].
#[derive(Clone, Debug, Serialize)]
pub struct PivotTableMetadata {
    /// Title.
    ///
    /// The title is displayed above the table.  Every table should have a
    /// title.
    pub title: Option<Box<Value>>,

    /// Caption.
    ///
    /// The caption is displayed below the table.  Captions are optional.
    pub caption: Option<Box<Value>>,

    /// Corner text, displayed in the top-left corner of the table.  Corner text
    /// is optional.
    pub corner_text: Option<Box<Value>>,

    /// User-specified optional notes, with special variables expanded into
    /// their values.
    pub notes: Option<String>,

    /// User-specified optional notes, with special variables left in their
    /// original forms.
    ///
    /// This allows the notes to be edited in their original form and then
    /// expanded for display.
    pub notes_unexpanded: Option<String>,

    /// The localized name of the command that produced this pivot table,
    /// e.g. `Frequencies` translated into the local language.
    pub command_local: Option<String>,

    /// The locale-invariant name of the command that produced this pivot table,
    /// e.g. `Frequencies`.
    pub command_c: Option<String>,

    /// The locale-invariant command ID for the particular kind of output that
    /// this table represents in the procedure.  This can be the same as
    /// `command_c`, e.g. `Frequencies`, or different, e.g. `Case Processing
    /// Summary`.
    ///
    /// `Notes` and `Warnings` are common generic subtypes.
    pub subtype: Option<Box<Value>>,

    /// The language used in output.
    pub language: Option<String>,

    /// A locale, including an encoding, such as `en_US.windows-1252` or
    /// `it_IT.windows-1252`.
    pub locale: Option<String>,

    /// Name of the dataset analyzed to produce the output, e.g. `DataSet1`.
    pub dataset: Option<String>,

    /// Name of the file that the dataset is from, e.g. `C:\Users\foo\bar.sav`.
    pub datafile: Option<String>,

    /// Creation date for the table.
    pub date: Option<NaiveDateTime>,
}

impl Default for PivotTableMetadata {
    fn default() -> Self {
        Self {
            title: None,
            caption: None,
            corner_text: None,
            notes: None,
            notes_unexpanded: None,
            command_local: None,
            command_c: None,
            subtype: None,
            language: None,
            locale: None,
            dataset: None,
            datafile: None,
            date: Some(Utc::now().naive_local()),
        }
    }
}

impl PivotTableMetadata {
    /// Return this metadata with the given `subtype`.
    pub fn with_subtype(self, subtype: impl Into<Value>) -> Self {
        Self {
            subtype: Some(Box::new(subtype.into())),
            ..self
        }
    }
}

/// A pivot table.
///
/// Pivot tables are PSPP's primary form of output.  They are analogous to the
/// pivot tables you might be familiar with from spreadsheets and databases.
/// See <https://en.wikipedia.org/wiki/Pivot_table> for a brief introduction to
/// the overall concept of a pivot table.
#[derive(Clone, Debug, Serialize)]
pub struct PivotTable {
    /// Style.
    pub style: PivotTableStyle,

    /// Current layer indexes, with `axes[Axis3::Z].dimensions.len()` elements.
    /// `layer[i]` is an offset into
    /// `axes[Axis3::Z].dimensions[i].data_leaves[]`, except that a dimension
    /// can have zero leaves, in which case `layer[i]` is zero and there's no
    /// corresponding leaf.
    layer: Vec<usize>,

    /// Metadata.
    pub metadata: PivotTableMetadata,

    /// Footnotes.
    pub footnotes: Footnotes,

    /// Table structure.
    #[serde(flatten)]
    structure: Structure,

    /// Data.
    cells: HashMap<usize, Value>,
}

impl Default for PivotTable {
    fn default() -> Self {
        Self {
            style: PivotTableStyle::default(),
            metadata: PivotTableMetadata::default(),
            layer: Vec::new(),
            footnotes: Footnotes::new(),
            structure: Structure::default(),
            cells: HashMap::new(),
        }
    }
}

/// The structure of a pivot table.
#[derive(Clone, Debug, Default, Serialize)]
pub struct Structure {
    /// Dimensions.
    dimensions: Vec<Dimension>,

    /// Axes.
    axes: EnumMap<Axis3, Axis>,
}

/// [Structure::new] error for a missing or duplicate dimension among the axes.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct InvalidAxes;

impl Structure {
    /// Creates a new `Structure` from the arguments.
    ///
    /// Each of the `dimensions` must appear exactly once in `axes`.
    ///
    /// Usually [Structure::from] is easier.
    pub fn new(
        dimensions: Vec<Dimension>,
        axes: EnumMap<Axis3, Axis>,
    ) -> Result<Self, InvalidAxes> {
        if dimensions.len()
            != axes
                .values()
                .map(|axis| axis.dimensions.len())
                .sum::<usize>()
        {
            return Err(InvalidAxes);
        }
        let mut seen = vec![false; dimensions.len()];
        for d in axes
            .values()
            .flat_map(|axis| axis.dimensions.iter())
            .copied()
        {
            if let Some(seen) = seen.get_mut(d)
                && !*seen
            {
                *seen = true;
            } else {
                return Err(InvalidAxes);
            }
        }
        Ok(Self { dimensions, axes })
    }

    /// Returns the number of cells in the pivot table, which is the product of
    /// the number of categories in each dimension.  (If any of the dimensions
    /// have zero categories, or if the table has no dimensions, the result is zero.)
    pub fn n_cells(&self) -> usize {
        if self.dimensions.is_empty() {
            0
        } else {
            self.dimensions.iter().map(|d| d.len()).product()
        }
    }

    /// Returns an iterator through dimensions on the given `axis`.
    pub fn axis_dimensions(
        &self,
        axis: Axis3,
    ) -> impl DoubleEndedIterator<Item = &Dimension> + ExactSizeIterator {
        self.axes[axis]
            .dimensions
            .iter()
            .copied()
            .map(|index| &self.dimensions[index])
    }

    /// Returns an iterator for all the values along `axis`.
    fn axis_values(&self, axis: Axis3) -> AxisIterator {
        AxisIterator {
            indexes: smallvec![0; self.axes[axis].dimensions.len()],
            lengths: self.axis_dimensions(axis).map(|d| d.len()).collect(),
            done: self.axis_extent(axis) == 0,
        }
    }

    /// Returns the number of categories along `axis`, that is, the product of
    /// the number of categories in all of the dimensions on `axis`.
    pub fn axis_extent(&self, axis: Axis3) -> usize {
        self.axis_dimensions(axis).map(|d| d.len()).product()
    }
}

impl<T> From<T> for Structure
where
    T: IntoIterator<Item = (Axis3, Dimension)>,
{
    fn from(value: T) -> Self {
        let mut structure = Structure::default();
        for (axis, dimension) in value {
            structure.axes[axis]
                .dimensions
                .push(structure.dimensions.len());
            structure.dimensions.push(dimension);
        }
        structure
    }
}

/// A type that can calculate the index into a [PivotTable]'s data hashmap.
pub trait CellIndex {
    /// Given the pivot table's `dimensions`, returns an index.
    fn cell_index<I>(self, dimensions: I) -> usize
    where
        I: ExactSizeIterator<Item = usize>;
}

impl<T> CellIndex for T
where
    T: AsRef<[usize]>,
{
    fn cell_index<I>(self, dimensions: I) -> usize
    where
        I: ExactSizeIterator<Item = usize>,
    {
        let data_indexes = self.as_ref();
        let mut index = 0;
        for (dimension, data_index) in dimensions.zip_eq(data_indexes.iter()) {
            debug_assert!(*data_index < dimension);
            index = dimension * index + data_index;
        }
        index
    }
}

/// A precomputed index.
pub struct PrecomputedIndex(
    /// The index.
    pub usize,
);

impl CellIndex for PrecomputedIndex {
    fn cell_index<I>(self, _dimensions: I) -> usize
    where
        I: ExactSizeIterator<Item = usize>,
    {
        self.0
    }
}

impl<C> Extend<(C, Value)> for PivotTable
where
    C: CellIndex,
{
    fn extend<T: IntoIterator<Item = (C, Value)>>(&mut self, iter: T) {
        for (cell_index, value) in iter {
            self.insert(cell_index, value);
        }
    }
}

/// A footnote in a [PivotTable].
///
/// A footnote is attached directly to a [Value], but it will only be displayed
/// correctly if it is also added to [PivotTable::footnotes] for its pivot
/// table.
#[derive(Clone, Debug, Serialize, PartialEq)]
pub struct Footnote {
    /// The index within [Footnotes].
    #[serde(skip)]
    index: usize,

    /// The footnote text.
    pub content: Box<Value>,

    /// The footnote marker.
    ///
    /// This is usually `None`, in which case [FootnoteMarkerType] determines
    /// the default marker.
    pub marker: Option<Box<Value>>,

    /// Whether to show the footnote.
    pub show: bool,
}

impl Footnote {
    /// Constructs a new footnote.
    pub fn new(content: impl Into<Value>) -> Self {
        Self {
            index: 0,
            content: Box::new(content.into()),
            marker: None,
            show: true,
        }
    }

    /// Returns the footnote with the given optional marker.
    pub fn with_marker(self, marker: Option<Value>) -> Self {
        Self {
            marker: marker.map(Box::new),
            ..self
        }
    }

    /// Returns the footnote with the given marker.
    pub fn with_some_marker(self, marker: impl Into<Value>) -> Self {
        Self::with_marker(self, Some(marker.into()))
    }

    /// Return the footnote with the given `show`.
    pub fn with_show(self, show: bool) -> Self {
        Self { show, ..self }
    }

    /// Return the footnote with the given `index`.
    pub fn with_index(self, index: usize) -> Self {
        Self { index, ..self }
    }

    /// Returns an object for formatting the footnote's marker.
    pub fn display_marker(&self, options: impl Into<ValueOptions>) -> impl Display {
        DisplayMarker {
            footnote: self,
            options: options.into(),
        }
    }

    /// Returns an object for formatting the footnote's text.
    pub fn display_content(&self, options: impl Into<ValueOptions>) -> impl Display {
        self.content.display(options)
    }

    /// Returns the footnote's index.
    pub fn index(&self) -> usize {
        self.index
    }
}

impl Default for Footnote {
    fn default() -> Self {
        Footnote::new(Value::default())
    }
}

struct DisplayMarker<'a> {
    footnote: &'a Footnote,
    options: ValueOptions,
}

impl Display for DisplayMarker<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(marker) = &self.footnote.marker {
            write!(f, "{}", marker.display(&self.options).without_suffixes())
        } else {
            let i = self.footnote.index + 1;
            match self.options.footnote_marker_type {
                FootnoteMarkerType::Alphabetic => write!(f, "{}", Display26Adic::new_lowercase(i)),
                FootnoteMarkerType::Numeric => write!(f, "{i}"),
            }
        }
    }
}

/// Displays a number in 26adic notation.
///
/// Zero is displayed as the empty string, 1 through 26 as `a` through `z`, 27
/// through 52 as `aa` through `az`, and so on.
pub struct Display26Adic {
    value: usize,
    base: u8,
}

impl Display26Adic {
    /// Constructs a `Display26Adic` for `value`, with letters in lowercase.
    pub fn new_lowercase(value: usize) -> Self {
        Self { value, base: b'a' }
    }

    /// Constructs a `Display26Adic` for `value`, with letters in uppercase.
    pub fn new_uppercase(value: usize) -> Self {
        Self { value, base: b'A' }
    }
}

impl Display for Display26Adic {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut output = SmallVec::<[u8; 16]>::new();
        let mut number = self.value;
        while number > 0 {
            number -= 1;
            let digit = (number % 26) as u8;
            output.push(digit + self.base);
            number /= 26;
        }
        output.reverse();
        write!(f, "{}", str::from_utf8(&output).unwrap())
    }
}

/// An entry in a metadata table.
pub struct MetadataEntry {
    /// The label for the entry.
    pub name: Value,

    /// The value for the entry.
    pub value: MetadataValue,
}

impl MetadataEntry {
    /// Constructs a new metadata entry with the given name and value.
    pub fn new(name: impl Into<Value>, value: MetadataValue) -> Self {
        Self {
            name: name.into(),
            value,
        }
    }

    /// Converts the metadata entry into a pivot table.
    pub fn into_pivot_table(self) -> PivotTable {
        let mut data = Vec::new();
        let group = match self.visit(&mut data) {
            Category::Group(group) => group,
            Category::Leaf(leaf) => Group::new("Metadata").with(leaf).with_label_shown(),
        };
        PivotTable::new([(Axis3::Y, Dimension::new(group))]).with_data(
            data.into_iter()
                .enumerate()
                .filter(|(_row, value)| !value.is_empty())
                .map(|(row, value)| ([row], value)),
        )
    }
    fn visit(self, data: &mut Vec<Value>) -> Category {
        match self.value {
            MetadataValue::Leaf(value) => {
                data.push(value);
                Leaf::new(self.name).into()
            }
            MetadataValue::Group(items) => Group::with_capacity(self.name, items.len())
                .with_multiple(items.into_iter().map(|item| item.visit(data)))
                .into(),
        }
    }
}

/// A value in a metadata table.
pub enum MetadataValue {
    /// A value.
    Leaf(
        /// The value.
        Value,
    ),
    /// A nested group of entries.
    Group(
        /// The entries.
        Vec<MetadataEntry>,
    ),
}

impl MetadataValue {
    /// Construct a new "leaf" metadata value.
    pub fn new_leaf(value: impl Into<Value>) -> Self {
        Self::Leaf(value.into())
    }
}

impl Serialize for MetadataValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            MetadataValue::Leaf(value) => value.serialize_bare(serializer),
            MetadataValue::Group(items) => {
                let mut map = serializer.serialize_map(Some(items.len()))?;
                for item in items {
                    let name = item.name.display(()).to_string();
                    map.serialize_entry(&name, &item.value)?;
                }
                map.end()
            }
        }
    }
}
impl Serialize for MetadataEntry {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match &self.value {
            MetadataValue::Leaf(value) => {
                let mut map = serializer.serialize_map(Some(1))?;
                let name = self.name.display(()).to_string();
                map.serialize_entry(&name, &BareValue(value))?;
                map.end()
            }
            MetadataValue::Group(items) => {
                let mut map = serializer.serialize_map(Some(items.len()))?;
                for item in items {
                    let name = item.name.display(()).to_string();
                    map.serialize_entry(&name, &item.value)?;
                }
                map.end()
            }
        }
    }
}

#[cfg(test)]
mod test {
    use std::str::FromStr;

    use crate::output::pivot::{
        Display26Adic, MetadataEntry, MetadataValue,
        look::Color,
        tests::assert_rendering,
        value::{TemplateValue, Value, ValueInner},
    };

    #[test]
    fn parse_color() {
        assert_eq!(Color::from_str("red"), Ok(Color::new(255, 0, 0)));
        assert_eq!(Color::from_str("transparent"), Ok(Color::TRANSPARENT));
        assert_eq!(Color::from_str("rgb(12,34,56)"), Ok(Color::new(12, 34, 56)));
        assert_eq!(Color::from_str("#abcdef"), Ok(Color::new(0xab, 0xcd, 0xef)));
        assert_eq!(Color::from_str("abcdef"), Ok(Color::new(0xab, 0xcd, 0xef)));
        assert_eq!(Color::from_str("transparent"), Ok(Color::TRANSPARENT));
    }

    #[test]
    fn display_26adic() {
        for (number, lowercase, uppercase) in [
            (0, "", ""),
            (1, "a", "A"),
            (2, "b", "B"),
            (26, "z", "Z"),
            (27, "aa", "AA"),
            (28, "ab", "AB"),
            (29, "ac", "AC"),
            (18278, "zzz", "ZZZ"),
            (18279, "aaaa", "AAAA"),
            (19010, "abcd", "ABCD"),
        ] {
            assert_eq!(Display26Adic::new_lowercase(number).to_string(), lowercase);
            assert_eq!(Display26Adic::new_uppercase(number).to_string(), uppercase);
        }
    }

    #[test]
    fn template() {
        for (template, expected) in [
            (
                "1: [:^1,:]1; [:^1,:]2",
                "1: First,1.00,Second,2,; Third,3.00,Fourth,4,",
            ),
            (r#"2: [:^1\n:]1"#, "2: First\n1.00\nSecond\n2\n"),
            (r#"3: [:^1 = ^2\n:]1"#, "3: First = 1.00\nSecond = 2\n"),
            ("4: [%1:, ^1:]1", "4: First, 1.00, Second, 2"),
            ("5: [%1 = %2:, ^1 = ^2:]1", "5: First = 1.00, Second = 2"),
            ("6: [%1:, ^1:]1", "6: First, 1.00, Second, 2"),
            ("7: [%1:, ^1: and $1]1", "7: First, 1.00, Second and 2"),
            ("8: [%1:, ^1: and $1]3", "8: One and two"),
            ("9: [%1:, ^1: and $1]4", "9: Just one"),
        ] {
            let value = Value::new(ValueInner::Template(TemplateValue {
                args: vec![
                    vec![
                        Value::new_user_text("First"),
                        Value::new_number(Some(1.0)),
                        Value::new_user_text("Second"),
                        Value::new_integer(Some(2.0)),
                    ],
                    vec![
                        Value::new_user_text("Third"),
                        Value::new_number(Some(3.0)),
                        Value::new_user_text("Fourth"),
                        Value::new_integer(Some(4.0)),
                    ],
                    vec![Value::new_user_text("One"), Value::new_user_text("two")],
                    vec![Value::new_user_text("Just one")],
                ],
                localized: String::from(template),
                id: None,
            }));
            assert_eq!(value.display(()).to_string(), expected);
        }
    }

    #[test]
    fn metadata_entry() {
        let tree = MetadataEntry {
            name: Value::from("Group"),
            value: MetadataValue::Group(vec![
                MetadataEntry {
                    name: Value::from("Name 1"),
                    value: MetadataValue::Leaf(Value::from("Value 1")),
                },
                MetadataEntry {
                    name: Value::from("Subgroup 1"),
                    value: MetadataValue::Group(vec![
                        MetadataEntry {
                            name: Value::from("Subname 1"),
                            value: MetadataValue::Leaf(Value::from("Subvalue 1")),
                        },
                        MetadataEntry {
                            name: Value::from("Subname 2"),
                            value: MetadataValue::Leaf(Value::from("Subvalue 2")),
                        },
                        MetadataEntry {
                            name: Value::from("Subname 3"),
                            value: MetadataValue::Leaf(Value::new_integer(Some(3.0))),
                        },
                    ]),
                },
                MetadataEntry {
                    name: Value::from("Name 2"),
                    value: MetadataValue::Leaf(Value::from("Value 2")),
                },
            ]),
        };
        assert_eq!(
            serde_json::to_string_pretty(&tree).unwrap(),
            r#"{
  "Name 1": "Value 1",
  "Subgroup 1": {
    "Subname 1": "Subvalue 1",
    "Subname 2": "Subvalue 2",
    "Subname 3": 3
  },
  "Name 2": "Value 2"
}"#
        );

        assert_rendering("metadata_entry", &tree.into_pivot_table());
    }
}