use serde_json::Value;
pub fn required_json_string(value: &Value, field: &str, owner: &str) -> Result<String, String> {
value
.get(field)
.and_then(Value::as_str)
.filter(|value| !value.trim().is_empty())
.map(str::to_owned)
.ok_or_else(|| format!("{owner} must have non-empty `{field}` string"))
}
pub fn optional_json_string(value: &Value, field: &str) -> Option<String> {
value
.get(field)
.and_then(Value::as_str)
.filter(|value| !value.trim().is_empty())
.map(str::to_owned)
}
pub fn optional_json_string_any(value: &Value, fields: &[&str]) -> Option<String> {
fields
.iter()
.find_map(|field| optional_json_string(value, field))
}
pub fn optional_json_string_array(value: &Value, field: &str) -> Option<Vec<String>> {
value.get(field).and_then(Value::as_array).map(|items| {
items
.iter()
.filter_map(Value::as_str)
.filter(|item| !item.trim().is_empty())
.map(str::to_owned)
.collect::<Vec<_>>()
})
}
pub fn require_json_array_field<'a>(
value: &'a Value,
field: &str,
owner: &str,
) -> Result<&'a Vec<Value>, String> {
value
.get(field)
.and_then(Value::as_array)
.ok_or_else(|| format!("{owner}.{field} must be an array"))
}
pub fn quoted_platform_values<'a>(values: impl IntoIterator<Item = &'a str>) -> String {
values
.into_iter()
.map(|value| format!("`{value}`"))
.collect::<Vec<_>>()
.join(", ")
}