OXYGENHUB
# 89a1d0243544 oxygen / src / cli / json.rs
786 B · Rust · last changed Machine interface for editors (spec 0.8b): oxy root, cat, version, hunks, discard by path or hunk; --json on log (with --path), show, diff, resolve, op log, bookmark list, remote list, fetch, pull, push, submodule status, bisect status, verify, policy show; commit --hunks; integration test over every JSON command 1 month ago source blame history raw
1
//! Tiny JSON building for `--json` outputs: one document on stdout,
2
//! nothing else, with string escaping shared with the hooks payloads.
3
4
pub use oxy_core::repo::{json_array as arr, json_str as s};
5
6
pub fn obj<I: IntoIterator<Item = (&'static str, String)>>(pairs: I) -> String {
7
    let inner: Vec<String> = pairs.into_iter().map(|(k, v)| format!("{}:{v}", s(k))).collect();
8
    format!("{{{}}}", inner.join(","))
9
}
10
pub fn b(v: bool) -> String {
11
    v.to_string()
12
}
13
pub fn n<T: std::fmt::Display>(v: T) -> String {
14
    v.to_string()
15
}
16
pub fn opt<T>(v: Option<T>, f: impl Fn(T) -> String) -> String {
17
    match v {
18
        Some(x) => f(x),
19
        None => "null".into(),
20
    }
21
}
22
pub fn hashes(v: &[oxy_core::hash::ChangeHash]) -> String {
23
    arr(v.iter().map(|h| s(&h.to_hex())))
24
}