flm_core/stdlib/features/xnotes/diagnostics.rs
1//! This module defines the diagnostic conditions that the `xnotes` feature
2//! reports (see [diagnostics](crate::guide::concepts_overview#diagnostics)).
3//!
4//! The conditions [`BadLabelArgument`], [`Empty`] and [`NoNumbering`] concern
5//! the xnotes written in a document. The lowering rule of an xnote reports
6//! [`BadLabelArgument`] when the optional argument of the xnote contains
7//! something other than `\label` commands, and [`Empty`] when the xnote has no
8//! content. The collect stage ([`COLLECT`](super::COLLECT)) reports
9//! [`NoNumbering`] when a document contains xnotes of a numbered xnote kind
10//! and the registry does not contain the numbering feature.
11//!
12//! The three other conditions indicate an error in the code of a feature, and
13//! a document cannot cause them. The collect stage reports [`Unpaired`] for an
14//! element whose aspects return different numbers of xnotes. The lowering
15//! rules of a list of xnotes and of its content pointers report
16//! [`PointerRefused`] for a content pointer that does not refer to the content
17//! of an xnote. The lowering rule of an entry of a list of xnotes reports
18//! [`EntryOutsideList`] when the entry is lowered outside its list.
19//!
20//! Each of these conditions is reported as an Error, except [`Empty`], which
21//! is reported as a Warning. The document is rendered in spite of these
22//! conditions. The documentation of each condition states what is written in
23//! place of the faulty part of the document.
24//!
25//! The condition [`StageFailed`](super::StageFailed), which the stages of the
26//! feature report, is defined together with the stages.
27
28use techy::error::DiagnosticInfo;
29
30/// The error that is reported when the optional argument of an xnote contains
31/// something other than `\label` commands (id `flm.xnotes.bad-label-argument`).
32///
33/// The optional argument of an xnote contains the labels of the xnote, as in
34/// `\footnote[\label{fn:a}]{…}`. This argument may contain only `\label{…}`
35/// commands, whitespace (including `~` and `\ `) and comments.
36///
37/// Reported by the lowering rule of the xnote when it is invoked. The error is
38/// reported once for each xnote, for the first node of the argument that does
39/// not meet this requirement. All content of the argument other than the
40/// `\label` commands is ignored, and these `\label` commands still label the
41/// xnote.
42///
43/// In a registry without the references feature, the `\label` command is not
44/// defined, and a `\label` command in the optional argument is reported in the
45/// same way.
46#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
47#[diagnostic(
48 id = "flm.xnotes.bad-label-argument",
49 message = "Invalid ‘{found}’ in the optional argument of this {kind}: this \
50 argument may contain only ‘\\label{{…}}’ commands"
51)]
52pub struct BadLabelArgument {
53 /// The name of the xnote kind, e.g. `footnote`.
54 pub kind: String,
55 /// The source text of the first node of the optional argument that is not
56 /// a `\label` command, whitespace or a comment. The text is trimmed and
57 /// cut after 40 characters.
58 pub found: String,
59}
60
61/// Error raised when the xnotes of a numbered xnote kind cannot be numbered
62/// (`flm.xnotes.no-numbering`).
63///
64/// The numbering feature computes the numbers of the xnotes of a numbered
65/// xnote kind, such as footnotes. This error is reported when a document
66/// contains xnotes of a numbered kind and the registry does not contain the
67/// numbering feature.
68///
69/// Reported by the collect stage [`COLLECT`](super::COLLECT), once for each
70/// such xnote kind, at the first xnote of the kind. The xnotes of this kind
71/// have no number: their marks in the text and in the list of the kind are
72/// empty. The list of the kind is still built.
73#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
74#[diagnostic(
75 id = "flm.xnotes.no-numbering",
76 message = "Numbering is not available for ‘{kind}’ notes"
77)]
78pub struct NoNumbering {
79 /// The name of the xnote kind.
80 pub kind: String,
81}
82
83/// The warning that is reported when an xnote has no content (id
84/// `flm.xnotes.empty`). Content that consists only of whitespace (including
85/// `~` and `\ `) and comments counts as empty.
86///
87/// Reported by the lowering rule of the xnote when it is invoked. The xnote is
88/// otherwise processed as usual, with empty content: its mark is written, and
89/// for an xnote kind with a list, the entry of the xnote in the list is empty.
90#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
91#[diagnostic(id = "flm.xnotes.empty", message = "This {kind} is empty")]
92pub struct Empty {
93 /// The name of the xnote's kind.
94 pub kind: String,
95}
96
97/// Error representing a content pointer
98/// ([`CONTENT_POINTER`](super::CONTENT_POINTER)) that does not refer to the
99/// content of an xnote (id `flm.xnotes.pointer-refused`).
100///
101/// In a list of xnotes, the content of each xnote is written in an entry of
102/// the list. Each entry contains a content pointer: a synthesized node that
103/// refers to the region of a node that contains the content of the xnote, such
104/// as the mandatory argument of a `\footnote` command. The region must contain
105/// the content of an xnote of a kind declared in the registry, as the
106/// [`XnoteSource`](super::XnoteSource) aspect of the node's element returns
107/// it. This requirement ensures that no content is written twice: any other
108/// content is already written where its node is placed.
109///
110/// A document cannot cause this error: it is caused by the faulty stage that
111/// created the content pointer. Reported by the lowering rule of the content
112/// pointer ([`PointerRule`](super::PointerRule)), and by the lowering rule of
113/// a list of xnotes ([`ListRule`](super::ListRule)), which lowers the content
114/// pointers of its entries itself. The content pointer expands to nothing.
115#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
116#[diagnostic(
117 id = "flm.xnotes.pointer-refused",
118 message = "Invalid content pointer to {region} of a node of ‘{element}’: the \
119 pointer must refer to the content of an xnote"
120)]
121pub struct PointerRefused {
122 /// The name of the element of the node that the content pointer refers to.
123 pub element: String,
124 /// The region of this node that the content pointer refers to, as text,
125 /// e.g. `argument 1`.
126 pub region: String,
127}
128
129/// Error representing an entry of a list of xnotes ([`ENTRY`](super::ENTRY))
130/// that is lowered by its own lowering rule (id
131/// `flm.xnotes.entry-outside-list`).
132///
133/// The lowering rule of a list of xnotes lowers the content of each entry
134/// itself, and never invokes the lowering rule of the entry. An entry is
135/// therefore lowered by its own rule only when a stage placed it outside a
136/// list, or when the lowering rule of its list lowers the children of the list
137/// one by one.
138///
139/// A document cannot cause this error: it is caused by a faulty stage or
140/// lowering rule. Reported by [`EntryRule`](super::EntryRule). The entry
141/// expands to nothing.
142#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
143#[diagnostic(
144 id = "flm.xnotes.entry-outside-list",
145 message = "Entries of a list of xnotes must be lowered by the lowering rule of their list"
146)]
147pub struct EntryOutsideList;
148
149/// Error raised when the two aspects of an element that describe its xnotes
150/// return different numbers of xnotes of one kind (id `flm.xnotes.unpaired`).
151///
152/// Each xnote that an element carries is described in two aspects of the
153/// element. The [`Referenceable`](crate::model::aspects::Referenceable) aspect
154/// returns two reference points for each xnote, created with
155/// [`ref_points`](super::ref_points). The [`XnoteSource`](super::XnoteSource)
156/// aspect returns the region of the node that contains the content of each
157/// xnote. For each xnote kind, both aspects must return the same number of
158/// xnotes.
159///
160/// A document cannot cause this error: it indicates an error in the
161/// definition of the element. Reported by the collect stage
162/// [`COLLECT`](super::COLLECT) once for each such node and xnote kind. The
163/// xnotes of the node are paired with the content regions in order. An xnote
164/// without a content region has empty content. A content region without an
165/// xnote is not written, and the lowering rule of the element reports
166/// `flm.run.missing-entry` for it.
167#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
168#[diagnostic(
169 id = "flm.xnotes.unpaired",
170 message = "Misconfigured element ‘{element}’: different numbers of xnotes of \
171 kind ‘{kind}’ in its reference points ({points}) and in its \
172 ‘XnoteSource’ aspect ({contents})"
173)]
174pub struct Unpaired {
175 /// The name of the element of the node.
176 pub element: String,
177 /// The name of the xnote kind.
178 pub kind: String,
179 /// The number of xnotes of this kind among the reference points of the
180 /// node.
181 pub points: usize,
182 /// The number of content regions of xnotes of this kind that the
183 /// [`XnoteSource`](super::XnoteSource) aspect of the element returns for
184 /// the node.
185 pub contents: usize,
186}