use serde_json::{Map, Value};
use std::path::PathBuf;
use super::{Action, Gesture};
pub(super) fn act(action: Action) -> Gesture {
Gesture::Act(action)
}
pub(super) fn str_of(obj: &Map<String, Value>, key: &str) -> Result<String, String> {
obj.get(key)
.and_then(Value::as_str)
.map(str::to_owned)
.ok_or_else(|| format!("missing or non-string field {key:?}"))
}
pub(super) fn path_of(obj: &Map<String, Value>, key: &str) -> Result<PathBuf, String> {
Ok(PathBuf::from(str_of(obj, key)?))
}
pub(super) fn usize_of(obj: &Map<String, Value>, key: &str) -> Result<usize, String> {
let n = obj
.get(key)
.and_then(Value::as_u64)
.ok_or_else(|| format!("missing or non-integer field {key:?}"))?;
usize::try_from(n).map_err(|_| format!("field {key:?} out of range"))
}
pub(super) fn obj(pairs: &[(&str, &str)]) -> Map<String, Value> {
pairs
.iter()
.map(|(k, v)| ((*k).to_owned(), Value::String((*v).to_owned())))
.collect()
}