Skip to main content

Policy

Trait Policy 

Source
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§

Source

fn is_allowed(&self, from: &M, to: &M) -> bool

Returns true if transitioning from from to to is permitted.

Provided Methods§

Source

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.

Implementors§

Source§

impl<M> Policy<M> for AllowAll

Source§

impl<M> Policy<M> for DenyAll