wasm4pm-compat 26.6.8

Minimal paper-complete, feature-capped Rust process-evidence crate. Start with compatibility. Graduate to execution.
Documentation
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Parallel Workflow Typestate Model.
//!
//! Defined in [workflow.rs](file:///Users/sac/wasm4pm-compat/src/workflow.rs).
//!
//! This module provides a compile-time typestate workflow tracking mechanism for checking
//! process flow topology, transitions, and cancellation branches.
//!
//! ## Representation
//! All types in this module, including [`crate::workflow::BranchToken`], [`crate::workflow::ParallelWorkflow`], [`crate::workflow::JoinPoint`],
//! and the state markers, are zero-sized types (0 bytes). They use [`core::marker::PhantomData`] to carry compile-time
//! structural and generic type information without runtime overhead.
//!
//! ## Structure-only
//! This module does not execute code, spawn threads, or run tasks. It acts strictly as a static shape and
//! transition validator to verify that branches are correctly initialized, progressed, canceled, or joined.
//!
//! ## Graduation
//! While this module ensures correct compile-time workflow structures, it graduates to the dynamic execution,
//! event broker, and orchestrator subsystems of `wasm4pm`.

use std::marker::PhantomData;

// =============================================================================
// 1. Zero-Sized State Markers representing the lifecycle of workflow branches
// =============================================================================

/// The branch is initialized but has not started executing.
///
/// ### Representation
/// A zero-sized state marker [`Pending`](file:///Users/sac/wasm4pm-compat/src/workflow.rs) that occupies 0 bytes of memory.
///
/// ### Structure-only
/// Represents a static state indicating a branch is ready to start. It contains no runtime status,
/// data values, or execution logic.
///
/// ### Graduation
/// In the full `wasm4pm` execution engine, a pending task is placed in a scheduler queue for dynamic dispatch.
/// In this compat crate, it is modeled purely as a compile-time static type parameter.
pub struct Pending;

/// The branch is currently active and executing.
///
/// ### Representation
/// A zero-sized state marker [`Running`](file:///Users/sac/wasm4pm-compat/src/workflow.rs) that occupies 0 bytes of memory.
///
/// ### Structure-only
/// Represents a static state indicating a branch is currently running. It holds no association with threads,
/// tasks, futures, or async runtimes.
///
/// ### Graduation
/// The `wasm4pm` engine tracks running tasks dynamically via host-managed event states and logs.
/// Here, it is represented as a static compile-time type parameter.
pub struct Running;

/// The branch completed its task successfully.
///
/// ### Representation
/// A zero-sized state marker [`Completed`](file:///Users/sac/wasm4pm-compat/src/workflow.rs) that occupies 0 bytes of memory.
///
/// ### Structure-only
/// Represents a static state indicating successful execution completion of a branch. It does not store
/// task results, return values, or logs.
///
/// ### Graduation
/// In `wasm4pm`, branch completion generates trace events and triggers downstream control flows dynamically.
/// In this compat layer, it statically proves path completion for compile-time checking.
pub struct Completed;

/// The branch was canceled by a concurrent cancellation region.
///
/// ### Representation
/// A zero-sized state marker [`Canceled`](file:///Users/sac/wasm4pm-compat/src/workflow.rs) that occupies 0 bytes of memory.
///
/// ### Structure-only
/// Represents a static state indicating a branch was canceled. It carries no cancellation error context
/// or cancellation signals.
///
/// ### Graduation
/// In `wasm4pm`, cancellation propagates dynamically via signals to active tasks, aborting execution.
/// Here, cancellation is a static transition that consumes the token, preventing future completion.
pub struct Canceled;

/// A marker trait to constrain allowed typestate markers.
///
/// ### Representation
/// A marker trait [`BranchState`](file:///Users/sac/wasm4pm-compat/src/workflow.rs) with no runtime footprint, generic parameters, or data fields.
///
/// ### Structure-only
/// Used purely as a compile-time type boundary to restrict state parameters to [`Pending`], [`Running`],
/// [`Completed`], or [`Canceled`]. It provides no functional logic.
///
/// ### Graduation
/// During graduation to the `wasm4pm` execution engine, these compile-time constraints map to dynamic,
/// schema-validated state transition policies.
pub trait BranchState {}
impl BranchState for Pending {}
impl BranchState for Running {}
impl BranchState for Completed {}
impl BranchState for Canceled {}

// =============================================================================
// 2. Linear Branch Token
// =============================================================================

/// An ownership-bearing token representing a specific execution path.
/// Since `_task` and `_state` are `PhantomData`, this struct has a size of 0 bytes.
///
/// ### Representation
/// A zero-sized type [`BranchToken`](file:///Users/sac/wasm4pm-compat/src/workflow.rs) holding a `PhantomData<T>` and `PhantomData<S>`.
/// Its size in memory is 0 bytes.
///
/// ### Structure-only
/// Models compile-time ownership of an abstract execution path. It holds no runtime task references,
/// performs no execution, and does not schedule workloads.
///
/// ### Graduation
/// In `wasm4pm`, a token represents a dynamic work item tracked by the runtime database. In the compat library,
/// it enforces compile-time sequencing and typestate safety.
pub struct BranchToken<T, S: BranchState> {
    pub _task: PhantomData<T>,
    pub _state: PhantomData<S>,
}

impl<T> BranchToken<T, Pending> {
    /// Progresses the branch from Pending to Running.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use wasm4pm_compat::workflow::{BranchToken, Pending, Running};
    /// use std::marker::PhantomData;
    ///
    /// struct MyTask;
    /// let token: BranchToken<MyTask, Pending> = BranchToken {
    ///     _task: PhantomData,
    ///     _state: PhantomData,
    /// };
    /// let running_token: BranchToken<MyTask, Running> = token.start();
    /// ```
    #[inline(always)]
    pub fn start(self) -> BranchToken<T, Running> {
        BranchToken {
            _task: PhantomData,
            _state: PhantomData,
        }
    }
}

impl<T> BranchToken<T, Running> {
    /// Normal successful completion of the branch.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use wasm4pm_compat::workflow::{BranchToken, Running, Completed};
    /// use std::marker::PhantomData;
    ///
    /// struct MyTask;
    /// let token: BranchToken<MyTask, Running> = BranchToken {
    ///     _task: PhantomData,
    ///     _state: PhantomData,
    /// };
    /// let completed_token: BranchToken<MyTask, Completed> = token.complete();
    /// ```
    #[inline(always)]
    pub fn complete(self) -> BranchToken<T, Completed> {
        BranchToken {
            _task: PhantomData,
            _state: PhantomData,
        }
    }
}

// =============================================================================
// 3. Parallel Workflow Carrier (AND-Split State)
// =============================================================================

/// Tracks the status of two concurrent branches (A and B) at compile time.
///
/// ### Representation
/// Struct [`ParallelWorkflow`](file:///Users/sac/wasm4pm-compat/src/workflow.rs) containing `branch_a` and `branch_b` of type `BranchToken<A, SA>` and `BranchToken<B, SB>`
/// respectively. Since both tokens are zero-sized, `ParallelWorkflow` is also zero-sized (0 bytes).
///
/// ### Structure-only
/// Models an AND-split/concurrency pattern between two branches without using OS threads, async futures,
/// or mutexes.
///
/// ### Graduation
/// Graduates to `wasm4pm`'s parallel gateways (BPMN AND-split and AND-join, or Petri Net transitions)
/// which manage dynamic, multi-threaded or distributed scheduling.
pub struct ParallelWorkflow<A, B, SA: BranchState, SB: BranchState> {
    pub branch_a: BranchToken<A, SA>,
    pub branch_b: BranchToken<B, SB>,
}

impl<A, B> ParallelWorkflow<A, B, Pending, Pending> {
    /// Initializes a parallel workflow split (AND-Split).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use wasm4pm_compat::workflow::{ParallelWorkflow, Pending};
    ///
    /// struct TaskA;
    /// struct TaskB;
    /// let workflow: ParallelWorkflow<TaskA, TaskB, Pending, Pending> = ParallelWorkflow::split();
    /// ```
    #[inline(always)]
    pub fn split() -> Self {
        ParallelWorkflow {
            branch_a: BranchToken {
                _task: PhantomData,
                _state: PhantomData,
            },
            branch_b: BranchToken {
                _task: PhantomData,
                _state: PhantomData,
            },
        }
    }
}

// =============================================================================
// 4. State Transitions (Normal & Cancellation Paths)
// =============================================================================

impl<A, B, SB: BranchState> ParallelWorkflow<A, B, Running, SB> {
    /// Completes the task on Branch A.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use wasm4pm_compat::workflow::{ParallelWorkflow, Pending, Running, Completed};
    ///
    /// struct TaskA;
    /// struct TaskB;
    /// let workflow: ParallelWorkflow<TaskA, TaskB, Pending, Pending> = ParallelWorkflow::split();
    /// // Start both branches
    /// let workflow = ParallelWorkflow {
    ///     branch_a: workflow.branch_a.start(),
    ///     branch_b: workflow.branch_b.start(),
    /// };
    /// // Complete branch A
    /// let workflow: ParallelWorkflow<TaskA, TaskB, Completed, Running> = workflow.complete_a();
    /// ```
    #[inline(always)]
    pub fn complete_a(self) -> ParallelWorkflow<A, B, Completed, SB> {
        ParallelWorkflow {
            branch_a: self.branch_a.complete(),
            branch_b: self.branch_b,
        }
    }
}

impl<A, B, SA: BranchState> ParallelWorkflow<A, B, SA, Running> {
    /// Completes the task on Branch B.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use wasm4pm_compat::workflow::{ParallelWorkflow, Pending, Running, Completed};
    ///
    /// struct TaskA;
    /// struct TaskB;
    /// let workflow: ParallelWorkflow<TaskA, TaskB, Pending, Pending> = ParallelWorkflow::split();
    /// // Start both branches
    /// let workflow = ParallelWorkflow {
    ///     branch_a: workflow.branch_a.start(),
    ///     branch_b: workflow.branch_b.start(),
    /// };
    /// // Complete branch B
    /// let workflow: ParallelWorkflow<TaskA, TaskB, Running, Completed> = workflow.complete_b();
    /// ```
    #[inline(always)]
    pub fn complete_b(self) -> ParallelWorkflow<A, B, SA, Completed> {
        ParallelWorkflow {
            branch_a: self.branch_a,
            branch_b: self.branch_b.complete(),
        }
    }
}

impl<A, B> ParallelWorkflow<A, B, Running, Running> {
    /// Fires a cancellation event from Branch A that targets Branch B.
    /// This transition consumes the active `BranchToken<B, Running>` and returns
    /// a `BranchToken<B, Canceled>` token. Because the running token is consumed
    /// and cannot be cloned, Branch B can never be completed.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use wasm4pm_compat::workflow::{ParallelWorkflow, Pending, Running, Completed, Canceled};
    ///
    /// struct TaskA;
    /// struct TaskB;
    /// let workflow: ParallelWorkflow<TaskA, TaskB, Pending, Pending> = ParallelWorkflow::split();
    /// // Start both branches
    /// let workflow = ParallelWorkflow {
    ///     branch_a: workflow.branch_a.start(),
    ///     branch_b: workflow.branch_b.start(),
    /// };
    /// // Cancel Branch B from Branch A
    /// let workflow: ParallelWorkflow<TaskA, TaskB, Completed, Canceled> = workflow.cancel_b_from_a();
    /// ```
    #[inline(always)]
    pub fn cancel_b_from_a(self) -> ParallelWorkflow<A, B, Completed, Canceled> {
        ParallelWorkflow {
            branch_a: BranchToken {
                _task: PhantomData,
                _state: PhantomData,
            },
            branch_b: BranchToken {
                _task: PhantomData,
                _state: PhantomData,
            },
        }
    }
}

// =============================================================================
// 5. Final Synchronization Point (AND-Join)
// =============================================================================

/// Represents a fully joined or synchronized workflow.
///
/// ### Representation
/// Struct [`CompletedWorkflow`](file:///Users/sac/wasm4pm-compat/src/workflow.rs) containing a single private field: `_private: ()`. It is zero-sized and takes 0 bytes.
///
/// ### Structure-only
/// Serves as a terminal state indicating a workflow has successfully synchronized or terminated. It contains no
/// executable code, control flow logic, or data payloads.
///
/// ### Graduation
/// In the `wasm4pm` execution engine, a completed workflow corresponds to a closed process instance or a finished case.
/// Here, it is just a terminal type proving synchronization.
pub struct CompletedWorkflow {
    pub _private: (),
}

/// A synchronization point (AND-Join) namespace for combining concurrent branches.
///
/// ### Representation
/// A zero-sized namespace struct [`JoinPoint`](file:///Users/sac/wasm4pm-compat/src/workflow.rs) that takes 0-bytes of memory.
///
/// ### Structure-only
/// Provides static helper methods to join concurrent branches without runtime synchronization mechanisms
/// like locks, semaphores, or condition variables.
///
/// ### Graduation
/// In the `wasm4pm` engine, synchronization is managed by a database-backed coordinator or actor system
/// evaluating event streams. Here, it is purely a type check.
pub struct JoinPoint;

impl JoinPoint {
    /// Synchronizes the workflow when both branches complete successfully.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use wasm4pm_compat::workflow::{ParallelWorkflow, Pending, Completed, CompletedWorkflow, JoinPoint};
    ///
    /// struct TaskA;
    /// struct TaskB;
    /// let workflow: ParallelWorkflow<TaskA, TaskB, Pending, Pending> = ParallelWorkflow::split();
    /// // Start and complete both branches
    /// let workflow = ParallelWorkflow {
    ///     branch_a: workflow.branch_a.start().complete(),
    ///     branch_b: workflow.branch_b.start().complete(),
    /// };
    /// // Synchronize using JoinPoint
    /// let completed: CompletedWorkflow = JoinPoint::join_success(workflow);
    /// ```
    #[inline(always)]
    pub fn join_success<A, B>(
        _wf: ParallelWorkflow<A, B, Completed, Completed>,
    ) -> CompletedWorkflow {
        CompletedWorkflow { _private: () }
    }

    /// Synchronizes the workflow when Branch B was cancelled.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use wasm4pm_compat::workflow::{ParallelWorkflow, Pending, Completed, Canceled, CompletedWorkflow, JoinPoint};
    ///
    /// struct TaskA;
    /// struct TaskB;
    /// let workflow: ParallelWorkflow<TaskA, TaskB, Pending, Pending> = ParallelWorkflow::split();
    /// // Start both, then cancel branch B from branch A
    /// let workflow = ParallelWorkflow {
    ///     branch_a: workflow.branch_a.start(),
    ///     branch_b: workflow.branch_b.start(),
    /// };
    /// let workflow: ParallelWorkflow<TaskA, TaskB, Completed, Canceled> = workflow.cancel_b_from_a();
    /// // Synchronize using JoinPoint
    /// let completed: CompletedWorkflow = JoinPoint::join_canceled_b(workflow);
    /// ```
    #[inline(always)]
    pub fn join_canceled_b<A, B>(
        _wf: ParallelWorkflow<A, B, Completed, Canceled>,
    ) -> CompletedWorkflow {
        CompletedWorkflow { _private: () }
    }
}