Skip to main content

corpora_engine/
reporter.rs

1//! Terminal-side subscribers: [`Gate`] prints diagnostics as they flow past (the running
2//! tally lives in the engine), and [`FailureReporter`] turns `ParseFailed` into a warning
3//! so unparsed files are visible rather than silently dropped.
4
5use corpora_core::subscriber::ek;
6use corpora_core::{Context, Diagnostic, Event, Interest, Subscriber};
7
8pub struct Gate;
9
10impl Subscriber for Gate {
11    fn name(&self) -> &'static str {
12        "gate"
13    }
14
15    fn interest(&self) -> Interest {
16        Interest::only(ek::DIAGNOSTIC)
17    }
18
19    fn handle(&mut self, ev: &Event, _cx: &mut Context<'_>) {
20        if let Event::Diagnostic(d) = ev {
21            eprintln!("{d}");
22        }
23    }
24}
25
26pub struct FailureReporter;
27
28impl Subscriber for FailureReporter {
29    fn name(&self) -> &'static str {
30        "failures"
31    }
32
33    fn interest(&self) -> Interest {
34        Interest::only(ek::PARSE_FAILED)
35    }
36
37    fn handle(&mut self, ev: &Event, cx: &mut Context<'_>) {
38        if let Event::ParseFailed { path, error } = ev {
39            cx.error(Diagnostic::warn("PARSE", path, error.0.clone()));
40        }
41    }
42}