Skip to main content

flm_core/stdlib/features/floats/
diagnostics.rs

1//! This module defines the diagnostic conditions of the [`floats`](super)
2//! feature. Their identifiers have the form `flm.floats.<condition>`. The
3//! conditions [`NonumberWithoutEffect`], [`LabelWithoutRefs`] and
4//! [`MissingHandler`] are notes, and the other conditions are errors.
5//!
6//! Every lowering rule of a float reads the float with
7//! [`read_float`](super::read_float), which reports the problems of the float.
8//! A document therefore gets the same reports on every backend. These
9//! conditions are reported only when a float is lowered. The `check` workflow
10//! ([`CHECK_WORKFLOW`](crate::stdlib::CHECK_WORKFLOW)) lowers no float, and
11//! therefore does not report them. They concern the following problems:
12//!
13//! - a repeated `\caption`, or a mistake in the optional argument of
14//!   `\caption` ([`CaptionRepeated`], [`CaptionUnknownKey`],
15//!   [`CaptionMissingValue`], [`CaptionRepeatedKey`]);
16//! - a `\nonumber` that has no effect ([`NonumberWithoutEffect`]);
17//! - a `\label` in a registry without the references feature
18//!   ([`LabelWithoutRefs`]);
19//! - a float to be numbered in a registry without the numbering feature
20//!   ([`NoNumbering`]);
21//! - content that the float type does not accept ([`ContentRefused`]).
22//!
23//! The check stage [`CHECK_HANDLERS`](super::CHECK_HANDLERS) reports
24//! [`MissingHandler`], also under the `check` workflow. The check stage
25//! reports [`StageFailed`] when it fails to read a table. A document cannot
26//! cause this error.
27
28use techy::error::DiagnosticInfo;
29
30/// The error that is reported when the body of a float contains more than one
31/// `\caption` (`flm.floats.caption-repeated`).
32///
33/// Reported by the lowering rule of the float, on every backend, at each
34/// `\caption` after the first. The first `\caption` gives the caption of the
35/// float. The other `\caption` commands are left out of the output.
36#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
37#[diagnostic(
38    id = "flm.floats.caption-repeated",
39    message = "A ‘{float_type}’ must hold at most one ‘\\caption’"
40)]
41pub struct CaptionRepeated {
42    /// The name of the float type, e.g. `figure`.
43    pub float_type: String,
44}
45
46/// The error that is reported when the optional argument of `\caption`
47/// contains a key other than `alt` (`flm.floats.caption-unknown-key`).
48///
49/// The optional argument of `\caption` contains key-value pairs. Its only key
50/// is `alt`, which gives the alternative text of the caption, as in
51/// `\caption[alt=A plot]{A plot of the data, before and after.}`. A reference
52/// to an unnumbered float shows the alternative text in place of the caption.
53/// Unlike in LaTeX, the optional argument does not give a short caption.
54///
55/// Reported by the lowering rule of the float, on every backend, at the
56/// `\caption`. The unknown key is ignored. A valid `alt` key in the same
57/// argument is still used.
58#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
59#[diagnostic(
60    id = "flm.floats.caption-unknown-key",
61    message = "Invalid ‘{key}’ in the optional argument of ‘\\caption’: \
62               this argument may contain only ‘alt=…’"
63)]
64pub struct CaptionUnknownKey {
65    /// The unknown key, as written. When the optional argument cannot be read
66    /// as key-value pairs, this is the text of the whole argument.
67    pub key: String,
68}
69
70/// The error that is reported when the key `alt` in the optional argument of
71/// `\caption` is written without `=` (`flm.floats.caption-missing-value`).
72///
73/// The key `alt` must be followed by `=` and the alternative text of the
74/// caption, as in `\caption[alt=A plot]{A plot of the data, before and
75/// after.}`.
76///
77/// Reported by the lowering rule of the float, on every backend, at the
78/// `\caption`. The key is ignored.
79#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
80#[diagnostic(
81    id = "flm.floats.caption-missing-value",
82    message = "Missing value for key ‘alt’ of ‘\\caption’: use the syntax ‘alt=…’"
83)]
84pub struct CaptionMissingValue;
85
86/// The error that is reported when the optional argument of `\caption`
87/// contains `alt=…` more than once (`flm.floats.caption-repeated-key`).
88///
89/// Reported by the lowering rule of the float, on every backend, at the
90/// `\caption`. The first value of `alt` is used, and the others are ignored.
91#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
92#[diagnostic(
93    id = "flm.floats.caption-repeated-key",
94    message = "Duplicate key ‘alt’ in the optional argument of ‘\\caption’"
95)]
96pub struct CaptionRepeatedKey;
97
98/// The note that is reported when a `\nonumber` in the body of a float has no
99/// effect (`flm.floats.nonumber-without-effect`).
100///
101/// The numbering policy of the float type
102/// ([`FloatNumbering`](super::FloatNumbering)) determines whether a float is
103/// numbered. A `\nonumber` has no effect in the following cases:
104///
105/// - The float type is never numbered
106///   ([`Unnumbered`](super::FloatNumbering::Unnumbered)).
107/// - The float type is numbered only when the float has a `\label`
108///   ([`AutoNumbered`](super::FloatNumbering::AutoNumbered), the default), and
109///   the float has no `\label`.
110/// - The float contains an earlier `\nonumber`.
111///
112/// Reported by the lowering rule of the float, on every backend, at each such
113/// `\nonumber`. The float is not numbered.
114#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
115#[diagnostic(
116    id = "flm.floats.nonumber-without-effect",
117    message = "‘\\nonumber’ has no effect here: {reason}"
118)]
119pub struct NonumberWithoutEffect {
120    /// The reason why the `\nonumber` has no effect, as the end of the
121    /// message, e.g. `a ‘figure’ is never numbered`.
122    pub reason: String,
123}
124
125/// The note that is reported when a float has a `\label` and the registry does
126/// not contain the [`refs`](crate::stdlib::features::refs) feature (id
127/// `flm.floats.label-without-refs`).
128///
129/// Without the references feature, `\ref` is not available, and nothing can
130/// refer to the float. The label still counts for the numbering policy of the
131/// float type: a float of a type that is numbered only when it has a `\label`
132/// ([`AutoNumbered`](super::FloatNumbering::AutoNumbered)) is numbered.
133///
134/// Reported by the lowering rule of the float, on every backend, at the first
135/// `\label` of the float. The float is otherwise lowered as usual.
136#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
137#[diagnostic(
138    id = "flm.floats.label-without-refs",
139    message = "This ‘{float_type}’ cannot be referred to: references to labels are not available"
140)]
141pub struct LabelWithoutRefs {
142    /// The name of the float type, e.g. `figure`.
143    pub float_type: String,
144}
145
146/// The error that is reported when a float is to be numbered and the registry
147/// does not contain the [`numbering`](crate::stdlib::features::numbering)
148/// feature (id `flm.floats.no-numbering`).
149///
150/// The numbering policy of the float type
151/// ([`FloatNumbering`](super::FloatNumbering)) determines whether a float is
152/// numbered. In the default configuration, a `figure` or a `table` is
153/// numbered when it has a `\label` and no `\nonumber`. Only the numbering
154/// feature computes the numbers of floats.
155///
156/// Reported by the lowering rule of the float, on every backend, at the float.
157/// On every backend except `latex-recomposed`, the float is written as an
158/// unnumbered float: the line under its content contains only its caption, or
159/// is left out when the float has no caption. On the `latex-recomposed`
160/// backend, the float is written as numbered, and LaTeX numbers it.
161#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
162#[diagnostic(
163    id = "flm.floats.no-numbering",
164    message = "Missing number for this ‘{float_type}’: numbering is not available"
165)]
166pub struct NoNumbering {
167    /// The name of the float type, e.g. `figure`.
168    pub float_type: String,
169}
170
171/// The error that is reported when the content of a float is not accepted by
172/// its float type (`flm.floats.content-refused`).
173///
174/// A float type lists the content handlers that it accepts
175/// ([`FloatType::handlers`](super::FloatType::handlers)). A content handler
176/// ([`FloatContentHandler`](super::FloatContentHandler)) checks the content of
177/// a float, and accepts it or refuses it. The content of a float is accepted
178/// when one of the content handlers of its type accepts it. In the default
179/// configuration, a `figure` must contain exactly one image, as in
180/// `\begin{figure}\includegraphics{plot.png}\caption{A plot}\end{figure}`. A
181/// `table` must contain exactly one table of cells or exactly one image. The
182/// commands `\caption`, `\label` and `\nonumber`, whitespace and comments do
183/// not count as content.
184///
185/// The message lists the content that each available content handler of the
186/// type accepts. A content handler that is not available in the registry
187/// accepts no content, and is reported as [`MissingHandler`]. When none of the
188/// content handlers of the type is available, no content can be accepted, and
189/// the message lists the missing content handlers instead.
190///
191/// Reported by the lowering rule of the float, on every backend, at the float.
192/// The content is lowered as usual, as if the float type accepted any
193/// content.
194#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
195#[diagnostic(
196    id = "flm.floats.content-refused",
197    message = "A ‘{float_type}’ must hold {admitted}"
198)]
199pub struct ContentRefused {
200    /// The name of the float type, e.g. `figure`.
201    pub float_type: String,
202    /// The content that the float type accepts, as the end of the message. It
203    /// lists what each available content handler of the type accepts, joined
204    /// with `or`, e.g. `exactly one table of cells or exactly one image`. When
205    /// none of the content handlers of the type is available, it lists the
206    /// missing content handlers instead, e.g. `content accepted by the missing
207    /// content handler ‘includegraphics’`.
208    pub admitted: String,
209}
210
211/// The note that is reported when a float type that is used in the document
212/// lists a content handler that is not available in the registry (id
213/// `flm.floats.missing-handler`).
214///
215/// A float type lists the content handlers that it accepts
216/// ([`FloatType::handlers`](super::FloatType::handlers)). Each content handler
217/// is contributed to the service slot [`CONTENT_SLOT`](super::CONTENT_SLOT) by
218/// the feature that provides its kind of content: the `graphics` feature
219/// contributes `includegraphics`, and the `cells` feature contributes `cells`.
220/// A content handler is missing, for instance, when the registry does not
221/// contain the feature that contributes it.
222///
223/// Reported by the check stage [`CHECK_HANDLERS`](super::CHECK_HANDLERS), also
224/// under the `check` workflow, once for each float type and each missing
225/// content handler of the type, at the first float of the type in the
226/// document. The missing content handler accepts no content, and the other
227/// content handlers of the type are tried. When none of the content handlers
228/// of a float type is available, every float of the type is reported as
229/// [`ContentRefused`].
230#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
231#[diagnostic(
232    id = "flm.floats.missing-handler",
233    message = "Missing content handler ‘{handler}’ for float type ‘{float_type}’"
234)]
235pub struct MissingHandler {
236    /// The name of the float type, e.g. `figure`.
237    pub float_type: String,
238    /// The name of the missing content handler, e.g. `includegraphics`.
239    pub handler: String,
240}
241
242/// Error raised when the check stage of the floats feature fails to read a
243/// table (id `flm.floats.stage-failed`).
244///
245/// The check stage [`CHECK_HANDLERS`](super::CHECK_HANDLERS) reads the element
246/// index ([`ELEMENT_INDEX`](crate::stdlib::features::index::ELEMENT_INDEX)).
247/// A table read fails only when the code of a feature is faulty, for instance
248/// when the value type of the table differs from the declared one (see
249/// [`AccessError`](crate::core::data::store::AccessError)). A document cannot
250/// cause this error.
251///
252/// Reported with no position in the source. The check stage then stops, and
253/// reports no [`MissingHandler`] for the document.
254#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
255#[diagnostic(id = "flm.floats.stage-failed", message = "Stage ‘{stage}’ failed: {detail}")]
256pub struct StageFailed {
257    /// The name of the stage, `flm.floats.check-handlers`.
258    pub stage: String,
259    /// The error message of the failed table read.
260    pub detail: String,
261}