Skip to main content

minco_interaction/
workflow.rs

1/// One explicitly compiled transition in a domain-owned state machine.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub struct TransitionRule<S> {
4    pub current: S,
5    pub target: S,
6}
7
8impl<S> TransitionRule<S> {
9    #[must_use]
10    pub const fn new(current: S, target: S) -> Self {
11        Self { current, target }
12    }
13}
14
15/// Returns whether a transition is an idempotent no-op or is present in the
16/// supplied static table. This is intentionally not a runtime workflow engine.
17#[must_use]
18pub fn transition_allowed<S: PartialEq>(
19    current: &S,
20    target: &S,
21    rules: &[TransitionRule<S>],
22) -> bool {
23    current == target
24        || rules
25            .iter()
26            .any(|rule| &rule.current == current && &rule.target == target)
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn only_static_or_idempotent_transitions_are_allowed() {
35        let rules = [TransitionRule::new("new", "open")];
36        assert!(transition_allowed(&"new", &"new", &rules));
37        assert!(transition_allowed(&"new", &"open", &rules));
38        assert!(!transition_allowed(&"open", &"new", &rules));
39    }
40}