use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
use nu_protocol::{Example, LabeledError, Signature, SyntaxShape, Value};
use crate::plugin::JevPlugin;
use super::{build_choice, question_to_nu};
pub(crate) struct JevQuestionChoice;
impl SimplePluginCommand for JevQuestionChoice {
type Plugin = JevPlugin;
fn name(&self) -> &str {
"jev question choice"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required("instructions", SyntaxShape::Any, "Decision instructions")
.required("criteria", SyntaxShape::Any, "Option names or descriptions")
}
fn description(&self) -> &str {
"Build a Choice question from a list or record"
}
fn extra_description(&self) -> &str {
"A list of 1 to 255 distinct strings becomes option names with null descriptions. A record supplies explicit descriptions. No HTTP request is made."
}
fn examples(&self) -> Vec<Example<'_>> {
vec![Example {
example: "jev question choice 'Message kind?' [normal promo spam]",
description: "Create a Choice question from option names",
result: None,
}]
}
fn run(
&self,
_plugin: &JevPlugin,
_engine: &EngineInterface,
call: &EvaluatedCall,
_input: &Value,
) -> Result<Value, LabeledError> {
let instructions: Value = call.req(0).map_err(LabeledError::from)?;
let criteria: Value = call.req(1).map_err(LabeledError::from)?;
question_to_nu(build_choice(&instructions, &criteria)?, call.head)
}
}