use serde_json::{Map, Value, json};
use crate::control::judge::{Answer, Ruling, Scope};
use super::{Action, Gesture, str_of};
pub(super) fn encode(workspace: &str, agent: &str, answer: Answer) -> Value {
json!({ "op": "answer", "workspace": workspace, "agent": agent,
"verdict": answer.ruling.word(), "scope": answer.scope.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"))?;
let said = str_of(o, "scope")?;
let scope = Scope::of(&said).ok_or_else(|| {
format!("answer: unknown scope {said:?}; say call, conversation or workspace")
})?;
Ok(Gesture::Act(Action::AnswerHold {
workspace,
agent,
answer: Answer { ruling, scope },
}))
}
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",
}))
}