Skip to main content

CdPolicy

Struct CdPolicy 

Source
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

Source

pub fn from_dir(self, path: impl Into<String>) -> Self

Scope: only moves that start in this section-relative directory.

Source

pub fn target(self, value: impl Into<String>) -> Self

Scope: only moves whose raw target is exactly this text (such as "-").

Source

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 ...

Source

pub fn absolute(self, message: impl Into<String>) -> Self

Rule: the target must be an absolute path (start with /). A bare cd fails it.

Source

pub fn source(self, path: impl Into<String>, message: impl Into<String>) -> Self

Rule: the move must start in this section-relative directory.

Source

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.

Source

pub fn within(self, path: impl Into<String>, message: impl Into<String>) -> Self

Rule: the destination must stay inside this section-relative directory.

Source

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.

Source

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).

Source

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).

Source

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.

Source

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).

Source

pub fn after_markers(&self) -> impl Iterator<Item = Marker> + '_

The markers read by after rules, so preparing the level clears them.

Source

pub fn paths(&self) -> Vec<&str>

Every section-relative path the policy declares, as written.

Source

pub fn lint(&self) -> Vec<String>

Problems with how the policy was built.

Source

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.

Source

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.

Source

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.)

Source

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.

Trait Implementations§

Source§

impl Clone for CdPolicy

Source§

fn clone(&self) -> CdPolicy

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CdPolicy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CdPolicy

Source§

fn default() -> CdPolicy

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.