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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//! # ready-active-safe
//!
//! A small lifecycle engine for externally driven systems.
//!
//! Most real systems do not have an "anything can go anywhere" state graph. They have a lifecycle.
//! They start up, run, and shut down. When things go wrong, they go safe and recover.
//!
//! This crate gives you a tiny kernel for that shape of problem.
//! You write one pure function, [`Machine::on_event`], and you get back a [`Decision`].
//! The decision is plain data, not effects. Your runtime stays yours.
//!
//! This crate is not a general purpose state machine framework. It is a lifecycle engine.
//!
//! ## What You Write
//!
//! - `Mode`: lifecycle phases like Ready, Active, Safe
//! - `Event`: input from the outside world
//! - `Command`: work for your runtime to execute
//!
//! ## Feature Flags
//!
//! | Feature | Default | Requires | Description |
//! |-----------|---------|----------|-------------|
//! | `full` | Yes | none | Enables all features below |
//! | `std` | No* | none | Standard library support |
//! | `runtime` | No* | `std` | Event loop and command dispatch |
//! | `time` | No* | none | Clock, instant, and deadline types |
//! | `journal` | No* | `std` | Transition recording and replay |
//!
//! *Enabled by default through the `full` feature.
//!
//! ## Module Overview
//!
//! - **Root types** ([`Machine`], [`Decision`], [`ModeChange`], [`Policy`]):
//! Always available, `no_std`-compatible. These are the core contracts.
//! - [`LifecycleError`]: transition failure errors.
//! - **[`time`]** *(feature `time`)*: Clock and deadline abstractions.
//! - **[`runtime`]** *(feature `runtime`)*: Event acceptance and command dispatch.
//! - **[`journal`]** *(feature `journal`)*: Transition recording and replay.
//!
//! ## Examples
//!
//! Run any example with `cargo run --example <name>`. Suggested reading order:
//!
//! 1. **`basic`** — core API: `Machine`, `Decision`, `stay()`, `transition()`, `emit()`
//! 2. **`openxr_session`** — real-world domain mapping (XR session lifecycle)
//! 3. **`recovery`** — `Policy` enforcement and `apply_checked`
//! 4. **`runner`** — `Runner` event loop with policy denial handling
//! 5. **`recovery_cycle`** — repeated fault/recovery with external retry logic
//! 6. **`channel_runtime`** — multi-threaded event feeding over channels
//! 7. **`metrics`** — observability wrapper around `Runner`
//! 8. **`replay`** — journal recording and deterministic replay
//!
//! ## Quick Start
//!
//! ```rust
//! use ready_active_safe::prelude::*;
//!
//! #[derive(Debug, Clone, PartialEq, Eq)]
//! enum Mode {
//! Ready,
//! Active,
//! Safe,
//! }
//!
//! #[derive(Debug)]
//! enum Event {
//! Start,
//! Stop,
//! Fault,
//! }
//!
//! #[derive(Debug)]
//! enum Command {
//! Initialize,
//! Shutdown,
//! }
//!
//! struct System;
//!
//! impl Machine for System {
//! type Mode = Mode;
//! type Event = Event;
//! type Command = Command;
//!
//! fn initial_mode(&self) -> Mode { Mode::Ready }
//!
//! fn on_event(&self, mode: &Mode, event: &Event) -> Decision<Mode, Command> {
//! use Command::*;
//! use Event::*;
//! use Mode::*;
//!
//! match (mode, event) {
//! (Ready, Start) => transition(Active).emit(Initialize),
//! (Active, Stop | Fault) => transition(Safe).emit(Shutdown),
//! _ => ignore(),
//! }
//! }
//! }
//!
//! let system = System;
//! let mut mode = system.initial_mode();
//!
//! let decision = system.decide(&mode, &Event::Start);
//! assert!(decision.is_transition());
//! assert_eq!(decision.target_mode(), Some(&Mode::Active));
//! ```
extern crate alloc;
// Core modules (always available)
// Feature-gated modules
// Public API re-exports
pub use LifecycleError;
pub use ;
/// Convenience re-exports for common imports.
/// Asserts that a decision transitions to the expected mode.
///
/// Produces a clear failure message when the assertion fails.
///
/// # Examples
///
/// ```
/// use ready_active_safe::prelude::*;
/// use ready_active_safe::assert_transitions_to;
///
/// let d: Decision<&str, ()> = transition("active");
/// assert_transitions_to!(d, "active");
/// ```
/// Asserts that a decision stays in the current mode.
///
/// Produces a clear failure message when the assertion fails.
///
/// # Examples
///
/// ```
/// use ready_active_safe::prelude::*;
/// use ready_active_safe::assert_stays;
///
/// let d: Decision<&str, ()> = stay();
/// assert_stays!(d);
/// ```
/// Asserts that a decision emits the expected commands.
///
/// Compares the full command list in order. For partial checks,
/// use standard assertions on `decision.commands()` directly.
///
/// # Examples
///
/// ```
/// use ready_active_safe::prelude::*;
/// use ready_active_safe::assert_emits;
///
/// let d: Decision<(), &str> = stay().emit("init").emit("begin");
/// assert_emits!(d, ["init", "begin"]);
/// ```