1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! # Statekit
//!
//! An immutable state-transition validator for predefined workflows.
//!
//! This crate is useful when states are dynamic, configuration-driven, or stored
//! as data, allowing applications to avoid hard-coding state transitions.
//!
//! State and transition invariants are enforced while constructing the machine
//! through [`MachineBuilder`].
//!
//! # Examples
//! ```
//! use statekit::{Machine, StateError};
//!
//! fn main() -> Result<(), StateError> {
//! let machine = Machine::builder()
//! .try_allow("queued", "running")?
//! .try_allow("running", "completed")?
//! .try_allow("running", "failed")?
//! .build()?;
//!
//! machine.validate_transition("queued", "running")?;
//!
//! assert!(machine.can_transition("queued", "running"));
//! assert!(!machine.can_transition("queued", "completed"));
//!
//! let mut instance = machine.instance("queued")?;
//!
//! instance.transition_to("running")?;
//!
//! assert_eq!(instance.state(), "running");
//! assert!(instance.can_transition_to("completed"));
//! assert!(!instance.can_transition_to("queued"));
//!
//! for transition in machine.transitions() {
//! println!("{} -> {}", transition.source(), transition.target());
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! # Invariants
//!
//! - State names must not be empty or consist entirely of whitespace.
//! - State names must not begin or end with Unicode whitespace.
//! - State names are case-sensitive.
//! - Self-transitions are rejected.
//! - Cycles between distinct states are permitted.
//! - A machine must contain at least one transition.
//! - Duplicate transitions between the same source and target are stored as a single logical transition.
//! - Transitions with non-existing edges are rejected.
//! - Instances must be initialized with an existing state.
//! - Instances can only transition to adjacent states.
//!
//! # Additional documentation
//!
//! See the project README for the specification, migration guide, changelog,
//! and benchmark documentation.
pub use MachineBuilder;
pub use StateError;
pub use MachineInstance;
pub use Machine;
pub use Transition;
pub use MachineInner;
pub use StateName;
pub use Transitions;