OXYGENHUB
◆ main oxygen / src / cli / json.rs
813 B · Rust · last changed Unbound hosted repositories adopt the first pusher's id 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
8
        .into_iter()
9
        .map(|(k, v)| format!("{}:{v}", s(k)))
10
        .collect();
11
    format!("{{{}}}", inner.join(","))
12
}
13
pub fn b(v: bool) -> String {
14
    v.to_string()
15
}
16
pub fn n<T: std::fmt::Display>(v: T) -> String {
17
    v.to_string()
18
}
19
pub fn opt<T>(v: Option<T>, f: impl Fn(T) -> String) -> String {
20
    match v {
21
        Some(x) => f(x),
22
        None => "null".into(),
23
    }
24
}
25
pub fn hashes(v: &[oxy_core::hash::ChangeHash]) -> String {
26
    arr(v.iter().map(|h| s(&h.to_hex())))
27
}