use kernel::Address;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Records {
Summary,
Whole,
}
pub(super) fn print(
records: Records,
mut watching: tokio::sync::broadcast::Receiver<wire::Committed>,
) {
std::thread::spawn(move || {
while let Ok(committed) = watching.blocking_recv() {
match printed(committed.record(), records) {
Ok(text) => println!("{text}"),
Err(error) => eprintln!(
" seq {} is in the history but could not be printed here: {error}",
committed.record().seq().value()
),
}
}
});
}
pub(super) fn printed(
record: &kernel::EventRecord,
records: Records,
) -> Result<String, serde_json::Error> {
match records {
Records::Whole => serde_json::to_string(record),
Records::Summary => {
let kind = serde_json::to_value(record.kind())?;
let kind = kind.as_str().unwrap_or("?");
let at = record.addr().map_or("city", Address::as_str);
Ok(format!(" seq {} {kind} {at}", record.seq().value()))
}
}
}