use serde_json::{Map, Value, json};
use crate::monitor::Verb;
use super::{Action, Gesture, str_of};
pub(super) fn encode(verb: &Verb) -> Value {
match verb {
Verb::Arm { workspace, model } => {
json!({ "op": "arm", "workspace": workspace, "model": model })
}
Verb::Disarm { workspace } => {
json!({ "op": "disarm", "workspace": workspace })
}
Verb::Flag {
workspace,
agent,
reason,
} => json!({ "op": "flag", "workspace": workspace,
"agent": agent, "reason": reason }),
}
}
pub(super) fn decode(op: &str, o: &Map<String, Value>) -> Result<Gesture, String> {
let workspace = str_of(o, "workspace")?;
let verb = match op {
"arm" => Verb::Arm {
workspace,
model: str_of(o, "model")?,
},
"disarm" => Verb::Disarm { workspace },
_ => Verb::Flag {
workspace,
agent: str_of(o, "agent")?,
reason: str_of(o, "reason")?,
},
};
Ok(Gesture::Act(Action::Monitor(verb)))
}