Skip to main content

flm_core/stdlib/features/citations/
diagnostics.rs

1//! This module defines the diagnostic conditions that the `citations` feature
2//! reports (see [diagnostics](crate::guide::concepts_overview#diagnostics)).
3//!
4//! The lowering rules of `\cite` and `\R` report the errors in the citations
5//! of a document: [`NoKey`], [`NoCitation`], [`UnknownFlag`] and
6//! [`Uncollected`]. The stages of the feature report the problems with the
7//! citation entries. The retrieve stage ([`RETRIEVE`](super::RETRIEVE))
8//! obtains the citation entries from the citation providers, and reports
9//! [`NoProvider`], [`ProviderFailed`], [`ProviderWarning`], [`EntryCites`] and
10//! [`EntryNotInline`]. The check stage ([`CHECK`](super::CHECK)) reports
11//! [`NoEntry`] for each cited key that has no citation entry.
12//!
13//! Each of these conditions is reported as an Error, except
14//! [`ProviderWarning`], which is reported as a Warning. The document is
15//! rendered in spite of these conditions. The documentation of each condition
16//! states what is written in place of the faulty citation or citation entry.
17//!
18//! The condition [`StageFailed`](super::StageFailed), which a stage of the
19//! feature reports when it cannot read or write one of its tables, is defined
20//! together with the stages.
21
22use techy::error::DiagnosticInfo;
23
24/// The error that is reported when a `\cite` call has an empty argument (id
25/// `flm.citations.no-key`). An argument that contains only commas and
26/// whitespace counts as empty.
27///
28/// Reported by the lowering rule of `\cite` when it is invoked, and by the
29/// lowering rule of `\R` for the `\cite` call that follows the `\R`. If the
30/// `\cite` call stands alone, it expands to a dropped mark
31/// (`flm.pres.dropped`). Several `\cite` calls written directly one after
32/// another, as in `\cite{a}\cite[Thm 4]{b}`, expand to one citation mark, and
33/// a call with an empty argument adds no number to this citation mark.
34#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
35#[diagnostic(
36    id = "flm.citations.no-key",
37    message = "Missing key in the argument of ‘\\cite’"
38)]
39pub struct NoKey;
40
41/// Error raised when a `\R` is not immediately followed by `\cite`
42/// (`flm.citations.no-citation`). The `\R` command must immediately be
43/// followed by a valid `\cite{..}` command invocation, as in
44/// `\R\cite{bib:myref}` or `\R[I]\cite{bib:myref,bib:myref2}`. Reported by the
45/// lowering rule for `\R` when `\R` is invoked. In such a case, the `\R`
46/// expands to a dropped mark (`flm.pres.dropped`).
47#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
48#[diagnostic(
49    id = "flm.citations.no-citation",
50    message = "‘\\R’ must be followed immediately by ‘\\cite’"
51)]
52pub struct NoCitation;
53
54/// The error that is reported when the flags argument of `\R` contains an
55/// unknown flag (`flm.citations.unknown-flag`).
56///
57/// The flags argument of `\R` accepts two flags, each written as one
58/// character, as in `\R[S]\cite{a}`. The flag `S` capitalizes the word that
59/// `\R` writes before the citation mark (`Ref.` instead of `ref.`), and the
60/// flag `I` omits this word. Whitespace in the flags argument is ignored.
61///
62/// Reported by the lowering rule of `\R` when it is invoked, once for each
63/// unknown flag. Each unknown flag is ignored. If the flags argument contains
64/// content other than characters, the error is reported once, with the plain
65/// text of the whole argument, and the whole flags argument is ignored.
66#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
67#[diagnostic(
68    id = "flm.citations.unknown-flag",
69    message = "Invalid flag ‘{flag}’ for ‘\\R’; expected ‘S’, ‘I’"
70)]
71pub struct UnknownFlag {
72    /// The unknown flag. If the flags argument contains content other than
73    /// characters, this field contains the plain text of the whole argument.
74    pub flag: String,
75}
76
77/// Error raised when a citation key is cited only outside the trees of the
78/// document, so that it has no number (condition id
79/// `flm.citations.uncollected`).
80///
81/// The collect stage ([`COLLECT`](super::COLLECT)) records the citation keys
82/// of the citations in the trees of the document, and only these citation keys
83/// are numbered. A citation in other content is not recorded. This is the
84/// case for a citation in a [synthesized
85/// value](crate::guide::concepts_overview#synthesized-values) that a stage
86/// creates, and for a citation in the template of a user definition whose use
87/// sites are expanded during lowering.
88///
89/// Reported by the lowering rules of `\cite` and `\R` through
90/// [`stage_mark`](super::stage_mark), once for each such citation key, at the
91/// `\cite` or `\R`. The citation key is left out of the citation mark. If no
92/// citation key is left, the citation expands to a dropped mark
93/// (`flm.pres.dropped`).
94#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
95#[diagnostic(
96    id = "flm.citations.uncollected",
97    message = "Citation to ‘{key}’ may not appear here (citations may only appear in document text)"
98)]
99pub struct Uncollected {
100    /// The citation key.
101    pub key: String,
102}
103
104/// The error that is reported when a document cites works and no citation
105/// provider is set up (`flm.citations.no-provider`).
106///
107/// Citation providers supply the citation entries of the cited works. The
108/// program that embeds FLM contributes them to the service slot
109/// [`PROVIDERS_SLOT`](super::PROVIDERS_SLOT); see
110/// [`CitationProvider`](super::CitationProvider).
111///
112/// Reported once by the retrieve stage ([`RETRIEVE`](super::RETRIEVE)), at the
113/// first citation of the document. The citations are numbered as usual, but no
114/// cited key has a citation entry. A dropped mark (`flm.pres.dropped`) is
115/// written in place of each citation entry, and the check stage also reports
116/// [`NoEntry`] for each cited key.
117#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
118#[diagnostic(
119    id = "flm.citations.no-provider",
120    message = "No bibliography source set up to handle citation ‘{key}’"
121)]
122pub struct NoProvider {
123    /// The document's first cited key.
124    pub key: String,
125}
126
127/// The error that is reported when a citation provider fails to retrieve the
128/// citation entry for a citation key (`flm.citations.provider-failed`).
129///
130/// A citation provider reports this failure by returning a
131/// [`CitationProviderError`](super::CitationProviderError) for the citation
132/// key. The retrieve stage also reports this error for every citation key of a
133/// call of [`CitationProvider::answer_all`](super::CitationProvider::answer_all)
134/// that returns a different number of results than it received requests.
135///
136/// Reported by the retrieve stage ([`RETRIEVE`](super::RETRIEVE)), at the first
137/// citation of the citation key. No later citation provider is called for this
138/// citation key, and the citation key has no citation entry. A dropped mark
139/// (`flm.pres.dropped`) is written in place of its citation entry, and the
140/// check stage also reports [`NoEntry`] for it.
141#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
142#[diagnostic(
143    id = "flm.citations.provider-failed",
144    message = "Failed to retrieve bibliography entry for ‘{key}’: {detail}"
145)]
146pub struct ProviderFailed {
147    /// The citation key.
148    pub key: String,
149    /// The error message that the
150    /// [`CitationProviderError`](super::CitationProviderError) contains.
151    pub detail: String,
152}
153
154/// The warning that is reported for each warning text that a citation
155/// provider adds to its answer for a citation key
156/// (`flm.citations.provider-warning`).
157///
158/// A citation provider adds a warning text to an answer with
159/// [`CitationAnswer::with_warning`](super::CitationAnswer::with_warning). The
160/// message of this diagnostic is the warning text.
161///
162/// Reported by the retrieve stage ([`RETRIEVE`](super::RETRIEVE)), at the first
163/// citation of the citation key, in the order in which the warning texts were
164/// added. The answer is processed as usual.
165#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
166#[diagnostic(id = "flm.citations.provider-warning", message = "{detail}")]
167pub struct ProviderWarning {
168    /// The citation key.
169    pub key: String,
170    /// The warning text.
171    pub detail: String,
172}
173
174/// The error that is reported when the citation entry for a citation key
175/// contains a citation (`flm.citations.entry-cites`).
176///
177/// A citation entry may not contain `\cite` or `\R`. The citation entry is
178/// checked after the user definitions that it uses have been expanded, so a
179/// citation that a user definition writes is also found.
180///
181/// Reported by the retrieve stage ([`RETRIEVE`](super::RETRIEVE)), at the first
182/// citation of the citation key. The retrieve stage refuses the citation
183/// entry, so the citation key has no citation entry. A dropped mark
184/// (`flm.pres.dropped`) is written in place of the citation entry, and the
185/// check stage also reports [`NoEntry`] for the citation key.
186#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
187#[diagnostic(
188    id = "flm.citations.entry-cites",
189    message = "The bibliography entry for ‘{key}’ may not use ‘\\cite’ recursively"
190)]
191pub struct EntryCites {
192    /// The citation key.
193    pub key: String,
194}
195
196/// The error that is reported when the citation entry for a citation key is
197/// not inline content (`flm.citations.entry-not-inline`).
198///
199/// A citation entry must be inline content, that is, content without
200/// paragraph breaks and blocks, as in `D. Knuth, \emph{The TeXbook}, 1984`. A
201/// citation entry is written inside an entry of the list of cited works, or
202/// inside a citation mark under the in-place placement
203/// ([`CitationPlacement::InPlace`](super::CitationPlacement::InPlace)). The
204/// citation entry is checked after the user definitions that it uses have
205/// been expanded.
206///
207/// Reported by the retrieve stage ([`RETRIEVE`](super::RETRIEVE)), at the first
208/// citation of the citation key. The retrieve stage refuses the citation
209/// entry, so the citation key has no citation entry. A dropped mark
210/// (`flm.pres.dropped`) is written in place of the citation entry, and the
211/// check stage also reports [`NoEntry`] for the citation key.
212#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
213#[diagnostic(
214    id = "flm.citations.entry-not-inline",
215    message = "The bibliography entry for ‘{key}’ must hold inline content only (no paragraph breaks or blocks)"
216)]
217pub struct EntryNotInline {
218    /// The citation key.
219    pub key: String,
220}
221
222/// The error that is reported when a cited key has no citation entry
223/// (`flm.citations.no-entry`).
224///
225/// The field `reason` states why the citation key has no citation entry
226/// ([`MissingEntry`](super::MissingEntry)). For instance, no citation provider
227/// handles the citation key, or the citation provider failed to retrieve the
228/// citation entry.
229///
230/// Reported by the check stage ([`CHECK`](super::CHECK)), at the first citation
231/// of the citation key. The citation key keeps its number, and its citations
232/// are written as usual. A dropped mark (`flm.pres.dropped`) is written in
233/// place of the citation entry: in the list of cited works, or, under the
234/// in-place placement
235/// ([`CitationPlacement::InPlace`](super::CitationPlacement::InPlace)), in the
236/// citation marks.
237#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
238#[diagnostic(
239    id = "flm.citations.no-entry",
240    message = "No entry found for citation key ‘{key}’: {reason}"
241)]
242pub struct NoEntry {
243    /// The citation key.
244    pub key: String,
245    /// The reason why the citation key has no citation entry, as returned by
246    /// [`MissingEntry::reason`](super::MissingEntry::reason). For instance,
247    /// `no appropriate citation provider found`.
248    pub reason: String,
249}