Skip to main content

flm_core/stdlib/features/citations/
check.rs

1//! The check stage of the `citations` feature, which reports every cited key
2//! that has no citation entry.
3
4use techy::error::Diagnostic;
5
6use crate::core::data::store::StageOut;
7use crate::core::plan::PlanCx;
8use crate::core::run::StageCx;
9use crate::core::stage::{Effect, Needs, Scope, Stage, StageDecl};
10
11use super::diagnostics::NoEntry;
12use super::entries::{MissingEntry, Retrieval, ENTRIES};
13use super::numbers::{stage_failed, KEYS};
14
15/// The name of the check stage and of the capability it provides,
16/// `flm.citations.check`. The capability is a member of the collection
17/// [`flm.validated`](crate::stdlib::VALIDATED), which every workflow
18/// demands (asks the plan to run a stage that provides it; see
19/// [`PlanRequest::demand`](crate::core::plan::PlanRequest::demand)), so the
20/// check runs under every workflow.
21pub const CHECK: &str = "flm.citations.check";
22
23/// The check stage, [`CHECK`]: a check stage of document scope. It reads the
24/// cited keys ([`KEYS`]) and their citation entries ([`ENTRIES`]), and reports
25/// every cited key with no citation entry ([`NoEntry`]), in order of first
26/// citation, at the citation key's first citation: one report per citation
27/// key, even when several citation keys refer to one work.
28///
29/// Because its capability is a member of `flm.validated`, it runs under every
30/// workflow, the `check` workflow included, with the same reports. Because it
31/// reads [`ENTRIES`], the retrieve stage runs before it, so the `check`
32/// workflow asks the citation providers as a rendering workflow does.
33#[derive(Debug, Default)]
34pub(super) struct Check;
35
36impl Stage for Check {
37    fn decl(&self) -> StageDecl {
38        StageDecl::new(CHECK, Effect::Check, Scope::Document).provides(CHECK).docs(
39            "Reports, as an Error, every citation key the document cites that has no citation \
40             entry in `flm.citations.entries`.",
41        )
42    }
43
44    /// Reads the cited keys, with where each is first cited, and their
45    /// citation entries.
46    fn needs(&self, _cx: &PlanCx<'_>) -> Needs {
47        Needs::new().reads(KEYS).reads(ENTRIES)
48    }
49
50    fn run(&self, cx: &StageCx<'_>, out: &mut StageOut<'_>) {
51        let (cited, entries) = match (cx.get(KEYS), cx.get(ENTRIES)) {
52            (Ok(Some(cited)), Ok(entries)) => (cited, entries),
53            (Err(error), _) | (_, Err(error)) => return stage_failed(out, CHECK, error),
54            _ => return,
55        };
56        for (key, span) in cited.iter() {
57            let missing = match entries.and_then(|entries| entries.get(key)) {
58                Some(Retrieval::Found(_)) => continue,
59                Some(Retrieval::Missing(missing)) => missing.clone(),
60                // Both tables hold the same keys: a key the entries table does
61                // not hold was asked of no provider.
62                None => MissingEntry::UnknownProvider,
63            };
64            out.diags.push(Diagnostic::error(NoEntry { key: key.to_string(), reason: missing.reason() }, span.clone()));
65        }
66    }
67}