pub struct Answer { /* private fields */ }Expand description
A rule that judges a submitted answer. See the module documentation.
use shellgame::engine::answer::{exact, int, parts};
let rule = exact("navigator")
.ignore_case()
.mistake("toto není heslo", "To je obsah decoy.txt, ne password.txt.")
.error("Heslo není správné.");
let both = parts(",", [int(3), exact("log.txt")]).format_message("Formát: počet,soubor");Implementations§
Source§impl Answer
impl Answer
Sourcepub fn ignore_case(self) -> Self
pub fn ignore_case(self) -> Self
Compare case-insensitively (Unicode lowercase on both sides).
Sourcepub fn trim_trailing_slash(self) -> Self
pub fn trim_trailing_slash(self) -> Self
After stripping whitespace, also drop trailing /, so delta/ counts as delta.
Sourcepub fn mistake(
self,
key: impl Into<MistakeKey>,
message: impl Into<String>,
) -> Self
pub fn mistake( self, key: impl Into<MistakeKey>, message: impl Into<String>, ) -> Self
A tailored message for one specific wrong answer. Text rules match it exactly after
the rule’s normalization (strip, trailing slash, case folding); for int the key
is an integer. Mistakes are checked before the correct answer and in the order
declared.
Sourcepub fn feedback(self, feedback: fn(&str) -> Option<String>) -> Self
pub fn feedback(self, feedback: fn(&str) -> Option<String>) -> Self
Computed feedback for a wrong answer. It runs after literal mistakes and after the
correct-answer check, receives the normalized answer (stripped, trailing slash
trimmed if enabled, case as typed) and returns Some(message) to override the
error text, or None to fall through to it.
Sourcepub fn error(self, message: impl Into<String>) -> Self
pub fn error(self, message: impl Into<String>) -> Self
The message for a wrong answer, instead of messages::INCORRECT. It must not
reveal the answer.
Sourcepub fn invalid(self, message: impl Into<String>) -> Self
pub fn invalid(self, message: impl Into<String>) -> Self
The message when an int answer is not a number, instead of
messages::EXPECTED_INTEGER.
Sourcepub fn required(self, message: impl Into<String>) -> Self
pub fn required(self, message: impl Into<String>) -> Self
The message when the player submits no answer at all, instead of
messages::ANSWER_REQUIRED (messages::ANSWER_REQUIRED_NUMBER for int).
Sourcepub fn format_message(self, message: impl Into<String>) -> Self
pub fn format_message(self, message: impl Into<String>) -> Self
The message of a parts answer with the wrong number of parts, instead of
messages::ANSWER_FORMAT. Not the same as Answer::error, which parts answers
do not support: a wrong value is judged by the parts themselves.
Sourcepub fn secret(self) -> Self
pub fn secret(self) -> Self
Mark the answer as a secret: the catalog lint then checks that it never appears in
hints or in a shellgame submit <answer> example in the text.
Sourcepub fn accepts<I, S>(self, examples: I) -> Self
pub fn accepts<I, S>(self, examples: I) -> Self
Example answers that must be accepted. Checked by the catalog lint, never at run time; they document (and pin) what the rule means.
Sourcepub fn rejects<I, S>(self, examples: I) -> Self
pub fn rejects<I, S>(self, examples: I) -> Self
Example answers that must be rejected. Checked by the catalog lint.
Sourcepub fn check(&self, ctx: &Ctx<'_>, answer: &str) -> Check
pub fn check(&self, ctx: &Ctx<'_>, answer: &str) -> Check
Judge an answer. Feedback is data: a wrong answer is Check::Fail(text).
Sourcepub fn required_message(&self) -> &str
pub fn required_message(&self) -> &str
What to tell a player who submits nothing for a level that needs an answer.
Sourcepub fn is_dynamic(&self) -> bool
pub fn is_dynamic(&self) -> bool
True when the answer (or one of its parts) depends on the situation, so it cannot be
known without a Ctx.
Sourcepub fn is_secret(&self) -> bool
pub fn is_secret(&self) -> bool
True when marked Answer::secret.
Sourcepub fn static_answer(&self) -> Option<String>
pub fn static_answer(&self) -> Option<String>
One correct answer, as a player would type it, when it is known statically: the
expected text, the first of one_of, the number, the suffix itself, the parts
joined by the separator. None for exact_with (and anything containing it).
Sourcepub fn accepted_examples(&self) -> &[String]
pub fn accepted_examples(&self) -> &[String]
The accepts examples of this rule (not of its parts).
Sourcepub fn rejected_examples(&self) -> &[String]
pub fn rejected_examples(&self) -> &[String]
The rejects examples of this rule (not of its parts).
Sourcepub fn expected_texts(&self) -> Vec<String>
pub fn expected_texts(&self) -> Vec<String>
Every statically known correct answer in this tree (all accepted values, numbers, suffixes, each part’s, and the joined text of every parts rule).
Sourcepub fn message_texts(&self) -> Vec<&str>
pub fn message_texts(&self) -> Vec<&str>
Every message this tree can show a player, apart from computed feedback.