use super::super::{Context, args};
use super::act;
use crate::boundary::{Action, Gesture};
use crate::control::judge::{Answer, Ruling, Scope};
pub(in crate::boundary::line) fn answer(
tail: &str,
ctx: &Context,
verb: &str,
) -> Result<Gesture, String> {
let said = args::required(tail, verb, "pass, hold or refuse")?;
let (word, scope) = match said.split_once("--scope") {
Some((word, said)) => (word.trim().to_owned(), scope_of(said.trim(), verb)?),
None => (said, Scope::Call),
};
let ruling = Ruling::of(&word)
.ok_or_else(|| format!("/{verb}: unknown verdict {word:?}; usage: {ANSWER_USAGE}"))?;
Ok(act(Action::AnswerHold {
workspace: args::workspace(ctx, verb)?,
agent: args::agent(ctx, verb)?,
answer: Answer { ruling, scope },
}))
}
fn scope_of(said: &str, verb: &str) -> Result<Scope, String> {
Scope::of(said).ok_or_else(|| format!("/{verb}: unknown scope {said:?}; usage: {ANSWER_USAGE}"))
}
pub fn spell_answer(answer: Answer) -> String {
match answer.scope {
Scope::Call => format!("/answer {}", answer.ruling.word()),
scope => format!("/answer {} --scope {}", answer.ruling.word(), scope.word()),
}
}
pub const ANSWER_USAGE: &str =
"/answer pass | hold | refuse [--scope call | conversation | workspace]";
pub(in crate::boundary::line) fn floor(
verb: &str,
tail: &str,
ctx: &Context,
) -> Result<Gesture, String> {
args::none(tail, verb)?;
Ok(act(Action::Floor {
workspace: args::workspace(ctx, verb)?,
agent: args::agent(ctx, verb)?,
raised: verb == "revoke",
}))
}