#![allow(dead_code)]
use serde_json::{json, Value};
pub fn ok(json_mode: bool, fields: Value) {
if json_mode {
let mut obj = json!({"status": "ok"});
if let (Some(o), Some(f)) = (obj.as_object_mut(), fields.as_object()) {
for (k, v) in f {
o.insert(k.clone(), v.clone());
}
}
println!("{}", serde_json::to_string_pretty(&obj).unwrap());
}
}
pub fn err(json_mode: bool, code: &str, message: &str) {
if json_mode {
let obj = json!({"status":"error","code":code,"message":message});
println!("{}", serde_json::to_string_pretty(&obj).unwrap());
} else {
eprintln!("error [{}]: {}", code, message);
}
}
pub fn load_file(path: &str) -> anyhow::Result<Value> {
let content =
std::fs::read_to_string(path).map_err(|e| anyhow::anyhow!("Cannot read {path}: {e}"))?;
serde_json::from_str(&content).map_err(|e| anyhow::anyhow!("Invalid JSON in {path}: {e}"))
}
pub fn get_str<'a>(v: &'a Value, key: &str) -> Option<&'a str> {
v.get(key)?.as_str()
}
pub fn get_bool(v: &Value, key: &str) -> bool {
v.get(key).and_then(|x| x.as_bool()).unwrap_or(false)
}
pub fn get_str_array(v: &Value, key: &str) -> Vec<String> {
v.get(key)
.and_then(|x| x.as_array())
.map(|arr| {
arr.iter()
.filter_map(|s| s.as_str().map(|s| s.to_string()))
.collect()
})
.unwrap_or_default()
}