pub struct CdPolicy { /* private fields */ }Expand description
A cd policy; see the module documentation for the contract.
use shellgame::engine::prelude::*;
// Level 1.12: from the deepest directory, after `pwd`, one exact command.
let drill = policy()
.from_dir("gamma/deep/a/b/c")
.after(Marker::Pwd, "Nejdřív použijte příkaz 'pwd'.")
.exact("../../../..", "Použijte jeden příkaz 'cd ../../../..'.");Implementations§
Source§impl CdPolicy
impl CdPolicy
Sourcepub fn from_dir(self, path: impl Into<String>) -> Self
pub fn from_dir(self, path: impl Into<String>) -> Self
Scope: only moves that start in this section-relative directory.
Sourcepub fn target(self, value: impl Into<String>) -> Self
pub fn target(self, value: impl Into<String>) -> Self
Scope: only moves whose raw target is exactly this text (such as "-").
Sourcepub fn exact(
self,
command: impl Into<String>,
message: impl Into<String>,
) -> Self
pub fn exact( self, command: impl Into<String>, message: impl Into<String>, ) -> Self
Rule: the target must equal command, compared after stripping whitespace and
trailing slashes (unless the whole target is /), so ../ is ...
Sourcepub fn absolute(self, message: impl Into<String>) -> Self
pub fn absolute(self, message: impl Into<String>) -> Self
Rule: the target must be an absolute path (start with /). A bare cd fails it.
Sourcepub fn source(self, path: impl Into<String>, message: impl Into<String>) -> Self
pub fn source(self, path: impl Into<String>, message: impl Into<String>) -> Self
Rule: the move must start in this section-relative directory.
Sourcepub fn after(self, marker: Marker, message: impl Into<String>) -> Self
pub fn after(self, marker: Marker, message: impl Into<String>) -> Self
Rule: this marker must exist, so an earlier drill (such as Marker::Pwd) must
have been performed first.
Sourcepub fn within(self, path: impl Into<String>, message: impl Into<String>) -> Self
pub fn within(self, path: impl Into<String>, message: impl Into<String>) -> Self
Rule: the destination must stay inside this section-relative directory.
Sourcepub fn rule(self, rule: fn(&CdMove<'_>) -> Result<(), String>) -> Self
pub fn rule(self, rule: fn(&CdMove<'_>) -> Result<(), String>) -> Self
Rule: a custom check. It runs before the move (with CdMove::dest None) and
again, for evidence, after it (with dest set), and returns the block message on
failure. Custom rules produce their own messages, which a bare cd never replaces.
Sourcepub fn bare_cd(self, message: impl Into<String>) -> Self
pub fn bare_cd(self, message: impl Into<String>) -> Self
The message to show instead of a built-in rule’s when the move is a bare cd (which
goes home, outside wherever the level is).
Sourcepub fn arrive(self, path: impl Into<String>) -> Self
pub fn arrive(self, path: impl Into<String>) -> Self
Record evidence only when the move lands in this section-relative directory, not for
any move that satisfies the rules (such as cd /, which is absolute but not the
target).
Sourcepub fn on_move(self, on_move: fn(&CdMove<'_>, &Markers<'_>)) -> Self
pub fn on_move(self, on_move: fn(&CdMove<'_>, &Markers<'_>)) -> Self
Run after every successful move (see the contract), with the markers of the level.
Sourcepub fn manual_evidence(self) -> Self
pub fn manual_evidence(self) -> Self
Never record evidence automatically; CdPolicy::on_move decides when the drill is
done (by creating the level’s Marker::Cd itself).
Sourcepub fn after_markers(&self) -> impl Iterator<Item = Marker> + '_
pub fn after_markers(&self) -> impl Iterator<Item = Marker> + '_
The markers read by after rules, so preparing the level clears them.
Sourcepub fn pre_move(&self, level: &Level, mv: &CdMove<'_>) -> CdDecision
pub fn pre_move(&self, level: &Level, mv: &CdMove<'_>) -> CdDecision
Decide whether a move that is about to happen is allowed. See the module documentation for the contract.
Sourcepub fn post_move(&self, level: &Level, mv: &CdMove<'_>)
pub fn post_move(&self, level: &Level, mv: &CdMove<'_>)
Record the outcome of a move that succeeded: evidence (unless it is manual), then
CdPolicy::on_move. mv.dest must be the directory the shell ended up in and
mv.pwd the directory it started in. See the module documentation.
Sourcepub fn legacy_pre_move(&self, level: &Level, mv: &CdMove<'_>) -> CdDecision
pub fn legacy_pre_move(&self, level: &Level, mv: &CdMove<'_>) -> CdDecision
CdPolicy::pre_move for a shell started by the Python version of the game.
That shell reports a move only before it happens, and the Python hook recorded the
evidence right then. So when the move is allowed because it is in scope, not lifted
and every rule passes, and the policy has no arrive directory and no manual
evidence, the evidence is recorded immediately. (The new hook records it after the
move, so a move that fails in the shell earns nothing.)
Sourcepub fn legacy_post_move(&self, level: &Level, ctx: &Ctx<'_>, dest: &Path)
pub fn legacy_post_move(&self, level: &Level, ctx: &Ctx<'_>, dest: &Path)
CdPolicy::post_move for a shell started by the Python version of the game, which
reports only the directory it ended up in (cd-hook --post-move $PWD). Evidence is
recorded when the policy has an arrive directory and dest is it (the pre-move
report covers every other policy), then on_move runs with pwd set to dest.