use serde_json::{Map, Value, json};
use crate::control::judge::Ruling;
use super::{Action, Gesture, str_of};
pub(super) fn encode(workspace: &str, agent: &str, ruling: Ruling) -> Value {
json!({ "op": "answer", "workspace": workspace,
"agent": agent, "verdict": ruling.word() })
}
pub(super) fn decode(o: &Map<String, Value>) -> Result<Gesture, String> {
let workspace = str_of(o, "workspace")?;
let agent = str_of(o, "agent")?;
let word = str_of(o, "verdict")?;
let ruling = Ruling::of(&word)
.ok_or_else(|| format!("answer: unknown verdict {word:?}; say pass, hold or refuse"))?;
Ok(Gesture::Act(Action::AnswerHold {
workspace,
agent,
ruling,
}))
}
pub(super) fn encode_floor(workspace: &str, agent: &str, raised: bool) -> Value {
let op = if raised { "revoke" } else { "restore" };
json!({ "op": op, "workspace": workspace, "agent": agent })
}
pub(super) fn decode_floor(op: &str, o: &Map<String, Value>) -> Result<Gesture, String> {
Ok(Gesture::Act(Action::Floor {
workspace: str_of(o, "workspace")?,
agent: str_of(o, "agent")?,
raised: op == "revoke",
}))
}