initial commit

This commit is contained in:
2024-02-08 17:34:21 +01:00
commit 7a8554d313
8 changed files with 1129 additions and 0 deletions

28
src/output.rs Normal file
View File

@@ -0,0 +1,28 @@
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}",);
}
}