//! Which choice of a daemon question (`rho_protocol::Question`) an answer's
//! text names. Frontends that take answers as text, an ACP host's form and
//! `rho mcp serve`'s `answer`, resolve it here, and send the daemon the
//! choice's value, which is what it matches.
use rho_protocol::{Choice, Question};
/// The choice `text` names: its exact displayed label first, since people
/// and the models relaying them answer with labels, then its value, then
/// its label in any case.
pub(crate) fn find_choice<'a>(question: &'a Question, text: &str) -> Option<&'a Choice> {
let choices = &question.choices;
choices
.iter()
.find(|choice| choice.label == text)
.or_else(|| choices.iter().find(|choice| choice.value == text))
.or_else(|| {
choices
.iter()
.find(|choice| choice.label.eq_ignore_ascii_case(text))
})
}
/// The value of the choice `text` names by value or label, or `text` itself
/// as the answerer's own answer.
pub(crate) fn choice_value(question: &Question, text: String) -> String {
find_choice(question, &text).map_or(text, |choice| choice.value.clone())
}