Skip to main content

apply_decision_checked

Function apply_decision_checked 

Source
pub fn apply_decision_checked<M, C, E, P>(
    current_mode: M,
    decision: Decision<M, C, E>,
    policy: &P,
) -> Result<(M, Vec<C>), LifecycleError<M>>
where P: Policy<M>,
Expand description

Applies a decision to an owned current mode after checking a policy.

If the decision requests a mode change and the policy denies it, this returns a LifecycleError::TransitionDenied and the commands are dropped.

use ready_active_safe::prelude::*;

#[derive(Debug, Clone, PartialEq, Eq)]
enum Mode {
    Ready,
    Active,
}

struct ForwardOnly;

impl Policy<Mode> for ForwardOnly {
    fn is_allowed(&self, from: &Mode, to: &Mode) -> bool {
        matches!((from, to), (Mode::Ready, Mode::Active))
    }
}

let policy = ForwardOnly;
let decision: Decision<Mode, (), ()> = transition(Mode::Active);

let (mode, _commands) = apply_decision_checked(Mode::Ready, decision, &policy)?;
assert_eq!(mode, Mode::Active);

ยงErrors

Returns LifecycleError::TransitionDenied if the policy rejects the requested mode change.