pub trait Policy<M> {
// Required method
fn is_allowed(&self, from: &M, to: &M) -> bool;
// Provided method
fn check(&self, from: &M, to: &M) -> Result<(), &'static str> { ... }
}Expand description
Determines whether a mode transition is allowed.
Policies let you enforce transition rules externally, for example by
ensuring that certain modes can only be reached from specific
predecessors. The runtime checks the policy before applying a
ModeChange from a Decision.
When a transition is denied, the current mode is unchanged and any commands in the decision are dropped. Plan accordingly in your runtime if commands must always execute.
§Examples
use ready_active_safe::Policy;
#[derive(Debug, PartialEq)]
enum Mode { Ready, Active, Safe }
struct AllowAll;
impl Policy<Mode> for AllowAll {
fn is_allowed(&self, _from: &Mode, _to: &Mode) -> bool {
true
}
}
let policy = AllowAll;
assert!(policy.is_allowed(&Mode::Ready, &Mode::Active));Required Methods§
Sourcefn is_allowed(&self, from: &M, to: &M) -> bool
fn is_allowed(&self, from: &M, to: &M) -> bool
Returns true if transitioning from from to to is permitted.
Provided Methods§
Sourcefn check(&self, from: &M, to: &M) -> Result<(), &'static str>
fn check(&self, from: &M, to: &M) -> Result<(), &'static str>
Returns Ok(()) if the transition is allowed, or a reason string if denied.
This has a default implementation based on Policy::is_allowed.
Override it when you want a more specific error reason.
§Errors
Returns a static reason string when the transition is denied.