Skip to main content

harmont_cli/output/
json.rs

1//! JSON-lines [`OutputRenderer`] — replaces the former
2//! `hm-plugin-output-json` WASM plugin. Serialises each
3//! [`BuildEvent`] as a single JSON line.
4
5use std::fmt;
6use std::io::Write;
7
8use hm_plugin_protocol::BuildEvent;
9
10use crate::runner::OutputRenderer;
11
12/// Renders [`BuildEvent`]s as newline-delimited JSON (one object per
13/// line). Suitable for piping into `jq` or other machine consumers.
14#[derive(Debug)]
15pub struct JsonRenderer<W> {
16    out: W,
17}
18
19impl<W> JsonRenderer<W> {
20    /// Create a new renderer writing to `out`.
21    #[must_use]
22    pub const fn new(out: W) -> Self {
23        Self { out }
24    }
25}
26
27impl<W> OutputRenderer for JsonRenderer<W>
28where
29    W: Write + Send + fmt::Debug,
30{
31    fn on_event(&mut self, event: &BuildEvent) {
32        if let Ok(mut bytes) = serde_json::to_vec(event) {
33            bytes.push(b'\n');
34            let _ = self.out.write_all(&bytes);
35        }
36    }
37}