use serde_json::Value;
pub fn ok(value: &Value, pretty: bool) -> ! {
let rendered = if pretty {
serde_json::to_string_pretty(value)
} else {
serde_json::to_string(value)
};
match rendered {
Ok(rendered) => {
println!("{rendered}");
std::process::exit(0);
}
Err(e) => fail("internal", &format!("serializing output: {e}"), 1),
}
}
pub fn fail(kind: &str, message: &str, code: i32) -> ! {
let body = serde_json::json!({ "error": { "kind": kind, "message": message } });
eprintln!("{body}");
std::process::exit(code);
}
pub fn fail_engine(e: &topodb::TopoError) -> ! {
match e {
topodb::TopoError::Rejected(_) => fail("rejected", &e.to_string(), 2),
topodb::TopoError::Busy => fail("busy", &e.to_string(), 3),
_ => fail("internal", &e.to_string(), 1),
}
}
pub fn render(json: &Value, text: Option<String>, text_mode: bool, pretty: bool) -> ! {
if text_mode {
if let Some(t) = text {
println!("{t}");
std::process::exit(0);
}
}
ok(json, pretty)
}
pub fn empty_scope_echo(scope: &str, source: &str) {
eprintln!("topodb: 0 matches in scope {scope} (source: {source})");
}
pub fn time_filter_echo(desc: &str, source: &str) {
eprintln!("topodb: time filter: {desc} (source: {source})");
}