OXYGENHUB
# e071e91a14ef spectra / src / cli / json.rs
857 B · Rust · last changed Project skeleton in Oxygen's shape: workspace, deps/oxygen pinned, make check, version, deps-check (#1) 2 days ago source blame history raw
1
#![allow(dead_code)] // Shared with oxy: not every helper is used yet.
2
//! Tiny JSON building for `--json` outputs: one document on stdout,
3
//! nothing else, with string escaping shared with the hooks payloads.
4
5
pub use oxy_core::repo::{json_array as arr, json_str as s};
6
7
pub fn obj<I: IntoIterator<Item = (&'static str, String)>>(pairs: I) -> String {
8
    let inner: Vec<String> = pairs.into_iter().map(|(k, v)| format!("{}:{v}", s(k))).collect();
9
    format!("{{{}}}", inner.join(","))
10
}
11
pub fn b(v: bool) -> String {
12
    v.to_string()
13
}
14
pub fn n<T: std::fmt::Display>(v: T) -> String {
15
    v.to_string()
16
}
17
pub fn opt<T>(v: Option<T>, f: impl Fn(T) -> String) -> String {
18
    match v {
19
        Some(x) => f(x),
20
        None => "null".into(),
21
    }
22
}
23
pub fn hashes(v: &[oxy_core::hash::ChangeHash]) -> String {
24
    arr(v.iter().map(|h| s(&h.to_hex())))
25
}