pub struct Choice<O> { /* private fields */ }Expand description
A question with one answer from a list of options.
Each option has a name, an optional description, and a value of the type O. The answer is a
ChoiceAnswer, with the probability of each option, and it gives the values back.
choice! makes a Choice that the compiler checks. The constructors make one
from data. A Choice has 1 to 255 options, and each name is different.
§Examples
The smallest Choice has a list of names, and the answer gives a name back:
use system2::Choice;
let team = Choice::new(
"Which team must answer the ticket?",
["billing", "engineering", "support"],
)?;A description tells Jev what an option means:
let teams = [
("billing", "Payments, invoices, and refunds."),
("engineering", "Bugs and outages."),
];
let team = Choice::with_descriptions("Which team must answer the ticket?", teams)?;An option can be a value of the caller. A closure gives its name, or its name and description, and the answer gives the value back:
struct Team {
name: String,
duties: String,
email: String,
}
let teams = vec![Team {
name: "billing".to_string(),
duties: "Payments, invoices, and refunds.".to_string(),
email: "[email protected]".to_string(),
}];
let by_name = Choice::with_values("Which team must answer?", &teams, |team| &team.name)?;
let described = Choice::with_values("Which team must answer?", &teams, |team| {
(&team.name, &team.duties)
})?;An enum that derives Options gives its variants as the options. Each doc comment is a
description:
use system2::Options;
#[derive(Debug, Clone, Copy, PartialEq, Options)]
enum Team {
/// Payments, invoices, and refunds.
Billing,
/// Bugs and outages.
Engineering,
}
let instructions = "Which team must answer the ticket?".to_string();
let team = Choice::<Team>::of(instructions)?;choice! takes the same forms, and the compiler checks them. A question
with no variable can be a constant:
use system2::prelude::*;
use system2::Choice;
#[derive(Debug, Clone, Copy, PartialEq, Options)]
enum Team {
/// Payments, invoices, and refunds.
Billing,
/// Bugs and outages.
Engineering,
}
const TEAM: Choice<Team> = choice!("Which team must answer the ticket?", Team);
const LANGUAGE: Choice<&str> = choice!("Which language is the ticket in?", ["en", "de", "fr"]);
const NEXT: Choice<&str> = choice!("What is the next step?", {
"reply": "Reply to the customer.",
"escalate": "Give the ticket to an engineer.",
});
let team = TEAM.ask("The site is down.").await?;
if *team.most_likely() == Team::Engineering && team.confidence() > 0.8 { /* … */ }Implementations§
Source§impl<O> Choice<O>
impl<O> Choice<O>
Sourcepub fn new(
instructions: impl Serialize,
items: impl IntoIterator<Item = O>,
) -> Result<Self, InvalidChoice>where
O: Display,
pub fn new(
instructions: impl Serialize,
items: impl IntoIterator<Item = O>,
) -> Result<Self, InvalidChoice>where
O: Display,
Makes a Choice with an option for each item. The Display of an item is its name.
The options have no descriptions. The answer gives the items back.
§Errors
InvalidChoice::Instructionsif the instructions do not serialize, are empty, or are a bare number orbool.InvalidChoice::EmptyNameif a name is only whitespace.InvalidChoice::Countif there are no items, or more than 255.InvalidChoice::Duplicateif two items have the same name.
Sourcepub fn with_descriptions<D: Serialize>(
instructions: impl Serialize,
options: impl IntoIterator<Item = (O, D)>,
) -> Result<Self, InvalidChoice>where
O: Display,
pub fn with_descriptions<D: Serialize>(
instructions: impl Serialize,
options: impl IntoIterator<Item = (O, D)>,
) -> Result<Self, InvalidChoice>where
O: Display,
Makes a Choice from (name, description) pairs. The Display of a name is the name.
The pairs can be owned or borrowed, such as a Vec, a &BTreeMap, or a
&serde_json::Map. A description is any Serialize value. A description of () or
None sends no description. The answer gives the name back.
§Errors
InvalidChoice::Instructionsif the instructions do not serialize, are empty, or are a bare number orbool.InvalidChoice::Descriptionif a description breaks a rule of the instructions.InvalidChoice::EmptyNameif a name is only whitespace.InvalidChoice::Countif there are no options, or more than 255.InvalidChoice::Duplicateif two options have the same name.
Sourcepub fn with_values<L: Label>(
instructions: impl Serialize,
values: impl IntoIterator<Item = O>,
label: impl FnMut(&O) -> L,
) -> Result<Self, InvalidChoice>
pub fn with_values<L: Label>( instructions: impl Serialize, values: impl IntoIterator<Item = O>, label: impl FnMut(&O) -> L, ) -> Result<Self, InvalidChoice>
Makes a Choice from the values of the caller, and a closure that gives their labels.
The closure gives a Label: a name, or a name and a description. The values need no
Clone, and the answer gives a value back.
§Errors
InvalidChoice::Instructionsif the instructions do not serialize, are empty, or are a bare number orbool.InvalidChoice::Descriptionif a description breaks a rule of the instructions.InvalidChoice::EmptyNameif a name is only whitespace.InvalidChoice::Countif there are no values, or more than 255.InvalidChoice::Duplicateif two values have the same name.
Sourcepub fn of(instructions: impl Serialize) -> Result<Self, InvalidChoice>where
O: Options,
pub fn of(instructions: impl Serialize) -> Result<Self, InvalidChoice>where
O: Options,
Makes a Choice from instructions that come from data, with the options of the type O.
It validates the options of O again, as Choice::with_values does.
§Errors
InvalidChoice::Instructions if the instructions do not serialize, are empty, or are a
bare number or bool. The derive of Options validates the options, so another variant
does not occur.
Sourcepub const fn tag<T>(self, tag: T) -> Tagged<Self, T>
pub const fn tag<T>(self, tag: T) -> Tagged<Self, T>
Puts a tag next to the question, so that the answer comes back next to it.
It is the same as Ask::tag, but it is a const fn, so a constant can
hold a tagged question.
Sourcepub fn answer(
self,
most_likely: &O,
confidence: f64,
probabilities: impl IntoIterator<Item = f64>,
) -> Result<ChoiceAnswer<O>, InvalidAnswer>where
O: PartialEq,
pub fn answer(
self,
most_likely: &O,
confidence: f64,
probabilities: impl IntoIterator<Item = f64>,
) -> Result<ChoiceAnswer<O>, InvalidAnswer>where
O: PartialEq,
Builds the answer that a response with these values would give.
most_likely is the option that the API chose, confidence is its confidence, and
probabilities has one probability for each option, in the order of the question. It’s
for tests of your own decision code, and it goes through the same checks as a response
from the API.
§Errors
Returns InvalidAnswer if most_likely is not an option of the question, if there is
not one probability for each option, or if a value is not from 0 to 1.
§Examples
use system2::prelude::*;
let team = choice!("Which team should handle this ticket?", ["billing", "support"])
.answer(&"billing", 0.8, [0.9, 0.1])?;
assert_eq!(*team.most_likely(), "billing");