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
// 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/>.

//! Data cell contents.
//!
//! This module contains [Value], which is the contents of a single pivot table
//! cell, plus what it in turn contains.

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

use crate::{
    calendar::{date_time_to_pspp, time_to_pspp},
    data::{ByteString, Datum, EncodedString, WithEncoding},
    format::{self, DATETIME40_0, Decimal, F8_2, F40, Format, TIME40_0, Type, UncheckedFormat},
    output::pivot::{
        Footnote, FootnoteMarkerType,
        look::{CellStyle, FontStyle},
    },
    settings::{Settings, Show},
    spv::html::Markup,
    variable::{VarType, Variable},
};
use chrono::{NaiveDateTime, NaiveTime};
use itertools::Itertools;
use serde::{
    Serialize, Serializer,
    ser::{SerializeMap, SerializeStruct},
};
use std::{
    borrow::Borrow,
    fmt::{Debug, Display, Write},
    iter::{once, repeat},
    sync::Arc,
};

/// The content of a single pivot table cell.
///
/// A [Value] is also a pivot table's title, caption, footnote marker and
/// contents, and so on.
#[derive(Clone, Default, PartialEq)]
pub struct Value {
    /// Content.
    pub inner: ValueInner,

    /// Optional styling.
    pub styling: Option<Box<ValueStyle>>,
}

impl Serialize for Value {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.inner.serialize(serializer)
    }
}
/// Wrapper for [Value] that serializes in a plain way:
///
/// - Numbers: The number.
/// - Strings: The string.
/// - Variables: The variable name.
/// - Text: The localized text string.
/// - Markup: A string containing HTML for the markup.
/// - Template: The formatted template string.
/// - Empty: `()`.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct BareValue<T>(pub T);
impl<T> Serialize for BareValue<T>
where
    T: Borrow<Value>,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let value = self.0.borrow();
        match &value.inner {
            ValueInner::Datum(datum_value) => datum_value.serialize_bare(serializer),
            ValueInner::Variable(variable_value) => variable_value.var_name.serialize(serializer),
            ValueInner::Text(text_value) => text_value.localized.serialize(serializer),
            ValueInner::Markup(markup) => markup.serialize(serializer),
            ValueInner::Template(_) => value.display(()).to_string().serialize(serializer),
            ValueInner::Empty => ().serialize(serializer),
        }
    }
}
impl Value {
    /// Constructs a new `Value`, initially with no styling.
    ///
    /// Usually one of the other constructors is more convenient.
    pub fn new(inner: ValueInner) -> Self {
        Self {
            inner,
            styling: None,
        }
    }

    /// Constructs a new `Value` from `number` with a default [F8_2] format.
    /// Some related useful methods are:
    ///
    /// - [with_source_variable], to add information about the variable that the
    ///   datum came from (or use [new_datum_from_variable] as a shortcut to
    ///   combine both).
    ///
    /// - [with_format] to override the default format.
    ///
    /// [with_source_variable]: Self::with_source_variable
    /// [new_datum_from_variable]: Self::new_datum_from_variable
    /// [with_format]: Self::with_format
    pub fn new_number(number: Option<f64>) -> Self {
        Self::new(ValueInner::Datum(DatumValue::new_number(number)))
    }

    /// Construct a new `Value` from `number` with format [F40].
    pub fn new_integer(x: Option<f64>) -> Self {
        Self::new_number(x).with_format(F40)
    }

    /// Constructs a new `Value` as a number whose value is `date_time`, which
    /// is converted to the [PSPP date representation](crate::calendar), with
    /// format [DATETIME40_0].
    pub fn new_date(date_time: NaiveDateTime) -> Self {
        Self::new_number(Some(date_time_to_pspp(date_time))).with_format(DATETIME40_0)
    }

    /// Constructs a new `Value` as a number whose value is `time`, which is
    /// converted to the [PSPP time representation](crate::calendar), with
    /// format [TIME40_0].
    pub fn new_time(time: NaiveTime) -> Self {
        Self::new_number(Some(time_to_pspp(time))).with_format(TIME40_0)
    }

    /// Constructs a new `Value` from localizable text string `s`.
    ///
    /// PSPP doesn't support internationalization yet, so this does the same
    /// thing as [new_user_text] for now.
    ///
    /// [new_user_text]: Self::new_user_text
    pub fn new_text(s: impl Into<String>) -> Self {
        Self::new_user_text(s)
    }

    /// Constructs a new `Value` from localizable text string `localized`,
    /// English string `c`, and identifier `id`.  If the string came from the
    /// user, `user_provided` should be true.
    pub fn new_general_text(localized: String, c: String, id: String, user_provided: bool) -> Self {
        Self::new(ValueInner::Text(TextValue {
            user_provided,
            c: (c != localized).then_some(c),
            id: (id != localized).then_some(id),
            localized,
        }))
    }

    /// Constructs a new `Value` from `markup`.
    pub fn new_markup(markup: Markup) -> Self {
        Self::new(ValueInner::Markup(markup))
    }

    /// Constructs a new text `Value` from `s`, which should have been provided
    /// by the user.
    pub fn new_user_text(s: impl Into<String>) -> Self {
        Self::new(ValueInner::new_user_text(s))
    }

    /// Constructs a new `Value` from `variable`.
    pub fn new_variable(variable: &Variable) -> Self {
        Self::new(ValueInner::Variable(VariableValue {
            show: None,
            var_name: String::from(variable.name.as_str()),
            variable_label: variable.label.clone(),
        }))
    }

    /// Constructs a new `Value` from `datum` with a default format.  Some
    /// related useful methods are:
    ///
    /// - [with_source_variable], to add information about the variable that the
    ///   datum came from (or use [new_datum_from_variable] as a shortcut to
    ///   combine both).
    ///
    /// - [with_format] to override the default format.
    ///
    /// [with_source_variable]: Self::with_source_variable
    /// [new_datum_from_variable]: Self::new_datum_from_variable
    /// [with_format]: Self::with_format
    pub fn new_datum<B>(datum: &Datum<B>) -> Self
    where
        B: EncodedString,
    {
        Self::new(ValueInner::Datum(DatumValue::new(datum)))
    }

    /// Construct a new, empty `Value`.
    pub const fn new_empty() -> Self {
        // Can't use `Self::default()` because that is non-const.
        Value {
            inner: ValueInner::Empty,
            styling: None,
        }
    }

    /// Returns a reference to a statically allocated empty `Value`.
    pub const fn static_empty() -> &'static Self {
        static EMPTY: Value = Value::new_empty();
        &EMPTY
    }

    /// Returns true if this `Value` is empty and unstyled.
    pub const fn is_empty(&self) -> bool {
        self.inner.is_empty() && self.styling.is_none()
    }

    /// Returns this value with its value label, format, and variable name from
    /// `variable`.
    pub fn with_source_variable(self, variable: &Variable) -> Self {
        let value_label = self
            .datum()
            .and_then(|datum| variable.value_labels.get(&datum).map(String::from));
        self.with_value_label(value_label)
            .with_format(variable.print_format)
            .with_variable_name(Some(variable.name.as_str().into()))
    }

    /// Returns this value with its display format set to `format`, if it is a
    /// [DatumValue].
    pub fn with_format(self, format: impl Into<ValueFormat>) -> Self {
        Self {
            inner: self.inner.with_format(format),
            ..self
        }
    }

    /// Construct a new `Value` from `datum`, which is a value of `variable`.
    pub fn new_datum_from_variable(datum: &Datum<ByteString>, variable: &Variable) -> Self {
        Self::new_datum(&datum.as_encoded(variable.encoding())).with_source_variable(variable)
    }

    /// Returns the inner [Datum], if this value is a [DatumValue].
    pub fn datum(&self) -> Option<&Datum<WithEncoding<ByteString>>> {
        self.inner.datum()
    }

    /// Returns this `Value` with the added `footnote`.
    pub fn with_footnote(mut self, footnote: &Arc<Footnote>) -> Self {
        self.add_footnote(footnote);
        self
    }

    /// Returns this `Value` with the added `footnotes`.
    pub fn with_footnotes<'a>(
        mut self,
        footnotes: impl IntoIterator<Item = &'a Arc<Footnote>>,
    ) -> Self {
        for footnote in footnotes {
            self.add_footnote(footnote);
        }
        self
    }

    /// Adds `footnote` to this `Value`.
    pub fn add_footnote(&mut self, footnote: &Arc<Footnote>) {
        let footnotes = &mut self.styling_mut().footnotes;
        footnotes.push(footnote.clone());
        footnotes.sort_by_key(|f| f.index);
    }

    /// Removes all of the footnotes from `value`.
    pub fn clear_footnotes(&mut self) {
        if let Some(styling) = &mut self.styling
            && !styling.footnotes.is_empty()
        {
            styling.footnotes.clear();
            if styling.is_empty() {
                self.styling = None;
            }
        }
    }

    /// Returns this `Value` with the added `subscripts`.
    pub fn with_subscripts<'a>(
        mut self,
        subscripts: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.styling_mut()
            .subscripts
            .extend(subscripts.into_iter().map(|s| s.into()));
        self
    }

    /// Returns this `Value` with the added `subscript`.
    pub fn with_subscript(mut self, subscript: impl Into<String>) -> Self {
        self.add_subscript(subscript);
        self
    }

    /// Adds `subscript` to this `Value`.
    pub fn add_subscript(&mut self, subscript: impl Into<String>) {
        self.styling_mut().subscripts.push(subscript.into());
    }

    /// Returns this `Value` with `show` as the [Show] setting for value labels,
    /// if this is a [DatumValue].
    pub fn with_show_value_label(mut self, show: Option<Show>) -> Self {
        if let Some(datum_value) = self.inner.as_datum_value_mut() {
            datum_value.show = show;
        }
        self
    }

    /// Returns this `Value` with `value_label` as the value label, if this is a
    /// [DatumValue].
    ///
    /// Use [with_source_variable], instead, to automatically add a value label
    /// and other information from a source variable.
    ///
    /// [with_source_variable]: Self::with_source_variable
    pub fn with_value_label(mut self, value_label: Option<String>) -> Self {
        if let Some(datum_value) = self.inner.as_datum_value_mut() {
            datum_value.value_label = value_label.clone()
        }
        self
    }

    /// Returns this `Value` with `variable_name` as the variable's name, if
    /// this is a [DatumValue].
    ///
    /// Use [with_source_variable], instead, to automatically add a variable
    /// name and other information from a source variable.
    ///
    /// [with_source_variable]: Self::with_source_variable
    pub fn with_variable_name(mut self, variable_name: Option<String>) -> Self {
        if let Some(datum_value) = self.inner.as_datum_value_mut() {
            datum_value.variable = variable_name.clone()
        }
        self
    }

    /// Returns this `Value` with `show` as the [Show] setting for variable
    /// labels, if this is a [VariableValue].
    pub fn with_show_variable_label(mut self, show: Option<Show>) -> Self {
        if let ValueInner::Variable(variable_value) = &mut self.inner {
            variable_value.show = show;
        }
        self
    }

    /// Returns this `Value` with the specified `font_style`.
    pub fn with_font_style(mut self, font_style: FontStyle) -> Self {
        self.set_font_style(font_style);
        self
    }

    /// Sets the `Value`'s font style.
    pub fn set_font_style(&mut self, font_style: FontStyle) {
        self.styling_mut().font_style = Some(font_style);
    }

    /// Returns this `Value` with the specified `cell_style`.
    pub fn with_cell_style(mut self, cell_style: CellStyle) -> Self {
        self.set_cell_style(cell_style);
        self
    }

    /// Sets the `Value`'s cell style.
    pub fn set_cell_style(&mut self, cell_style: CellStyle) {
        self.styling_mut().cell_style = Some(cell_style);
    }

    /// Returns this `Value` with the specified `styling`.
    pub fn with_styling(self, styling: Option<Box<ValueStyle>>) -> Self {
        Self { styling, ..self }
    }

    /// Returns the styling for this `Value` for modification.
    ///
    /// If this `Value` doesn't have styling yet, this creates it.
    pub fn styling_mut(&mut self) -> &mut ValueStyle {
        self.styling.get_or_insert_default()
    }

    /// Returns this `Value`'s font style, if it has one.
    pub fn font_style(&self) -> Option<&FontStyle> {
        self.styling
            .as_ref()
            .map(|styling| styling.font_style.as_ref())
            .flatten()
    }

    /// Returns this `Value`'s cell style, if it has one.
    pub fn cell_style(&self) -> Option<&CellStyle> {
        self.styling
            .as_ref()
            .map(|styling| styling.cell_style.as_ref())
            .flatten()
    }

    /// Returns this `Value`'s subscripts.
    pub fn subscripts(&self) -> &[String] {
        self.styling
            .as_ref()
            .map_or(&[], |styling| &styling.subscripts)
    }

    /// Returns this `Value`'s footnotes.
    pub fn footnotes(&self) -> &[Arc<Footnote>] {
        self.styling
            .as_ref()
            .map_or(&[], |styling| &styling.footnotes)
    }

    /// Returns an object that will format this value, including subscripts and
    /// superscripts and footnotes.  `options` controls whether variable and
    /// value labels are included.
    pub fn display(&self, options: impl Into<ValueOptions>) -> DisplayValue<'_> {
        let display = self.inner.display(options);
        match &self.styling {
            Some(styling) => display.with_styling(styling),
            None => display,
        }
    }

    /// Serializes this value in a plain way, like [BareValue].  This function
    /// can be used on a field as `#[serde(serialize_with =
    /// Value::serialize_bare)]`.
    pub fn serialize_bare<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        BareValue(self).serialize(serializer)
    }
}

impl From<&str> for Value {
    fn from(value: &str) -> Self {
        Self::new_text(value)
    }
}

impl From<String> for Value {
    fn from(value: String) -> Self {
        Self::new_text(value)
    }
}

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

/// Helper struct for printing a [Value] with `format!` and `{}`.
///
/// Create this struct with [Value::display].
#[derive(Clone, Debug)]
pub struct DisplayValue<'a> {
    inner: &'a ValueInner,
    subscripts: &'a [String],
    footnotes: &'a [Arc<Footnote>],
    options: ValueOptions,
    show_value: bool,
    show_label: Option<&'a str>,
}

impl<'a> DisplayValue<'a> {
    /// Returns the subscripts to be displayed, as an iterator of `&str`.
    pub fn subscripts(&self) -> impl Iterator<Item = &str> + ExactSizeIterator + Clone {
        self.subscripts.iter().map(String::as_str)
    }

    /// Returns true if the value to be displayed includes subscripts.
    pub fn has_subscripts(&self) -> bool {
        !self.subscripts.is_empty()
    }

    /// Returns the footnotes to be displayed, as an iterator.
    ///
    /// The iterator can have fewer elements than there are footnotes, because
    /// footnotes can be hidden.
    pub fn footnotes(&self) -> impl Iterator<Item = impl Display> + Clone {
        self.footnotes
            .iter()
            .filter(|f| f.show)
            .map(|f| f.display_marker(self.options.clone()))
    }

    /// Returns true if there are footnotes to be displayed.
    ///
    /// Because footnotes can be hidden, this method can return false for values
    /// with footnotes.
    pub fn has_footnotes(&self) -> bool {
        self.footnotes().next().is_some()
    }

    /// Returns this [DisplayValue] modified so that it won't show any
    /// subscripts or footnotes.
    pub fn without_suffixes(self) -> Self {
        Self {
            subscripts: &[],
            footnotes: &[],
            ..self
        }
    }

    /// Returns this [DisplayValue] modified so that it will only show the
    /// suffixes and footnotes, not the body.
    pub fn without_body(self) -> Self {
        Self {
            inner: &ValueInner::Empty,
            ..self
        }
    }

    /// Returns the [Markup] to be formatted, if any.
    pub fn markup(&self) -> Option<&Markup> {
        self.inner.as_markup()
    }

    /// Returns this display split into `(body, suffixes)` where `suffixes` is
    /// subscripts and footnotes and `body` is everything else.
    pub fn split(self) -> (Self, Self) {
        (self.clone().without_suffixes(), self.without_body())
    }

    /// Returns this display with subscripts and footnotes taken from `styling`.
    ///
    /// (This display can't use the other parts of `styling`, since we're just
    /// formatting plain text.)
    pub fn with_styling(mut self, styling: &'a ValueStyle) -> Self {
        self.subscripts = styling.subscripts.as_slice();
        self.footnotes = styling.footnotes.as_slice();
        self
    }

    /// Returns this display with the given `subscripts.`
    pub fn with_subscripts(self, subscripts: &'a [String]) -> Self {
        Self { subscripts, ..self }
    }

    /// Returns this display with the given `footnotes.`
    pub fn with_footnotes(self, footnotes: &'a [Arc<Footnote>]) -> Self {
        Self { footnotes, ..self }
    }

    /// Returns true if this display will format to the empty string.
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty() && self.subscripts.is_empty() && self.footnotes.is_empty()
    }

    /// Returns the character that the formatted value would use for a decimal
    /// point if it has one, or `None` if the value isn't a datum value and
    /// therefore doesn't have a decimal point.
    pub fn decimal(&self) -> Option<Decimal> {
        self.inner
            .as_datum_value()
            .map(|datum_value| datum_value.decimal())
    }

    fn small(&self) -> f64 {
        self.options.small
    }

    /// Returns a variable type for the value to be displayed.
    ///
    /// We consider a numeric value displayed by itself to be numeric, but if
    /// the value label is displayed then it is considered to be a string.
    /// Anything else is also a string.
    ///
    /// This is useful for passing to [HorzAlign::for_mixed], although maybe
    /// this method should just return [HorzAlign] directly.
    ///
    /// [HorzAlign]: crate::output::pivot::look::HorzAlign
    /// [HorzAlign::for_mixed]: crate::output::pivot::look::HorzAlign::for_mixed
    pub fn var_type(&self) -> VarType {
        if let Some(datum_value) = self.inner.as_datum_value()
            && datum_value.datum.is_number()
            && self.show_label.is_none()
        {
            VarType::Numeric
        } else {
            VarType::String
        }
    }
}

impl Display for DisplayValue<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.inner {
            ValueInner::Datum(datum_value) => datum_value.display(self, f),
            ValueInner::Variable(variable_value) => variable_value.display(self, f),
            ValueInner::Markup(markup) => write!(f, "{markup}"),
            ValueInner::Text(text_value) => write!(f, "{text_value}"),
            ValueInner::Template(template_value) => template_value.display(self, f),
            ValueInner::Empty => Ok(()),
        }?;

        for (subscript, delimiter) in self.subscripts.iter().zip(once('_').chain(repeat(','))) {
            write!(f, "{delimiter}{subscript}")?;
        }

        if !self.footnotes.is_empty() {
            write!(
                f,
                "[{}]",
                self.footnotes
                    .iter()
                    .map(|f| f.display_marker(&self.options))
                    .format(",")
            )?;
        }

        Ok(())
    }
}

impl Debug for Value {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let name = match &self.inner {
            ValueInner::Datum(_) => "Datum",
            ValueInner::Variable(_) => "Variable",
            ValueInner::Text(_) => "Text",
            ValueInner::Markup(_) => "Markup",
            ValueInner::Template(_) => "Template",
            ValueInner::Empty => "Empty",
        };
        write!(f, "{name}:{:?}", self.display(()).to_string())?;
        if let Some(markup) = self.inner.as_markup() {
            write!(f, " (markup: {markup:?})")?;
        }
        if let Some(styling) = &self.styling {
            write!(f, " ({styling:?})")?;
        }
        Ok(())
    }
}

/// A [Format] inside a [Value].
///
/// Most `Value`s contain ordinary [Format]s, but occasionally one will have a
/// special format that is like [Type::F] except that nonzero numbers with
/// magnitude below a small threshold are instead shown in scientific notation.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ValueFormat {
    /// Any ordinary format.
    Other(Format),

    /// Displays numbers smaller than [PivotTableStyle::small] in scientific
    /// notation, and otherwise in the enclosed format (which should be
    /// [Type::F] format).
    ///
    /// [PivotTableStyle::small]: super::PivotTableStyle::small
    SmallE(Format),
}

impl ValueFormat {
    /// Returns the inner [Format].
    pub fn inner(&self) -> Format {
        match self {
            ValueFormat::Other(format) => *format,
            ValueFormat::SmallE(format) => *format,
        }
    }

    /// Returns this format as applied to the given `number` with `small` as the
    /// threshold.
    pub fn apply(&self, number: Option<f64>, small: f64) -> Format {
        if let ValueFormat::SmallE(format) = self
            && let Some(number) = number
            && number != 0.0
            && number.abs() < small
        {
            UncheckedFormat::new(Type::E, 40, format.d() as u8).fix()
        } else {
            self.inner()
        }
    }

    /// Returns true if this is [ValueFormat::SmallE].
    pub fn is_small_e(&self) -> bool {
        matches!(self, ValueFormat::SmallE(_))
    }
}

impl From<Format> for ValueFormat {
    fn from(format: Format) -> Self {
        Self::Other(format)
    }
}

impl Serialize for ValueFormat {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            ValueFormat::Other(format) => format.serialize(serializer),
            ValueFormat::SmallE(format) => {
                #[derive(Serialize)]
                struct SmallE(Format);
                SmallE(*format).serialize(serializer)
            }
        }
    }
}

/// A datum and how to display it.
#[derive(Clone, Debug, PartialEq)]
pub struct DatumValue {
    /// The datum.
    pub datum: Datum<WithEncoding<ByteString>>,

    /// The display format.
    pub format: ValueFormat,

    /// Whether to show `value` or `value_label` or both.
    ///
    /// If this is unset, then a higher-level default is used.
    pub show: Option<Show>,

    /// The name of the variable that `value` came from, if any.
    pub variable: Option<String>,

    /// The value label associated with `value`, if any.
    pub value_label: Option<String>,
}

impl Serialize for DatumValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        if let ValueFormat::Other(format) = self.format
            && format.type_() == Type::F
            && self.variable.is_none()
            && self.value_label.is_none()
        {
            self.datum.serialize(serializer)
        } else {
            let mut s = serializer.serialize_map(None)?;
            s.serialize_entry("datum", &self.datum)?;
            s.serialize_entry("format", &self.format)?;
            if let Some(show) = self.show {
                s.serialize_entry("show", &show)?;
            }
            if let Some(variable) = &self.variable {
                s.serialize_entry("variable", variable)?;
            }
            if let Some(value_label) = &self.value_label {
                s.serialize_entry("value_label", value_label)?;
            }
            s.end()
        }
    }
}

impl DatumValue {
    /// Constructs a new `DatumValue` for `datum`.
    pub fn new<B>(datum: &Datum<B>) -> Self
    where
        B: EncodedString,
    {
        Self {
            datum: datum.cloned(),
            format: ValueFormat::Other(F8_2),
            show: None,
            variable: None,
            value_label: None,
        }
    }

    /// Constructs a new `DatumValue` for `number`.
    pub fn new_number(number: Option<f64>) -> Self {
        Self::new(&Datum::<&str>::Number(number))
    }

    /// Returns this `DatumValue` with the given `format`.
    pub fn with_format(self, format: ValueFormat) -> Self {
        Self { format, ..self }
    }

    /// Writes this value to `f` using the settings in `display`.
    pub fn display<'a>(
        &self,
        display: &DisplayValue<'a>,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        if display.show_value {
            match &self.datum {
                Datum::Number(number) => {
                    let format = self.format.apply(*number, display.small());
                    self.datum
                        .display(format)
                        .with_settings(&display.options.settings)
                        .without_leading_spaces()
                        .fmt(f)?;
                }
                Datum::String(s) => {
                    if self.format.inner().type_() == Type::AHex {
                        write!(f, "{}", s.inner.display_hex())?;
                    } else {
                        f.write_str(&s.as_str())?;
                    }
                }
            }
        }
        if let Some(label) = display.show_label {
            if display.show_value {
                f.write_char(' ')?;
            }
            f.write_str(label)?;
        }
        Ok(())
    }

    /// Returns the decimal point used in the formatted value, if any.
    pub fn decimal(&self) -> Decimal {
        self.datum.display(self.format.inner()).decimal()
    }

    /// Serializes this value to `serializer` in the "bare" manner described for
    /// [BareValue].
    pub fn serialize_bare<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if let Datum::Number(Some(number)) = &self.datum
            && let number = *number
            && number.trunc() == number
            && number >= -(1i64 << 53) as f64
            && number <= (1i64 << 53) as f64
        {
            Some(number as u64).serialize(serializer)
        } else {
            self.datum.serialize(serializer)
        }
    }
}

/// A variable name.
#[derive(Clone, Debug, Serialize, PartialEq)]
pub struct VariableValue {
    /// Variable name.
    pub var_name: String,

    /// Variable label, if any.
    pub variable_label: Option<String>,

    /// Whether to show `var_name` or `variable_label` or both.
    ///
    /// If this is unset, then a higher-level default is used.
    pub show: Option<Show>,
}

impl VariableValue {
    fn display(&self, display: &DisplayValue<'_>, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        if display.show_value {
            f.write_str(&self.var_name)?;
        }
        if let Some(label) = display.show_label {
            if display.show_value {
                f.write_char(' ')?;
            }
            f.write_str(label)?;
        }
        Ok(())
    }
}

/// A text string.
///
/// A `TextValue` is used for text within a table, such as a title, a column or
/// row heading, or a footnote.  (String data values are better represented as
/// [DatumValue].)
#[derive(Clone, Debug, PartialEq)]
pub struct TextValue {
    /// Whether the text came from the user.
    ///
    /// PSPP can localize text that it writes itself, but not text provided by
    /// the user.
    pub user_provided: bool,

    /// Localized.
    ///
    /// This is the main output string.
    pub localized: String,

    /// English version of the string.
    ///
    /// Only for strings that are not user-provided, and only if it is different
    /// from `localized`.
    pub c: Option<String>,

    /// Identifier.
    ///
    /// Only for strings that are not user-provided, and only if it is different
    /// from `localized`.
    pub id: Option<String>,
}

impl Serialize for TextValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        if self.user_provided && self.c.is_none() && self.id.is_none() {
            serializer.serialize_str(&self.localized)
        } else {
            let mut s = serializer.serialize_struct(
                "TextValue",
                2 + self.c.is_some() as usize + self.id.is_some() as usize,
            )?;
            s.serialize_field("user_provided", &self.user_provided)?;
            s.serialize_field("localized", &self.localized)?;
            if let Some(c) = &self.c {
                s.serialize_field("c", &c)?;
            }
            if let Some(id) = &self.id {
                s.serialize_field("id", &id)?;
            }
            s.end()
        }
    }
}

impl Display for TextValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.localized)
    }
}

impl TextValue {
    /// Returns the localized version of this `TextValue`.
    pub fn localized(&self) -> &str {
        self.localized.as_str()
    }

    /// Returns the English version of this `TextValue`.
    pub fn c(&self) -> &str {
        self.c.as_ref().unwrap_or(&self.localized).as_str()
    }

    /// Returns an identifier for this `TextValue`.
    pub fn id(&self) -> &str {
        self.id.as_ref().unwrap_or(&self.localized).as_str()
    }
}

/// A template with substitutions.
#[derive(Clone, Debug, Serialize, PartialEq)]
pub struct TemplateValue {
    /// Template string.
    ///
    /// The documentation for [Value] in the PSPP manual describes the template
    /// syntax.
    ///
    /// [Value]: https://pspp.benpfaff.org/manual/spv/light-detail.html#value
    pub localized: String,

    /// Arguments to the template string.
    pub args: Vec<Vec<Value>>,

    /// Optional identifier for the template.
    pub id: Option<String>,
}

impl TemplateValue {
    fn display<'a>(
        &self,
        display: &DisplayValue<'a>,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        #[derive(Copy, Clone, Debug)]
        struct InnerTemplate<'b> {
            template: &'b str,
            escape: char,
        }

        impl<'b> InnerTemplate<'b> {
            fn new(template: &'b str, escape: char) -> Self {
                Self { template, escape }
            }

            fn extract(input: &'b str, escape: char, end: char) -> (Self, &'b str) {
                let mut prev = None;
                for (index, c) in input.char_indices() {
                    if c == end && prev != Some('\\') {
                        return (Self::new(&input[..index], escape), &input[index + 1..]);
                    }
                    prev = Some(c);
                }
                (Self::new(input, escape), "")
            }

            fn expand(
                &self,
                options: &ValueOptions,
                f: &mut std::fmt::Formatter<'_>,
                args: &mut std::slice::Iter<Value>,
            ) -> Result<usize, std::fmt::Error> {
                let mut iter = self.template.chars();

                // Always consume at least 1 argument to avoid infinite loop.
                let mut args_consumed = 1;

                while let Some(c) = iter.next() {
                    match c {
                        '\\' => {
                            let c = iter.next().unwrap_or('\\') as char;
                            let c = if c == 'n' { '\n' } else { c };
                            write!(f, "{c}")?;
                        }
                        c if c == self.escape => {
                            let (index, rest) = consume_int(iter.as_str());
                            iter = rest.chars();
                            if let Some(index) = index.checked_sub(1)
                                && let Some(arg) = args.as_slice().get(index)
                            {
                                args_consumed = args_consumed.max(index + 1);
                                write!(f, "{}", arg.display(options))?;
                            }
                        }
                        c => write!(f, "{c}")?,
                    }
                }
                for _ in 0..args_consumed {
                    args.next();
                }
                Ok(args_consumed)
            }
        }

        fn consume_int(input: &str) -> (usize, &str) {
            let mut n = 0;
            for (index, c) in input.char_indices() {
                match c.to_digit(10) {
                    Some(digit) => n = n * 10 + digit as usize,
                    None => return (n, &input[index..]),
                }
            }
            (n, "")
        }

        // Arguments are formatted without leading zeros for `PCT` and `DOLLAR`.
        // (I don't know why.)
        let mut options = display.options.clone();
        options.settings.leading_zero_pct = false;

        let mut iter = self.localized.chars();
        while let Some(c) = iter.next() {
            match c {
                '\\' => {
                    let c = match iter.next() {
                        None => '\\',
                        Some('n') => '\n',
                        Some(c) => c,
                    };
                    f.write_char(c)?;
                }
                '^' => {
                    let (index, rest) = consume_int(iter.as_str());
                    if let Some(index) = index.checked_sub(1)
                        && let Some(arg) = self.args.get(index)
                        && let Some(arg) = arg.first()
                    {
                        write!(f, "{}", arg.display(&options))?;
                    }
                    iter = rest.chars();
                }
                '[' => {
                    let (a, rest) = InnerTemplate::extract(iter.as_str(), '%', ':');
                    let (b, rest) = InnerTemplate::extract(rest, '^', ':');
                    let (c, rest) = InnerTemplate::extract(rest, '$', ']');
                    let (index, rest) = consume_int(rest);
                    iter = rest.chars();

                    let (first, mid, last) = if a.template.is_empty() {
                        (b, b, b)
                    } else if c.template.is_empty() {
                        (a, b, b)
                    } else {
                        (a, b, c)
                    };
                    if let Some(index) = index.checked_sub(1)
                        && let Some(args) = self.args.get(index)
                    {
                        let mut args = args.iter();
                        let n = first.expand(&options, f, &mut args)?;
                        while args.len() > n {
                            mid.expand(&options, f, &mut args)?;
                        }
                        if args.len() > 0 {
                            last.expand(&options, f, &mut args)?;
                        }
                    }
                }
                c => f.write_char(c)?,
            }
        }
        Ok(())
    }
}

/// Possible content for a [Value].
#[derive(Clone, Debug, Default, Serialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ValueInner {
    /// A [Datum] value.
    Datum(
        /// The datum.
        DatumValue,
    ),
    /// A variable name.
    Variable(
        /// The variable.
        VariableValue,
    ),
    /// Plain text.
    Text(
        /// The text.
        TextValue,
    ),
    /// Rich text.
    Markup(
        /// The rich text.
        Markup,
    ),
    /// A template with substitutions.
    Template(
        /// The template.
        TemplateValue,
    ),
    /// An empty value.
    #[default]
    Empty,
}

impl ValueInner {
    /// Returns true if this is a [ValueInner::Empty].
    pub const fn is_empty(&self) -> bool {
        matches!(self, Self::Empty)
    }

    /// Returns this value with its display format set to `format`, if it is a
    /// [DatumValue].
    pub fn with_format(mut self, format: impl Into<ValueFormat>) -> Self {
        if let Some(datum_value) = self.as_datum_value_mut() {
            datum_value.format = format.into();
        }
        self
    }

    /// Returns the [Datum] inside this value, if it is a [DatumValue].
    pub fn datum(&self) -> Option<&Datum<WithEncoding<ByteString>>> {
        self.as_datum_value().map(|d| &d.datum)
    }

    /// Returns the [Show] value inside this value, if it has one, or [None]
    /// otherwise.
    fn show(&self) -> Option<Show> {
        match self {
            ValueInner::Datum(DatumValue { show, .. })
            | ValueInner::Variable(VariableValue { show, .. }) => *show,
            _ => None,
        }
    }

    /// Returns the value label or variable label inside this value, if it has
    /// one.
    pub fn label(&self) -> Option<&str> {
        self.value_label().or_else(|| self.variable_label())
    }

    /// Returns the value label inside this value, if it has one.
    fn value_label(&self) -> Option<&str> {
        self.as_datum_value()
            .and_then(|d| d.value_label.as_ref().map(String::as_str))
    }

    /// Returns the variable label inside this value, if it has one.
    fn variable_label(&self) -> Option<&str> {
        self.as_variable_value()
            .and_then(|d| d.variable_label.as_ref().map(String::as_str))
    }

    /// Returns the [DatumValue] inside this value, if it is
    /// [ValueInner::Datum].
    pub fn as_datum_value(&self) -> Option<&DatumValue> {
        match self {
            ValueInner::Datum(datum) => Some(datum),
            _ => None,
        }
    }

    /// Returns the [DatumValue] inside this value, mutably, if it is
    /// [ValueInner::Datum].
    pub fn as_datum_value_mut(&mut self) -> Option<&mut DatumValue> {
        match self {
            ValueInner::Datum(datum) => Some(datum),
            _ => None,
        }
    }

    /// Returns the [VariableValue] inside this value, if it is
    /// [ValueInner::Variable].
    pub fn as_variable_value(&self) -> Option<&VariableValue> {
        match self {
            ValueInner::Variable(variable) => Some(variable),
            _ => None,
        }
    }

    /// Returns the [VariableValue] inside this value, mutably, if it is
    /// [ValueInner::Variable].
    pub fn as_variable_value_mut(&mut self) -> Option<&mut VariableValue> {
        match self {
            ValueInner::Variable(variable) => Some(variable),
            _ => None,
        }
    }

    /// Returns the [Markup] inside this value, if it is [ValueInner::Markup].
    fn as_markup(&self) -> Option<&Markup> {
        match self {
            ValueInner::Markup(markup) => Some(markup),
            _ => None,
        }
    }

    /// Returns an object that will format this value.  Settings on `options`
    /// control whether variable and value labels are included.
    pub fn display(&self, options: impl Into<ValueOptions>) -> DisplayValue<'_> {
        fn interpret_show(
            global_show: impl Fn() -> Show,
            table_show: Option<Show>,
            value_show: Option<Show>,
            label: &str,
        ) -> (bool, Option<&str>) {
            match value_show.or(table_show).unwrap_or_else(global_show) {
                Show::Value => (true, None),
                Show::Label => (false, Some(label)),
                Show::Both => (true, Some(label)),
            }
        }

        let options = options.into();
        let (show_value, show_label) = if let Some(value_label) = self.value_label() {
            interpret_show(
                || Settings::global().show_values,
                options.show_values,
                self.show(),
                value_label,
            )
        } else if let Some(variable_label) = self.variable_label() {
            interpret_show(
                || Settings::global().show_variables,
                options.show_variables,
                self.show(),
                variable_label,
            )
        } else {
            (true, None)
        };
        DisplayValue {
            inner: self,
            subscripts: &[],
            footnotes: &[],
            options,
            show_value,
            show_label,
        }
    }

    /// Constructs a new text `ValueInner` from `s`, which should have been
    /// provided by the user.
    pub fn new_user_text(s: impl Into<String>) -> Self {
        let s: String = s.into();
        if !s.is_empty() {
            Self::Text(TextValue {
                user_provided: true,
                localized: s,
                c: None,
                id: None,
            })
        } else {
            Self::Empty
        }
    }
}

/// Styling inside a [Value].
///
/// Most [Value]s use a default style, so this is a separate [Box]ed structure
/// to save memory.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ValueStyle {
    /// Cell style.
    pub cell_style: Option<CellStyle>,

    /// Font style.
    pub font_style: Option<FontStyle>,

    /// Subscripts.
    pub subscripts: Vec<String>,

    /// Footnotes.
    pub footnotes: Vec<Arc<Footnote>>,
}

impl ValueStyle {
    /// Returns true if this [ValueStyle] is empty.
    ///
    /// This will return false if the font style exists but is the default font
    /// style, and similarly for the cell style.
    pub fn is_empty(&self) -> bool {
        self.font_style.is_none()
            && self.cell_style.is_none()
            && self.subscripts.is_empty()
            && self.footnotes.is_empty()
    }
}

/// Options for displaying a [Value].
#[derive(Clone, Debug)]
pub struct ValueOptions {
    /// Whether to show values or value labels, or both.
    ///
    /// When this is `None`, a global default is used.
    pub show_values: Option<Show>,

    /// Whether to show variable names or variable labels, or both.
    ///
    /// When this is `None`, a global default is used.
    pub show_variables: Option<Show>,

    /// Numbers whose magnitudes are less than this value are displayed in
    /// scientific notation.  A value of 0 disables this feature.
    pub small: f64,

    /// Where to put the footnote markers.
    pub footnote_marker_type: FootnoteMarkerType,

    /// Settings for formatting [Datum]s.
    pub settings: format::Settings,
}

impl Default for ValueOptions {
    fn default() -> Self {
        Self {
            show_values: None,
            show_variables: None,
            small: 0.0001,
            footnote_marker_type: FootnoteMarkerType::default(),
            settings: Settings::global().formats.clone(),
        }
    }
}

impl From<()> for ValueOptions {
    fn from(_: ()) -> Self {
        ValueOptions::default()
    }
}

impl From<&ValueOptions> for ValueOptions {
    fn from(value: &ValueOptions) -> Self {
        value.clone()
    }
}