techy 0.2.0

A fast, extensible LaTeX parser for Rust
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
//! This module defines the error types of serialization. [`SerializeError`] is the
//! error of writing objects, [`DeserializeError`] the error of reading them back, and
//! [`RegistrationError`] the error of setting up a session. [`SerialValueError`] is the
//! error of converting plain data to or from a
//! [`SerialValue`](crate::serialize::SerialValue).

use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::sync::Arc;
use core::fmt;

use crate::engine::DescentLimit;
use crate::error::InconsistentDiagnosticCounts;
use crate::scopes::DefinitionKey;

use super::value::TableId;

/// The shared error value behind the `cause` field of [`SerializeError::Failed`] and
/// [`DeserializeError::Failed`]: an `Arc` (the shape of
/// [`HookFailed::cause`](crate::error::HookFailed::cause)) so that the error types stay
/// `Clone`.
type SharedCause = Arc<dyn core::error::Error + Send + Sync + 'static>;

/// Renders a source in error messages by its optional origin label: `source ‘label’`, or
/// `a source without origin label`.
struct OriginLabel<'a>(&'a Option<String>);

impl fmt::Display for OriginLabel<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0 {
            Some(label) => write!(f, "source ‘{label}’"),
            None => f.write_str("a source without origin label"),
        }
    }
}

/// The error that the write side of serialization returns.
///
/// Every operation that serializes objects returns this type: the
/// [`serialize_object`](crate::serialize::SerializableObject::serialize_object) method of
/// a serializable object, the
/// [`serialize_argument_spec`](crate::core::specs::CallableSpec::serialize_argument_spec)
/// method of a callable spec, an [`ObjectSerdeDriver`](crate::serialize::ObjectSerdeDriver),
/// and the methods that intern objects into a session
/// ([`SerdeSession::intern`](crate::serialize::SerdeSession::intern),
/// [`SerializeContext::intern`](crate::serialize::SerializeContext::intern)).
///
/// Most variants report that the program has not set up what the serialization needs,
/// for instance a table that is not registered or a spec without a provenance stamp.
/// Other variants report objects that cannot be serialized, for instance objects that
/// refer to one another in a cycle. An implementation of `serialize_object` or of a
/// driver reports a failure of its own as [`Failed`](SerializeError::Failed).
///
/// The variants [`InTable`](SerializeError::InTable) and
/// [`InNode`](SerializeError::InNode) record where an error occurred: in which table, and
/// at which node of a tree. Their `cause` field contains the error itself.
///
/// The read side of serialization reports [`DeserializeError`]. The guide chapter
/// [Serializing parses](crate::guide::serialize) introduces both sides.
///
/// The type does not implement `PartialEq`, because a
/// [`Failed`](SerializeError::Failed) value can contain an arbitrary underlying error.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum SerializeError {
    /// The type of the object does not support serialization.
    ///
    /// The default implementation of
    /// [`serialize_object`](crate::serialize::SerializableObject::serialize_object)
    /// returns this error. A type that must implement the trait but is never serialized
    /// can therefore use an empty `impl` block. Implement the method to make the type
    /// serializable.
    Unsupported,
    /// A parsed argument has an argument spec that its callable spec does not declare at
    /// the index of the argument, and the callable spec cannot serialize such an argument
    /// spec.
    ///
    /// The default implementation of
    /// [`serialize_argument_spec`](crate::core::specs::CallableSpec::serialize_argument_spec)
    /// serializes the argument spec of the argument at index `i` as the index `i`: this
    /// argument must have the `i`-th argument spec that the callable spec declares. An
    /// argument spec that the parser supplies for one invocation alone is an
    /// *out-of-band* argument spec. To serialize out-of-band argument specs, a callable
    /// spec overrides `serialize_argument_spec` and serializes a description of the
    /// argument spec.
    ///
    /// The message of this error numbers the arguments from 1.
    ArgumentSpecOutOfBand {
        /// The index of the parsed argument in invocation order, counted from 0.
        index: usize,
        /// The number of argument specs that the callable spec declares.
        count: usize,
    },
    /// An implementation failed to serialize an object, for a reason of its own.
    ///
    /// A [`serialize_object`](crate::serialize::SerializableObject::serialize_object)
    /// method or a driver reports this error. `detail` describes the failure, and `cause`
    /// contains the underlying error if there is one;
    /// [`Error::source`](core::error::Error::source) returns it.
    ///
    /// Create the value with [`failed`](SerializeError::failed) and
    /// [`with_cause`](SerializeError::with_cause).
    Failed {
        /// The description of the failure, as a message of its own. The message of this
        /// error contains it after `Serialization failed:`.
        detail: String,
        /// The underlying error, if any. It is stored in an `Arc`, so that the error
        /// type stays `Clone`.
        cause: Option<SharedCause>,
    },
    /// A value cannot be converted to the value model of serialization.
    ///
    /// The [`SerialValueError`] tells what is wrong, for instance an integer outside the
    /// range of `i64`, a floating-point number, or a map key that is not a string. The
    /// session also reports this error, with
    /// [`NestingTooDeep`](SerialValueError::NestingTooDeep), when the serialized entry of
    /// an object is nested more deeply than
    /// [`SerialValue::MAX_NESTING_DEPTH`](crate::serialize::SerialValue::MAX_NESTING_DEPTH)
    /// allows.
    Value(SerialValueError),
    /// A table handle does not refer to a table of the session that it is used with.
    ///
    /// A [`TableHandle`](crate::serialize::TableHandle) is valid only for the session that
    /// registered its table. This error occurs when the handle comes from another session,
    /// or when the session has a table at the position of the handle, but with a driver
    /// of another type.
    UnknownTable {
        /// The table identifier of the handle.
        table: TableId,
    },
    /// The session has no table of the requested name with the expected driver type.
    ///
    /// The accessors of techy's standard tables (see
    /// [`StandardTableInterning`](crate::serialize::StandardTableInterning)) look up their
    /// table by name. For these accessors, the error means that the session was created
    /// with [`SerdeSession::empty`](crate::serialize::SerdeSession::empty) and not with
    /// [`SerdeSession::new`](crate::serialize::SerdeSession::new), or that a driver of
    /// another type is registered under the name.
    UnknownTableName {
        /// The requested table name.
        name: String,
    },
    /// The table already has the maximum number of entries (`u32::MAX`), and no further
    /// object can be interned into it.
    TableFull {
        /// The name of the table.
        table: &'static str,
    },
    /// A table position is beyond the end of its table.
    ///
    /// [`take_segment_with_main`](crate::serialize::SerdeSession::take_segment_with_main)
    /// reports this error when its main entry does not exist in the session. No segment
    /// is emitted in this case.
    IndexOutOfRange {
        /// The name of the table.
        table: &'static str,
        /// The position, counted from 0.
        index: u32,
        /// The number of entries in the table.
        len: u32,
    },
    /// The objects being serialized refer to one another in a cycle.
    ///
    /// While the session serialized an object of table `referrer`, this object interned
    /// an object of table `table` whose serialization was still in progress. Serialized
    /// references cannot form a cycle: the read side rebuilds each object from the
    /// objects that it refers to.
    ReferenceCycle {
        /// The table of the object that was interned again.
        table: &'static str,
        /// The table of the object that interned it.
        referrer: &'static str,
    },
    /// The objects refer to one another more deeply than the descent limit of the
    /// session allows.
    ///
    /// The serialization stops with this error. The limit is set with
    /// [`SerdeSession::with_descent_guard_init`](crate::serialize::SerdeSession::with_descent_guard_init).
    DescentLimitExceeded {
        /// The description of the limit that was reached, as the descent guard reports
        /// it.
        detail: String,
        /// The limit that was reached (see
        /// [`DescentRefusal::limit`](crate::core::DescentRefusal::limit)).
        limit: DescentLimit,
    },
    /// A driver produced an entry with an identifier other than the identifier of its
    /// table.
    ///
    /// A table whose objects are all of one kind declares their identifier with the
    /// [`homogeneous_identifier`](crate::serialize::ObjectSerdeDriver::homogeneous_identifier)
    /// method of its driver. Every entry that the driver produces must have this
    /// identifier. This error therefore indicates a bug in the driver.
    UnexpectedIdentifier {
        /// The name of the table.
        table: &'static str,
        /// The identifier that every entry of the table must have.
        expected: &'static str,
        /// The identifier of the entry.
        found: String,
    },
    /// A spec that can only be serialized by identity has no provenance stamp.
    ///
    /// Some spec types have no self-contained serialized form. A spec of such a type is
    /// serialized as a reference to its definition in the provider that defines it. This
    /// reference is taken from the provenance stamp of the spec
    /// ([`SpecProvenance`](crate::core::specs::SpecProvenance)).
    ///
    /// Among techy's providers, a package created with
    /// [`Package::new_shared`](crate::core::specs::Package::new_shared) issues provenance
    /// stamps. A custom provider can create them with
    /// [`SpecProvenance::new`](crate::core::specs::SpecProvenance::new). In a shared
    /// package, obtain the stamp of a spec with
    /// [`Package::provenance_for`](crate::core::specs::Package::provenance_for) and attach
    /// it with the `with_provenance` method of the spec before you insert the spec. You
    /// can also insert the spec with a method that stamps it, such as
    /// [`Package::define_macro`](crate::core::specs::Package::define_macro).
    MissingProvenance {
        /// The name of the type of the spec.
        spec: &'static str,
    },
    /// The provider that defined a spec no longer exists, so the identity of the spec
    /// cannot be serialized.
    ///
    /// A provenance stamp refers to its provider through a weak reference. This error
    /// occurs when all strong references to the provider have been dropped. Keep the
    /// package that defines a spec alive as long as objects that refer to the spec are
    /// serialized.
    ProviderDropped {
        /// The callable type that the spec was defined under, in its `Debug` form.
        callable_type: String,
        /// The key that the spec was defined under.
        key: DefinitionKey,
    },
    /// An error that occurred while the session serialized an object into the table
    /// `table`.
    ///
    /// The session wraps every error of a driver in this variant, and `cause` contains the
    /// error. Only the innermost table is recorded: `cause` is never an `InTable` itself.
    /// It can be an [`InNode`](SerializeError::InNode) whose own `cause` is the `InTable`
    /// of another table, for instance when a tree node interned its spec and the
    /// serialization of the spec failed.
    InTable {
        /// The name of the table.
        table: &'static str,
        /// The error.
        cause: Box<SerializeError>,
    },
    /// An error that occurred while the tree driver serialized the node `node` of a tree:
    /// its payload, its argument specs, or an object that it interned.
    ///
    /// The tree driver ([`TreeSerdeDriver`](crate::serialize::TreeSerdeDriver)) wraps
    /// every error at a node in this variant, and the session wraps the result in the
    /// [`InTable`](SerializeError::InTable) of the trees table. Only the innermost node is
    /// recorded: `cause` is never an `InNode` itself.
    InNode {
        /// The position of the node in the storage order of the tree, with the root first.
        node: u32,
        /// The invocation name of the node, if the node is a callable.
        callable: Option<String>,
        /// The error.
        cause: Box<SerializeError>,
    },
}

impl SerializeError {
    /// The [`Unsupported`](SerializeError::Unsupported) error: the type does not support
    /// serialization.
    ///
    /// This is what the default
    /// [`serialize_object`](crate::serialize::SerializableObject::serialize_object) returns.
    pub fn unsupported() -> SerializeError {
        SerializeError::Unsupported
    }

    /// An implementation's own failure ([`Failed`](SerializeError::Failed)) with the given
    /// description and no underlying error.
    ///
    /// Attach one with [`with_cause`](SerializeError::with_cause).
    pub fn failed(detail: impl Into<String>) -> SerializeError {
        SerializeError::Failed { detail: detail.into(), cause: None }
    }

    /// Attach the underlying error to a [`Failed`](SerializeError::Failed); any other variant
    /// is returned unchanged.
    ///
    /// The error is stored in an `Arc`, so that the value stays `Clone`, and is reachable
    /// through [`Error::source`](core::error::Error::source).
    pub fn with_cause(self, cause: impl core::error::Error + Send + Sync + 'static) -> SerializeError {
        match self {
            SerializeError::Failed { detail, .. } => {
                SerializeError::Failed { detail, cause: Some(Arc::new(cause)) }
            }
            other => other,
        }
    }

    /// Wrap a driver's failure with the table it happened in — unless it already
    /// carries a table location (only the innermost is kept).
    pub(crate) fn in_table(self, table: &'static str) -> SerializeError {
        match self {
            located @ SerializeError::InTable { .. } => located,
            cause => SerializeError::InTable { table, cause: Box::new(cause) },
        }
    }

    /// Wrap a per-node failure of the tree driver with the node it happened at —
    /// unless it already carries a node location (only the innermost is kept).
    pub(crate) fn in_node(self, node: u32, callable: Option<String>) -> SerializeError {
        match self {
            located @ SerializeError::InNode { .. } => located,
            cause => SerializeError::InNode { node, callable, cause: Box::new(cause) },
        }
    }
}

impl From<SerialValueError> for SerializeError {
    fn from(error: SerialValueError) -> SerializeError {
        SerializeError::Value(error)
    }
}

impl fmt::Display for SerializeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SerializeError::Unsupported => {
                write!(f, "Serialization is not supported by the type of this object")
            }
            SerializeError::ArgumentSpecOutOfBand { index, count } => write!(
                f,
                "Unsupported out-of-band argument spec of argument #{} (the callable spec \
                 declares {} argument specs); override \
                 ‘serialize_argument_spec’ in the callable spec",
                index.saturating_add(1),
                count
            ),
            SerializeError::Failed { detail, .. } => {
                write!(f, "Serialization failed: {detail}")
            }
            SerializeError::Value(error) => {
                write!(f, "Failed to convert a value to the serialized value model: {error}")
            }
            SerializeError::UnknownTable { table } => write!(
                f,
                "Unknown table #{} in this session: the table handle comes from another \
                 session, or the table has a driver of another type",
                table.ordinal()
            ),
            SerializeError::UnknownTableName { name } => write!(
                f,
                "Missing table ‘{name}’: no table of this name with the expected \
                 driver type is registered in this session"
            ),
            SerializeError::TableFull { table } => {
                write!(f, "Table ‘{table}’ is full (u32::MAX entries)")
            }
            SerializeError::IndexOutOfRange { table, index, len } => write!(
                f,
                "Position #{index} is out of range for table ‘{table}’ ({len} entries)"
            ),
            SerializeError::ReferenceCycle { table, referrer } => write!(
                f,
                "Reference cycle: an object of table ‘{table}’ refers back to \
                 itself (interned again by an object of table ‘{referrer}’)"
            ),
            SerializeError::DescentLimitExceeded { detail, .. } => {
                write!(f, "Objects nested too deeply for serialization: {detail}")
            }
            SerializeError::UnexpectedIdentifier { table, expected, found } => write!(
                f,
                "Misconfigured driver of table ‘{table}’: the driver produced an \
                 entry with identifier ‘{found}’ (expected ‘{expected}’)"
            ),
            SerializeError::MissingProvenance { spec } => write!(
                f,
                "Missing provenance stamp on a spec of type ‘{spec}’"
            ),
            SerializeError::ProviderDropped { callable_type, key } => write!(
                f,
                "Missing definition provider of the spec with {key} (callable \
                 type ‘{callable_type}’)"
            ),
            SerializeError::InTable { table, cause } => {
                write!(f, "Failed to serialize an object into table ‘{table}’: {cause}")
            }
            SerializeError::InNode { node, callable, cause } => match callable {
                Some(callable) => write!(
                    f,
                    "Failed to serialize node #{node} (callable ‘{callable}’) of a \
                     tree: {cause}"
                ),
                None => write!(f, "Failed to serialize node #{node} of a tree: {cause}"),
            },
        }
    }
}

impl core::error::Error for SerializeError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            SerializeError::Failed { cause, .. } => {
                cause.as_ref().map(|cause| &**cause as &(dyn core::error::Error + 'static))
            }
            SerializeError::Value(error) => Some(error),
            SerializeError::InTable { cause, .. } | SerializeError::InNode { cause, .. } => Some(&**cause),
            _ => None,
        }
    }
}

/// The error that the read side of serialization returns.
///
/// Every operation that reads serialized data returns this type: the
/// [`deserialize_object`](crate::serialize::DeserializableObject::deserialize_object)
/// function of a deserializable type, the
/// [`deserialize_argument_spec`](crate::core::specs::CallableSpec::deserialize_argument_spec)
/// method of a callable spec, an [`ObjectSerdeDriver`](crate::serialize::ObjectSerdeDriver),
/// an [`IdentifierResolver`](crate::serialize::IdentifierResolver), and the methods that
/// read data from a session
/// ([`SerdeSession::push_segment`](crate::serialize::SerdeSession::push_segment),
/// [`SerdeSession::object`](crate::serialize::SerdeSession::object),
/// [`DeserializeContext::object`](crate::serialize::DeserializeContext::object)).
///
/// The read side treats serialized data as untrusted input. Malformed data, for instance
/// a value of the wrong kind, a position beyond the end of a table, a reference cycle or
/// an unknown identifier, is reported as an error and never causes a panic.
///
/// Some variants report that the data was written by a program that does not match the
/// reading program:
///
/// - another version of techy
///   ([`UnsupportedVersion`](DeserializeError::UnsupportedVersion));
/// - another configuration ([`ProfileMismatch`](DeserializeError::ProfileMismatch));
/// - another language ([`FeatureAbsent`](DeserializeError::FeatureAbsent));
/// - another set of packages ([`MissingProvider`](DeserializeError::MissingProvider),
///   [`MissingDefinition`](DeserializeError::MissingDefinition)).
///
/// Other variants report that the reading program has not registered or supplied what
/// the data needs ([`UnknownIdentifier`](DeserializeError::UnknownIdentifier),
/// [`UnknownTableName`](DeserializeError::UnknownTableName),
/// [`NoSourceTextSupplier`](DeserializeError::NoSourceTextSupplier)).
///
/// The variants [`InEntry`](DeserializeError::InEntry) and
/// [`InNode`](DeserializeError::InNode) record where an error occurred: in which entry of
/// which table, and at which node of a tree. Their `cause` field contains the error
/// itself.
///
/// The write side of serialization reports [`SerializeError`]. The guide chapter
/// [Serializing parses](crate::guide::serialize) introduces both sides.
///
/// The type does not implement `PartialEq`, because a
/// [`Failed`](DeserializeError::Failed) value can contain an arbitrary underlying error.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum DeserializeError {
    /// A serialized argument refers to an argument spec that the callable spec of the
    /// reading program does not declare.
    ///
    /// The argument at index `index` is serialized as a reference to the `index`-th
    /// argument spec of its callable spec. The callable spec that was rebuilt or looked
    /// up by the reading program declares only `count` argument specs: it is not the
    /// callable spec that the data was written with.
    ///
    /// The message of this error numbers the arguments from 1.
    ArgumentIndexOutOfRange {
        /// The index of the serialized argument in invocation order, counted from 0.
        index: usize,
        /// The number of argument specs that the callable spec declares.
        count: usize,
    },
    /// A serialized argument contains a description of its argument spec, and the
    /// callable spec of the reading program does not read such descriptions.
    ///
    /// A callable spec that overrides
    /// [`serialize_argument_spec`](crate::core::specs::CallableSpec::serialize_argument_spec)
    /// writes such a description. The callable spec that reads the argument uses the
    /// default implementation of
    /// [`deserialize_argument_spec`](crate::core::specs::CallableSpec::deserialize_argument_spec),
    /// which reads only the index of a declared argument spec. The callable spec of the
    /// reading program is therefore not of the type that wrote the argument.
    ///
    /// The message of this error numbers the arguments from 1.
    UnexpectedArgumentSpecPayload {
        /// The index of the serialized argument in invocation order, counted from 0.
        index: usize,
    },
    /// An implementation failed to rebuild an object, for a reason of its own.
    ///
    /// A
    /// [`deserialize_object`](crate::serialize::DeserializableObject::deserialize_object)
    /// function, a driver or a resolver reports this error, for instance when the reading
    /// program lacks an object that the data refers to. `detail` describes the failure,
    /// and `cause` contains the underlying error if there is one;
    /// [`Error::source`](core::error::Error::source) returns it.
    ///
    /// Create the value with [`failed`](DeserializeError::failed) and
    /// [`with_cause`](DeserializeError::with_cause).
    Failed {
        /// The description of the failure, as a message of its own. The message of this
        /// error contains it after `Deserialization failed:`.
        detail: String,
        /// The underlying error, if any. It is stored in an `Arc`, so that the error
        /// type stays `Clone`.
        cause: Option<SharedCause>,
    },
    /// A serialized value has the wrong form.
    ///
    /// The [`SerialValueError`] tells what is wrong, for instance a value of the wrong
    /// kind, a missing, unknown or repeated map key, an integer out of range, an unknown
    /// enum variant, or a value nested too deeply.
    Value(SerialValueError),
    /// A table handle does not refer to a table of the session that it is used with.
    ///
    /// A [`TableHandle`](crate::serialize::TableHandle) is valid only for the session that
    /// registered its table. This error occurs when the handle comes from another session,
    /// or when the session has a table at the position of the handle, but with a driver
    /// of another type.
    UnknownTable {
        /// The table identifier of the handle.
        table: TableId,
    },
    /// A reference refers to a position beyond the end of its table.
    IndexOutOfRange {
        /// The name of the table.
        table: &'static str,
        /// The position that the reference refers to, counted from 0.
        index: u32,
        /// The number of entries in the table.
        len: u32,
    },
    /// A typed table position was read through the handle of another table.
    ///
    /// A typed position ([`SerialIndex`](crate::serialize::SerialIndex)) contains the
    /// identifier of its table in the session that created the position. In a session
    /// that registered its tables in another order, this identifier refers to another
    /// table.
    WrongTable {
        /// The name of the table of the handle.
        expected: &'static str,
        /// The table identifier that the position contains.
        found: TableId,
    },
    /// A tree was read with an annotation type other than the one that it was serialized
    /// with.
    ///
    /// The trees table stores each tree under the identifier that is registered for the
    /// annotation type of the tree, for instance `core.tree` for the unit annotation. A
    /// stored tree can be read back only with this annotation type.
    /// [`TreeSerialization::tree`](crate::serialize::TreeSerialization::tree) returns
    /// this error when its type argument differs from the annotation type of the stored
    /// tree. Reading a serialized parse result returns this error, wrapped in
    /// [`InEntry`](DeserializeError::InEntry), when the parse result refers to a tree
    /// whose annotation type is not the unit annotation.
    ///
    /// A program that reads trees of several annotation types can use `stored` to choose
    /// the annotation type, and read the tree again with this type.
    AnnotationTypeMismatch {
        /// The identifier under which the annotation type of the stored tree is
        /// registered, or `None` if no identifier is registered for this type.
        stored: Option<String>,
        /// The Rust type name of the requested annotation type, as returned by
        /// [`core::any::type_name`].
        requested: &'static str,
        /// The identifier under which the requested annotation type is registered, or
        /// `None` if the requested annotation type is not registered.
        requested_identifier: Option<String>,
    },
    /// The references between the serialized entries form a cycle.
    ///
    /// While the session deserialized entry `referrer_index` of table `referrer_table`,
    /// this entry read entry `index` of table `table`, whose deserialization was still in
    /// progress. A cycle cannot be rebuilt: the read side rebuilds each object from the
    /// objects that it refers to.
    ReferenceCycle {
        /// The table of the entry that was read again.
        table: &'static str,
        /// The position of the entry that was read again.
        index: u32,
        /// The table of the entry that read it.
        referrer_table: &'static str,
        /// The position of the entry that read it.
        referrer_index: u32,
    },
    /// The entries refer to one another more deeply than the descent limit of the
    /// session allows.
    ///
    /// The deserialization stops with this error. The limit is set with
    /// [`SerdeSession::with_descent_guard_init`](crate::serialize::SerdeSession::with_descent_guard_init).
    DescentLimitExceeded {
        /// The description of the limit that was reached, as the descent guard reports
        /// it.
        detail: String,
        /// The limit that was reached (see
        /// [`DescentRefusal::limit`](crate::core::DescentRefusal::limit)).
        limit: DescentLimit,
    },
    /// The reading session has no reader for the identifier of an entry.
    ///
    /// No reader is registered for `identifier` in the table `table`, and no resolver
    /// ([`TableHandle::register_resolver`](crate::serialize::TableHandle::register_resolver))
    /// provided one. Register the type that reads such entries with
    /// [`TableHandle::register_type`](crate::serialize::TableHandle::register_type), or,
    /// for the annotations of a tree, with
    /// [`TableHandle::register_annotation`](crate::serialize::TableHandle::register_annotation).
    /// [`latexlike::serialize::register`](crate::latexlike::serialize::register) registers
    /// the readers of all identifiers of techy and of the `latexlike` preset.
    UnknownIdentifier {
        /// The name of the table.
        table: &'static str,
        /// The identifier of the entry.
        identifier: String,
    },
    /// The segment has a format version that this version of techy does not read.
    ///
    /// The data was written by another version of techy. This version reads segments of
    /// version [`Segment::VERSION`](crate::serialize::Segment::VERSION).
    UnsupportedVersion {
        /// The version of the segment.
        found: u32,
        /// The version that this version of techy reads.
        expected: u32,
    },
    /// The profile of the segment differs from the profile of the session.
    ///
    /// The session requires the profile `expected`, which is set with
    /// [`SerdeSession::set_profile`](crate::serialize::SerdeSession::set_profile). The
    /// segment has the profile `found`, or no profile. The data was therefore written for
    /// another configuration. The session reports this error before it absorbs any entry
    /// of the segment.
    ProfileMismatch {
        /// The profile of the session.
        expected: String,
        /// The profile of the segment, or `None` if the segment has no profile.
        found: Option<String>,
    },
    /// The session has no table of the requested name with the expected driver type.
    ///
    /// This error occurs when the segment that the session absorbs lists such a table. It
    /// also occurs when an accessor of techy's standard tables (see
    /// [`StandardTableReading`](crate::serialize::StandardTableReading)) is used on a
    /// session that does not have the table, for instance a session created with
    /// [`SerdeSession::empty`](crate::serialize::SerdeSession::empty).
    UnknownTableName {
        /// The requested table name.
        name: String,
    },
    /// A serialized span does not fit its source.
    ///
    /// Either the byte range `start..end` extends beyond the `len` bytes of the source,
    /// or `start` is greater than `end`.
    SpanOutOfBounds {
        /// The span's start (byte offset, inclusive).
        start: usize,
        /// The span's end (byte offset, exclusive).
        end: usize,
        /// The source's length in bytes.
        len: usize,
    },
    /// A serialized span starts or ends inside a multi-byte character of its source.
    SpanNotOnCharBoundary {
        /// The span's start (byte offset, inclusive).
        start: usize,
        /// The span's end (byte offset, exclusive).
        end: usize,
    },
    /// The serialized data uses a parsing feature that the reading language declares
    /// absent.
    ///
    /// The serialized rules of a parsing state contain the section of the feature
    /// `feature`, or the serialized scope stack of a state is not empty, and the
    /// [`Lang::Features`](crate::core::Lang::Features) of the reading language declare
    /// this feature absent. The language that reads the data is therefore not the
    /// language that wrote it, or it declares other features.
    FeatureAbsent {
        /// The feature's name (`whitespace`, `paragraphs`, `groups`, `commands`,
        /// `comments`, `specials`, `forbidden_chars`, `scopes`).
        feature: &'static str,
    },
    /// The text of a source is not embedded in the data, and no supplier of source text
    /// is configured.
    ///
    /// A source can be serialized by reference: its entry records the length of its text
    /// and optionally a digest of the text, and the reading program supplies the text.
    /// Configure a supplier with
    /// [`SourceSerdeDriver::with_text_supplier`](crate::serialize::SourceSerdeDriver::with_text_supplier).
    NoSourceTextSupplier {
        /// The origin label of the source, if it has one.
        origin: Option<String>,
    },
    /// The text supplied for a source serialized by reference does not have the length
    /// that its entry records.
    ///
    /// Usually the text behind the source, for instance a file, has changed since the
    /// data was written.
    SourceLengthMismatch {
        /// The origin label of the source, if it has one.
        origin: Option<String>,
        /// The length in bytes that the entry records.
        expected: usize,
        /// The length in bytes of the supplied text.
        found: usize,
    },
    /// The text supplied for a source serialized by reference does not match the digest
    /// that its entry records.
    ///
    /// The [`digest_matches`](crate::serialize::SourceTextSupplier::digest_matches)
    /// method of the supplier returned `false`. Usually the text behind the source, for
    /// instance a file, has changed since the data was written.
    SourceDigestMismatch {
        /// The origin label of the source, if it has one.
        origin: Option<String>,
        /// The name of the digest algorithm, as the entry records it.
        algorithm: String,
    },
    /// The segment lists a table twice, or lists two tables with the same writer-side
    /// table identifier.
    DuplicateSegmentTable {
        /// The name of the table.
        name: String,
    },
    /// The segment does not continue the data that the session has absorbed.
    ///
    /// The entries of the segment for the table `table` start at position `found`, and
    /// the table has `expected` entries. Push the segments of a stream in order, each
    /// exactly once, into a session that has absorbed all earlier segments.
    SegmentOutOfOrder {
        /// The name of the table.
        table: &'static str,
        /// The number of entries in the table: the position at which the entries of the
        /// segment must start.
        expected: u32,
        /// The position at which the entries of the segment start.
        found: u32,
    },
    /// The session has entries that were interned since its last emitted segment, so it
    /// cannot absorb a segment.
    ///
    /// A segment continues the data that the session has emitted. Emit the pending
    /// entries of the table `table` with
    /// [`take_segment`](crate::serialize::SerdeSession::take_segment) first.
    UnemittedEntries {
        /// The name of the table.
        table: &'static str,
    },
    /// A reference in the segment contains a writer-side table identifier that the table
    /// directory of the segment does not list.
    UnknownWriterTable {
        /// The writer-side table identifier.
        table: TableId,
    },
    /// The entries of the segment do not fit into the table `table`: together with the
    /// entries that the table already has, they exceed `u32::MAX` entries, the maximum
    /// size of a table.
    ///
    /// Interning an object into a table has the same bound
    /// ([`SerializeError::TableFull`]), so a reader accepts every table that a writer can
    /// fill.
    TableFull {
        /// The name of the table.
        table: &'static str,
    },
    /// A check of the session on its own bookkeeping failed, which indicates a bug in
    /// techy.
    ///
    /// The error is not caused by the input or by an implementation. The session undoes
    /// the operation that found the error (a segment that was being absorbed is dropped),
    /// and the session stays usable.
    Internal {
        /// The description of the failed check, with the entry concerned.
        detail: String,
    },
    /// The data refers to a provider that the reading program does not supply.
    ///
    /// The session looks up providers by name in its
    /// [`KnownProviders`](crate::serialize::KnownProviders), which is stored in the user
    /// data of the session. This error occurs when the `KnownProviders` value has neither
    /// a provider nor a recipe of the name `name`, or when the session has no
    /// `KnownProviders` value. Add the package that the parse used, or a recipe that
    /// builds it, before reading.
    MissingProvider {
        /// The name of the provider.
        name: String,
    },
    /// The data refers to a definition that its provider does not contain in the reading
    /// program.
    ///
    /// The provider `provider`, a package that the reading program supplied, has no
    /// definition with the key `key` under the callable type `callable_type`. This package
    /// is therefore not the package that the data was written with.
    MissingDefinition {
        /// The name of the provider.
        provider: String,
        /// The callable type, in its `Debug` form.
        callable_type: String,
        /// The key that the spec was defined under.
        key: DefinitionKey,
    },
    /// The serialized data of a diagnostic contains a value that a
    /// [`DiagnosticValue`](crate::error::DiagnosticValue) cannot represent.
    ///
    /// The data of a diagnostic is the value that the
    /// [`serializable_data`](crate::error::DiagnosticInfo::serializable_data) method of
    /// its condition returns. This error occurs when the serialized data contains a byte
    /// string or a table position, which `serializable_data` never produces. `kind` tells
    /// which of the two the value is, and `path` tells where it is.
    UnrepresentableDiagnosticValue {
        /// The kind of the value: `bytes` or `index`.
        kind: &'static str,
        /// The location of the value in the data: the map keys and list positions from
        /// the root of the data, as in `error.cause_chain[2]`. The path is empty if the
        /// data itself is the value.
        path: String,
    },
    /// The counts of a serialized diagnostics collection contradict one another, so
    /// [`Diagnostics::from_parts`](crate::error::Diagnostics::from_parts) rejected them.
    ///
    /// The [`InconsistentDiagnosticCounts`] value contains the counts that were read and
    /// the invariant that they violate. A collection built with
    /// [`Diagnostics::push`](crate::error::Diagnostics::push) always satisfies these
    /// invariants.
    InconsistentDiagnosticCounts(
        /// The counts that were read and the invariant that they violate.
        InconsistentDiagnosticCounts,
    ),
    /// An error that occurred while the session deserialized the entry at position
    /// `index` of the table `table`.
    ///
    /// The session wraps every error of a driver in this variant, and `cause` contains the
    /// error. `identifier` is the identifier of the entry if it is known: the identifier
    /// of a table whose objects are all of one kind, or else the identifier that the
    /// entry contains, unless the entry itself is malformed.
    ///
    /// Only the innermost entry is recorded: `cause` is never an `InEntry` itself. It can
    /// be an [`InNode`](DeserializeError::InNode) whose own `cause` is the `InEntry` of
    /// another table, for instance when a tree node refers to a state that cannot be
    /// rebuilt.
    InEntry {
        /// The name of the table.
        table: &'static str,
        /// The position of the entry in the table.
        index: u32,
        /// The identifier of the entry, if it is known.
        identifier: Option<Cow<'static, str>>,
        /// The error.
        cause: Box<DeserializeError>,
    },
    /// An error that occurred while the tree driver rebuilt the node `node` of a tree:
    /// its structure, its payload, or an object that it refers to.
    ///
    /// The tree driver ([`TreeSerdeDriver`](crate::serialize::TreeSerdeDriver)) wraps
    /// every error at a node in this variant, and the session wraps the result in the
    /// [`InEntry`](DeserializeError::InEntry) of the entry of the tree. Only the innermost
    /// node is recorded: `cause` is never an `InNode` itself.
    InNode {
        /// The position of the node in the serialized node list, which is the storage
        /// order of the tree, with the root first.
        node: u32,
        /// The invocation name of the node, if the node is a callable.
        callable: Option<String>,
        /// The error.
        cause: Box<DeserializeError>,
    },
}

impl DeserializeError {
    /// An implementation's own failure ([`Failed`](DeserializeError::Failed)) with the given
    /// description and no underlying error.
    ///
    /// Attach one with [`with_cause`](DeserializeError::with_cause).
    pub fn failed(detail: impl Into<String>) -> DeserializeError {
        DeserializeError::Failed { detail: detail.into(), cause: None }
    }

    /// Attach the underlying error to a [`Failed`](DeserializeError::Failed); any other
    /// variant is returned unchanged.
    ///
    /// The error is stored in an `Arc`, so that the value stays `Clone`, and is reachable
    /// through [`Error::source`](core::error::Error::source).
    pub fn with_cause(self, cause: impl core::error::Error + Send + Sync + 'static) -> DeserializeError {
        match self {
            DeserializeError::Failed { detail, .. } => {
                DeserializeError::Failed { detail, cause: Some(Arc::new(cause)) }
            }
            other => other,
        }
    }

    /// Wrap a driver's failure with the entry it happened in — unless it already
    /// carries an entry location (only the innermost is kept).
    pub(crate) fn in_entry(
        self,
        table: &'static str,
        index: u32,
        identifier: Option<Cow<'static, str>>,
    ) -> DeserializeError {
        match self {
            located @ DeserializeError::InEntry { .. } => located,
            cause => DeserializeError::InEntry { table, index, identifier, cause: Box::new(cause) },
        }
    }

    /// Wrap a per-node failure of the tree driver with the node it happened at —
    /// unless it already carries a node location (only the innermost is kept).
    pub(crate) fn in_node(self, node: u32, callable: Option<String>) -> DeserializeError {
        match self {
            located @ DeserializeError::InNode { .. } => located,
            cause => DeserializeError::InNode { node, callable, cause: Box::new(cause) },
        }
    }
}

impl From<SerialValueError> for DeserializeError {
    fn from(error: SerialValueError) -> DeserializeError {
        DeserializeError::Value(error)
    }
}

impl fmt::Display for DeserializeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DeserializeError::ArgumentIndexOutOfRange { index, count } => write!(
                f,
                "Missing argument spec for serialized argument #{}: the callable spec \
                 declares only {} argument specs",
                index.saturating_add(1),
                count
            ),
            DeserializeError::UnexpectedArgumentSpecPayload { index } => write!(
                f,
                "Unexpected description of the argument spec of serialized argument #{}: \
                 the callable spec does not override ‘deserialize_argument_spec’",
                index.saturating_add(1)
            ),
            DeserializeError::Failed { detail, .. } => {
                write!(f, "Deserialization failed: {detail}")
            }
            DeserializeError::Value(error) => {
                write!(f, "Malformed serialized value: {error}")
            }
            DeserializeError::UnknownTable { table } => write!(
                f,
                "Unknown table #{} in this session: the table handle comes from another \
                 session, or the table has a driver of another type",
                table.ordinal()
            ),
            DeserializeError::IndexOutOfRange { table, index, len } => write!(
                f,
                "Position #{index} is out of range for table ‘{table}’ ({len} entries)"
            ),
            DeserializeError::WrongTable { expected, found } => write!(
                f,
                "Table mismatch: a position of table #{} was read through the handle of table \
                 ‘{expected}’",
                found.ordinal()
            ),
            DeserializeError::AnnotationTypeMismatch { stored, requested, requested_identifier } => {
                write!(f, "Invalid annotation type ‘{requested}’ (")?;
                match requested_identifier {
                    Some(identifier) => write!(f, "identifier ‘{identifier}’")?,
                    None => f.write_str("unregistered")?,
                }
                match stored {
                    Some(identifier) => {
                        write!(f, ") for a tree with annotation identifier ‘{identifier}’")
                    }
                    None => f.write_str(") for a tree of an unregistered annotation type"),
                }
            }
            DeserializeError::ReferenceCycle { table, index, referrer_table, referrer_index } => {
                write!(
                    f,
                    "Reference cycle: entry #{index} of table ‘{table}’ refers back \
                     to itself (read again by entry #{referrer_index} of table \
                     ‘{referrer_table}’)"
                )
            }
            DeserializeError::DescentLimitExceeded { detail, .. } => {
                write!(f, "Serialized entries nested too deeply: {detail}")
            }
            DeserializeError::UnknownIdentifier { table, identifier } => write!(
                f,
                "Missing reader for identifier ‘{identifier}’ in table \
                 ‘{table}’"
            ),
            DeserializeError::UnsupportedVersion { found, expected } => write!(
                f,
                "Unsupported segment version {found} (this version of techy reads version \
                 {expected})"
            ),
            DeserializeError::ProfileMismatch { expected, found } => match found {
                Some(found) => write!(
                    f,
                    "Segment profile ‘{found}’ does not match the session profile \
                     ‘{expected}’"
                ),
                None => write!(
                    f,
                    "Missing segment profile: the session requires profile \
                     ‘{expected}’"
                ),
            },
            DeserializeError::UnknownTableName { name } => write!(
                f,
                "Missing table ‘{name}’: no table of this name with the expected \
                 driver type is registered in this session"
            ),
            DeserializeError::SpanOutOfBounds { start, end, len } => {
                if start > end {
                    write!(
                        f,
                        "Invalid serialized span {start}..{end}: the start may not exceed \
                         the end"
                    )
                } else {
                    write!(f, "Invalid serialized span {start}..{end}: the source has {len} bytes")
                }
            }
            DeserializeError::SpanNotOnCharBoundary { start, end } => write!(
                f,
                "Invalid serialized span {start}..{end}: the span must start and end on \
                 character boundaries of the source"
            ),
            DeserializeError::FeatureAbsent { feature } => write!(
                f,
                "Unsupported parsing feature ‘{feature}’ in serialized data (the \
                 reading language declares this feature absent)"
            ),
            DeserializeError::NoSourceTextSupplier { origin } => write!(
                f,
                "Missing supplier of referenced source text for {}",
                OriginLabel(origin)
            ),
            DeserializeError::SourceLengthMismatch { origin, expected, found } => write!(
                f,
                "Wrong length of the text supplied for {}: {found} bytes, expected \
                 {expected} bytes",
                OriginLabel(origin)
            ),
            DeserializeError::SourceDigestMismatch { origin, algorithm } => write!(
                f,
                "Text supplied for {} does not match the recorded \
                 ‘{algorithm}’ digest",
                OriginLabel(origin)
            ),
            DeserializeError::DuplicateSegmentTable { name } => write!(
                f,
                "Duplicate table ‘{name}’ in segment (repeated name or writer-side \
                 table identifier)"
            ),
            DeserializeError::SegmentOutOfOrder { table, expected, found } => write!(
                f,
                "Segment out of order: its entries for table ‘{table}’ start at \
                 position {found}; the table has {expected} entries"
            ),
            DeserializeError::UnemittedEntries { table } => write!(
                f,
                "Unemitted entries in table ‘{table}’: call \
                 ‘take_segment’ before absorbing a segment"
            ),
            DeserializeError::UnknownWriterTable { table } => write!(
                f,
                "Unknown writer-side table #{} in a reference: the table directory of the \
                 segment does not list it",
                table.ordinal()
            ),
            DeserializeError::TableFull { table } => write!(
                f,
                "Segment exceeds the capacity of table ‘{table}’ \
                 (u32::MAX entries)"
            ),
            DeserializeError::Internal { detail } => {
                write!(f, "Internal error in the serialization session (a bug in techy): {detail}")
            }
            DeserializeError::MissingProvider { name } => write!(
                f,
                "Missing provider ‘{name}’ in the ‘KnownProviders’ \
                 of the session"
            ),
            DeserializeError::MissingDefinition { provider, callable_type, key } => write!(
                f,
                "Missing definition with {key} (callable type ‘{callable_type}’) \
                 in provider ‘{provider}’"
            ),
            DeserializeError::UnrepresentableDiagnosticValue { kind, path } => {
                write!(
                    f,
                    "Invalid value of kind ‘{kind}’ in the data of a serialized \
                     diagnostic, "
                )?;
                if path.is_empty() {
                    write!(f, "at the root")?;
                } else {
                    write!(f, "at ‘{path}’")?;
                }
                write!(
                    f,
                    " (diagnostic values can contain only null, booleans, integers, strings, \
                     lists, and string-keyed maps)"
                )
            }
            DeserializeError::InconsistentDiagnosticCounts(counts) => {
                write!(f, "{counts}")
            }
            DeserializeError::InEntry { table, index, identifier, cause } => match identifier {
                Some(identifier) => write!(
                    f,
                    "Failed to deserialize entry #{index} (identifier \
                     ‘{identifier}’) of table ‘{table}’: {cause}"
                ),
                None => write!(
                    f,
                    "Failed to deserialize entry #{index} of table ‘{table}’: \
                     {cause}"
                ),
            },
            DeserializeError::InNode { node, callable, cause } => match callable {
                Some(callable) => write!(
                    f,
                    "Failed to rebuild node #{node} (callable ‘{callable}’) of a \
                     tree: {cause}"
                ),
                None => write!(f, "Failed to rebuild node #{node} of a tree: {cause}"),
            },
        }
    }
}

impl core::error::Error for DeserializeError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            DeserializeError::Failed { cause, .. } => {
                cause.as_ref().map(|cause| &**cause as &(dyn core::error::Error + 'static))
            }
            DeserializeError::Value(error) => Some(error),
            DeserializeError::InconsistentDiagnosticCounts(error) => Some(error),
            DeserializeError::InEntry { cause, .. } | DeserializeError::InNode { cause, .. } => Some(&**cause),
            _ => None,
        }
    }
}

/// The error that the setup of a [`SerdeSession`](crate::serialize::SerdeSession)
/// returns.
///
/// [`register_table`](crate::serialize::SerdeSession::register_table) returns this
/// error, and so do the methods that register the readers and the resolvers of a table
/// ([`TableHandle::register_type`](crate::serialize::TableHandle::register_type) and the
/// related methods). Each variant reports a call that violates the documented
/// requirements of the method.
///
/// A program usually sets up a session before it writes or reads data. The errors of
/// writing and reading are [`SerializeError`] and [`DeserializeError`].
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum RegistrationError {
    /// The session already has a table of this name.
    DuplicateTableName {
        /// The name of the table.
        name: &'static str,
    },
    /// The session already has the maximum number of tables (`u32::MAX`).
    TooManyTables,
    /// A table handle does not refer to a table of the session that it is used with.
    ///
    /// The handle comes from another session, or the session has a table at the position
    /// of the handle, but with a driver of another type.
    UnknownTable {
        /// The table identifier of the handle.
        table: TableId,
    },
    /// The session has no table of the requested name with the expected driver type.
    ///
    /// A registration function that looks up techy's standard tables by name, such as
    /// [`register_core_readers`](crate::serialize::register_core_readers), was called on a
    /// session that does not have one of these tables.
    UnknownTableName {
        /// The requested table name.
        name: String,
    },
    /// The table already has a reader for this identifier.
    ///
    /// The reader was registered earlier, or a resolver of the table provided it earlier
    /// and the table stored it.
    DuplicateIdentifier {
        /// The name of the table.
        table: &'static str,
        /// The identifier.
        identifier: String,
    },
    /// The driver of the table returns the empty string from
    /// [`homogeneous_identifier`](crate::serialize::ObjectSerdeDriver::homogeneous_identifier).
    ///
    /// An identifier is a non-empty string. The driver of a table whose objects are of
    /// several kinds returns `None`.
    EmptyHomogeneousIdentifier {
        /// The name of the table.
        table: &'static str,
    },
    /// An annotation type of trees is registered twice in the trees table.
    ///
    /// Each annotation type is registered once, with
    /// [`TableHandle::register_annotation`](crate::serialize::TableHandle::register_annotation).
    /// Every trees table has the unit annotation registered: the annotation of the parser
    /// output (`NodeTree<L>`).
    DuplicateAnnotationType {
        /// The name of the trees table.
        table: &'static str,
    },
}

impl fmt::Display for RegistrationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RegistrationError::DuplicateTableName { name } => {
                write!(f, "Duplicate table name ‘{name}’ in this session")
            }
            RegistrationError::TooManyTables => {
                write!(f, "Too many tables: the session already has u32::MAX tables")
            }
            RegistrationError::UnknownTable { table } => write!(
                f,
                "Unknown table #{} in this session: the table handle comes from another \
                 session, or the table has a driver of another type",
                table.ordinal()
            ),
            RegistrationError::UnknownTableName { name } => write!(
                f,
                "Missing table ‘{name}’: no table of this name with the expected \
                 driver type is registered in this session"
            ),
            RegistrationError::DuplicateIdentifier { table, identifier } => write!(
                f,
                "Duplicate reader for identifier ‘{identifier}’ in table \
                 ‘{table}’"
            ),
            RegistrationError::EmptyHomogeneousIdentifier { table } => write!(
                f,
                "Misconfigured driver of table ‘{table}’: the homogeneous \
                 identifier may not be empty; return ‘None’ for a table with \
                 objects of several kinds"
            ),
            RegistrationError::DuplicateAnnotationType { table } => write!(
                f,
                "Duplicate registration of a tree annotation type in table \
                 ‘{table}’"
            ),
        }
    }
}

impl core::error::Error for RegistrationError {}

/// The error that the conversion of plain data to or from a
/// [`SerialValue`](crate::serialize::SerialValue) returns.
///
/// techy's own conversions of its serialized structures return this error, and so does
/// the serde bridge (`to_value` and `from_value`) with the `serde` cargo feature. The
/// type is available without the feature.
///
/// Writing fails on data that the value model cannot represent: floating-point numbers,
/// integers outside the range of `i64`, map keys that are not strings, and map keys that
/// begin with `$`.
///
/// Reading treats the value as untrusted input. A value of the wrong kind, an unknown,
/// missing or repeated map key, an unknown enum variant, or a value nested too deeply is
/// reported as an error and never causes a panic.
///
/// `to_value` does not check the nesting depth. A session rejects a value that is nested
/// too deeply when it interns the value, and reports
/// [`NestingTooDeep`](SerialValueError::NestingTooDeep).
///
/// A conversion error that occurs during serialization or deserialization is returned as
/// [`SerializeError::Value`] or [`DeserializeError::Value`].
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum SerialValueError {
    /// A floating-point number is to be written, a format yields a floating-point number
    /// when a value is read, or a value is to be read into a floating-point type (`f32`
    /// or `f64`). The value model has no floating-point values.
    FloatRejected,
    /// The data contains a map key that is not a string. The maps of the value model have
    /// string keys.
    NonStringMapKey,
    /// The data contains a map key that begins with `$`.
    ///
    /// The value model reserves this prefix for the objects of its canonical rendering
    /// (`$bytes` and `$index`). When a serialized value is read, a reserved key is rejected
    /// as well, but the error has the error type of the format: for a format whose error
    /// type is not `SerialValueError`, it is the `custom` error of the format, with the
    /// message of this variant.
    ReservedMapKey {
        /// The map key.
        key: String,
    },
    /// An integer does not fit its target type.
    ///
    /// When writing, the target type is `i64`, the type of
    /// [`Int`](crate::serialize::SerialValue::Int). When reading, the target type is the
    /// integer type that is read.
    IntegerOutOfRange {
        /// The integer, in decimal notation.
        value: String,
        /// The name of the target type, for instance `"i64"`.
        target: &'static str,
    },
    /// A value has another kind than the expected kind.
    ///
    /// `expected` describes the expected value, and `found` is the kind of the value
    /// found: `null`, `bool`, `int`, `str`, `bytes`, `list`, `map` or `index`.
    TypeMismatch {
        /// A description of the expected value, for instance `a string`.
        expected: Cow<'static, str>,
        /// The kind of the value found.
        found: &'static str,
    },
    /// A map does not have a required key.
    MissingField {
        /// The missing key.
        name: &'static str,
    },
    /// A map has a key that is not one of its expected keys.
    UnknownField {
        /// The unexpected key.
        name: String,
        /// The expected keys.
        expected: &'static [&'static str],
    },
    /// A map has the same key twice.
    DuplicateField {
        /// The repeated key.
        name: String,
    },
    /// An enum value has a variant name that the enum does not have.
    UnknownVariant {
        /// The variant name found.
        name: String,
        /// The variant names of the enum.
        expected: &'static [&'static str],
    },
    /// A value is nested more deeply than the value model allows: more than `limit` lists
    /// and maps enclose a part of the value.
    ///
    /// `limit` is
    /// [`SerialValue::MAX_NESTING_DEPTH`](crate::serialize::SerialValue::MAX_NESTING_DEPTH).
    /// Its documentation also describes how the structure of a segment counts against the
    /// limit.
    ///
    /// This error is reported when a segment is converted from its serialized form, when
    /// a session absorbs a segment, and when a session interns an object whose entry would
    /// exceed the limit. When a value is read through serde (with the `serde` feature, by
    /// the `Deserialize` implementations of `SerialValue` and `Segment`), the same check
    /// runs, but the error has the error type of the format, as for
    /// [`ReservedMapKey`](SerialValueError::ReservedMapKey).
    NestingTooDeep {
        /// The greatest nesting depth allowed.
        limit: usize,
    },
    /// Any other error, with its message. A serde `Serialize` or `Deserialize`
    /// implementation reports such an error through the `custom` method of serde's error
    /// traits.
    Custom(String),
}

impl fmt::Display for SerialValueError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SerialValueError::FloatRejected => {
                write!(f, "Unsupported floating-point number (serialized values have no floating-point type)")
            }
            SerialValueError::NonStringMapKey => {
                write!(f, "Invalid map key: map keys must be strings")
            }
            SerialValueError::ReservedMapKey { key } => {
                write!(f, "Invalid map key ‘{key}’: map keys may not begin with ‘$’")
            }
            SerialValueError::IntegerOutOfRange { value, target } => {
                write!(f, "Integer {value} is out of range for ‘{target}’")
            }
            SerialValueError::TypeMismatch { expected, found } => {
                write!(f, "Invalid value of kind ‘{found}’: expected {expected}")
            }
            SerialValueError::MissingField { name } => write!(f, "Missing key ‘{name}’"),
            SerialValueError::UnknownField { name, expected } => {
                write!(f, "Unknown key ‘{name}’")?;
                if expected.is_empty() {
                    write!(f, " (expected no keys)")
                } else {
                    write!(f, " (expected ")?;
                    write_name_list(f, expected)?;
                    write!(f, ")")
                }
            }
            SerialValueError::DuplicateField { name } => write!(f, "Duplicate key ‘{name}’"),
            SerialValueError::UnknownVariant { name, expected } => {
                write!(f, "Unknown variant ‘{name}’")?;
                if expected.is_empty() {
                    write!(f, " (expected no variants)")
                } else {
                    write!(f, " (expected ")?;
                    write_name_list(f, expected)?;
                    write!(f, ")")
                }
            }
            SerialValueError::NestingTooDeep { limit } => {
                write!(f, "Value nested too deeply (more than {limit} levels of lists and maps)")
            }
            SerialValueError::Custom(message) => f.write_str(message),
        }
    }
}

/// Writes a non-empty list of names: `‘a’` for a single name, `one of ‘a’, ‘b’` for
/// several names.
fn write_name_list(f: &mut fmt::Formatter<'_>, names: &[&str]) -> fmt::Result {
    match names {
        [only] => write!(f, "‘{only}’"),
        _ => {
            write!(f, "one of ")?;
            for (i, name) in names.iter().enumerate() {
                if i > 0 {
                    write!(f, ", ")?;
                }
                write!(f, "‘{name}’")?;
            }
            Ok(())
        }
    }
}

impl core::error::Error for SerialValueError {}