Skip to main content

callisto_cli/
output.rs

1use std::io;
2
3use serde::Serialize;
4
5use crate::cli::OutputFormat;
6
7pub fn write_json<W: io::Write, S: Serialize + ?Sized>(w: &mut W, val: &S) -> io::Result<()> {
8    let text = serde_json::to_string_pretty(val)?;
9    writeln!(w, "{text}")
10}
11
12pub fn log_line(format: OutputFormat, line: &str) {
13    match format {
14        OutputFormat::Json => eprintln!("{line}"),
15        OutputFormat::Text => println!("{line}"),
16    }
17}
18
19/// Trait implemented by CLI report structures to guarantee clean JSON stream splitting and diagnostic card formatting.
20pub trait ReportPresenter: Serialize {
21    fn present_json<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
22        write_json(w, self)
23    }
24
25    fn present_human(&self) -> String;
26}