use std::collections::BTreeMap; use eyre::{bail, Context}; use serde::Serialize; use serde_json::Value; #[derive(Default)] pub struct Output { elements: BTreeMap<&'static str, Value>, } impl Output { pub fn write(&mut self, key: &'static str, v: &impl Serialize) -> eyre::Result<()> { let v = serde_json::to_value(v).wrap_err("failed to serialize output")?; if self.elements.insert(key, v).is_some() { bail!("duplicated output key {key:?}"); } Ok(()) } } impl Drop for Output { fn drop(&mut self) { let out = serde_json::to_string_pretty(&self.elements).expect("can serialize output as json"); println!("{out}",); } }