use serde_json::Value;
pub(crate) fn get_str<'a>(val: &'a Value, key: &str) -> anyhow::Result<&'a str> {
val.get(key)
.and_then(Value::as_str)
.ok_or_else(|| anyhow::anyhow!("Missing required field: {key}"))
}
pub(crate) fn get_opt_str<'a>(val: &'a Value, key: &str) -> Option<&'a str> {
val.get(key).and_then(Value::as_str)
}
pub(crate) fn get_bool(val: &Value, key: &str, default: bool) -> bool {
val.get(key).and_then(Value::as_bool).unwrap_or(default)
}
pub(crate) fn get_opt_i64(val: &Value, key: &str) -> Option<i64> {
val.get(key).and_then(Value::as_i64)
}
pub(crate) fn get_opt_u64(val: &Value, key: &str) -> Option<u64> {
val.get(key).and_then(Value::as_u64)
}
pub(crate) fn get_usize(val: &Value, key: &str, default: usize) -> usize {
val.get(key)
.and_then(Value::as_u64)
.map_or(default, |v| usize::try_from(v).unwrap_or(default))
}
pub(crate) fn get_str_array(val: &Value, key: &str) -> Vec<String> {
val.get(key)
.and_then(Value::as_array)
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(String::from))
.collect()
})
.unwrap_or_default()
}
pub(crate) fn get_opt_bool(val: &Value, key: &str) -> Option<bool> {
val.get(key).and_then(Value::as_bool)
}