use crate::types::{Captured, Channel, Label, OutcomeEvent, ReadyAction, RemoteNotice, Summary};
pub trait Reporter {
fn begun(&mut self, targets: usize);
fn started(&mut self, action: &ReadyAction);
fn printed(&mut self, label: &Label, channel: Channel, bytes: &[u8]);
fn finished(&mut self, event: &OutcomeEvent, printed: Captured<'_>);
fn remote(&mut self, notice: &RemoteNotice);
fn summary(&mut self, summary: Summary);
}
impl<R: Reporter + ?Sized> Reporter for &mut R {
fn begun(&mut self, targets: usize) {
(**self).begun(targets);
}
fn started(&mut self, action: &ReadyAction) {
(**self).started(action);
}
fn printed(&mut self, label: &Label, channel: Channel, bytes: &[u8]) {
(**self).printed(label, channel, bytes);
}
fn finished(&mut self, event: &OutcomeEvent, printed: Captured<'_>) {
(**self).finished(event, printed);
}
fn remote(&mut self, notice: &RemoteNotice) {
(**self).remote(notice);
}
fn summary(&mut self, summary: Summary) {
(**self).summary(summary);
}
}
#[cfg(test)]
mod tests {
#![expect(
clippy::unwrap_used,
reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
)]
use std::collections::BTreeMap;
use std::path::PathBuf;
use super::Reporter;
use crate::types::{
ActionOutcome, Argument, Captured, Channel, Command, Diagnostic, Directory, Label,
NetworkAccess, OutcomeClass, OutcomeEvent, OutputSource, Provenance, ReadyAction,
RemoteCall, RemoteNotice, Summary, Tally, TargetRole, Timestamp,
};
#[derive(Default)]
struct Names(Vec<&'static str>);
impl Reporter for Names {
fn begun(&mut self, _targets: usize) {
self.0.push("begun");
}
fn started(&mut self, _action: &ReadyAction) {
self.0.push("started");
}
fn printed(&mut self, _label: &Label, _channel: Channel, _bytes: &[u8]) {
self.0.push("printed");
}
fn finished(&mut self, _event: &OutcomeEvent, _printed: Captured<'_>) {
self.0.push("finished");
}
fn remote(&mut self, _notice: &RemoteNotice) {
self.0.push("remote");
}
fn summary(&mut self, _summary: Summary) {
self.0.push("summary");
}
}
fn report_once<R: Reporter>(mut reporter: R) {
let label = Label::parse("//:app").unwrap();
let command = Command::new(vec![Argument::parse("cc").unwrap()]).unwrap();
reporter.begun(1);
reporter.started(&ReadyAction {
label: label.clone(),
command: command.clone(),
description: None,
env: BTreeMap::new(),
inputs: Vec::new(),
deps: Vec::new(),
outputs: Vec::new(),
network: NetworkAccess::Sealed,
output_source: OutputSource::Command,
});
reporter.printed(&label, Channel::Stdout, b"hi");
reporter.finished(
&OutcomeEvent {
at: Timestamp::UNIX_EPOCH,
provenance: Provenance::new(PathBuf::from("build.lua"), Directory::root()),
command,
key: None,
started: None,
finished: None,
outcome: ActionOutcome {
label,
role: TargetRole::Build,
class: OutcomeClass::Built,
diagnostic: None,
outputs: Vec::new(),
stdout: None,
stderr: None,
},
},
Captured::default(),
);
reporter.remote(&RemoteNotice::Failed {
call: RemoteCall::Lookup,
diagnostic: Diagnostic::new("unreachable"),
});
reporter.summary(Summary {
all: Tally {
failed: 0,
skipped: 0,
built: 1,
cached: 0,
fetched: 0,
remote: 0,
},
tests: Tally::default(),
});
}
#[test]
fn a_borrowed_reporter_receives_every_call_made_through_the_borrow() {
let mut names = Names::default();
report_once(&mut names);
assert_eq!(
names.0,
[
"begun", "started", "printed", "finished", "remote", "summary"
]
);
}
}