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 historyImplementations§
Source§impl<A: Action> Engine<A>
impl<A: Action> Engine<A>
Sourcepub fn new(
situation: A::Sit,
preconditions: Vec<Box<dyn Precondition<A>>>,
apply_fn: impl Fn(&A::Sit, &A) -> Result<A::Sit, String> + 'static,
) -> Self
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.
Sourcepub fn is_terminal(&self) -> bool
pub fn is_terminal(&self) -> bool
Is the current situation terminal?
Sourcepub fn next(self, action: A) -> Result<Self, EngineError<A>>
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.
Sourcepub fn back(self) -> Result<Self, Self>
pub fn back(self) -> Result<Self, Self>
Go back one step. The current situation moves to the redo stack.
Sourcepub fn forward(self) -> Result<Self, Self>
pub fn forward(self) -> Result<Self, Self>
Go forward one step (redo). Only available after back().
Sourcepub fn back_depth(&self) -> usize
pub fn back_depth(&self) -> usize
How many steps back are available.
Sourcepub fn forward_depth(&self) -> usize
pub fn forward_depth(&self) -> usize
How many steps forward are available (after back).
Trait Implementations§
Auto Trait Implementations§
impl<A> Freeze for Engine<A>
impl<A> !RefUnwindSafe for Engine<A>
impl<A> !Send for Engine<A>
impl<A> !Sync for Engine<A>
impl<A> Unpin for Engine<A>
impl<A> UnsafeUnpin for Engine<A>
impl<A> !UnwindSafe for Engine<A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more