use techy::error::{Diagnostic, DiagnosticInfo};
use crate::core::data::store::StageOut;
use crate::core::plan::PlanCx;
use crate::core::run::StageCx;
use crate::core::stage::{Effect, Needs, Scope, Stage, StageDecl};
use crate::model::element::{element_of, element_value};
use crate::stdlib::features::math::{CONTENT, ENVIRONMENT, MathEnvironment, lines};
pub const CHECK: &str = "flm.math-passthrough.check";
#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
#[diagnostic(
id = "flm.math-passthrough.empty-line",
message = "line {line} (counting from 0) of the `{environment}` environment must hold \
mathematics; a `\\label`, `\\tag` or `\\nonumber` alone is not mathematics"
)]
pub struct EmptyLine {
pub environment: String,
pub line: usize,
}
#[derive(Debug, Default, Clone, Copy)]
pub struct PassthroughCheck;
impl Stage for PassthroughCheck {
fn decl(&self) -> StageDecl {
StageDecl::new(CHECK, Effect::Check, Scope::PerTree).provides(CONTENT).docs(
"The check of passthrough mathematics: it accepts every formula as written, \
reports every equation line that holds no mathematics as \
`flm.math-passthrough.empty-line`, and provides `flm.math.content`.",
)
}
fn needs(&self, _cx: &PlanCx<'_>) -> Needs {
Needs::new()
}
fn run(&self, cx: &StageCx<'_>, out: &mut StageOut<'_>) {
let Some(tree) = cx.my_tree() else { return };
for node in tree.root().descendants() {
if element_of(&node).name != ENVIRONMENT {
continue;
}
let Some(value) = element_value::<MathEnvironment>(&node) else { continue };
for (line, span) in lines::empty_lines(&node) {
let condition = EmptyLine { environment: value.name.to_string(), line };
out.diags.push(Diagnostic::error(condition, span));
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_stage_is_a_per_tree_check_providing_content() {
let decl = PassthroughCheck.decl();
assert_eq!(decl.name, CHECK);
assert_eq!(decl.effect, Effect::Check);
assert_eq!(decl.scope, Scope::PerTree);
assert_eq!(decl.provides, [crate::model::names::CapName::from(CONTENT)]);
assert!(decl.writes.is_empty());
}
}