Skip to main content

Engine

Struct Engine 

Source
pub struct Engine<A: Action> { /* private fields */ }
Expand description

The enforcement engine — applies actions to situations with precondition checking.

Implements the .next() pattern with back/forward history:

let engine = Engine::new(initial_situation, preconditions, apply_fn);
let engine = engine.next(action1)?;   // validates + applies
let engine = engine.next(action2)?;   // validates + applies
let engine = engine.back()?;          // undo
let engine = engine.forward()?;       // redo
engine.trace().dump()                 // full history

Implementations§

Source§

impl<A: Action> Engine<A>

Source

pub fn new( situation: A::Sit, preconditions: Vec<Box<dyn Precondition<A>>>, apply_fn: impl Fn(&A::Sit, &A) -> Result<A::Sit, String> + 'static, ) -> Self

Create a new engine with an initial situation, preconditions, and apply function.

Source

pub fn step(&self) -> usize

Current step number (derived from history depth).

Source

pub fn situation(&self) -> &A::Sit

The current situation.

Source

pub fn trace(&self) -> &Trace

The full trace of all actions.

Source

pub fn is_terminal(&self) -> bool

Is the current situation terminal?

Source

pub fn next(self, action: A) -> Result<Self, EngineError<A>>

Apply an action — the .next() method.

Checks all preconditions. If any fail, returns EngineError::Violated. If all pass but apply fails, returns EngineError::LogicalError. Both variants return the engine for rollback.

Source

pub fn back(self) -> Result<Self, Self>

Go back one step. The current situation moves to the redo stack.

Source

pub fn forward(self) -> Result<Self, Self>

Go forward one step (redo). Only available after back().

Source

pub fn back_depth(&self) -> usize

How many steps back are available.

Source

pub fn forward_depth(&self) -> usize

How many steps forward are available (after back).

Source

pub fn try_next(self, action: A) -> Result<Self, Vec<String>>

Try to apply an action, returning the new engine or the violations as strings.

Trait Implementations§

Source§

impl<A: Action> Debug for Engine<A>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<A> Freeze for Engine<A>
where <A as Action>::Sit: Freeze,

§

impl<A> !RefUnwindSafe for Engine<A>

§

impl<A> !Send for Engine<A>

§

impl<A> !Sync for Engine<A>

§

impl<A> Unpin for Engine<A>
where <A as Action>::Sit: Unpin,

§

impl<A> UnsafeUnpin for Engine<A>
where <A as Action>::Sit: UnsafeUnpin,

§

impl<A> !UnwindSafe for Engine<A>

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