durable-lambda-core 1.2.0

Core replay engine, types, and operation logic for AWS Lambda durable execution in Rust
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Shared trait for all durable context types.
//!
//! [`DurableContextOps`] is the single interface satisfied by every context
//! type in the SDK: [`DurableContext`](crate::context::DurableContext),
//! `ClosureContext`, `TraitContext`, and `BuilderContext`.
//!
//! This trait exists for **static dispatch only** — never use it as `dyn
//! DurableContextOps`. It enables generic handler functions that work with any
//! context flavour without code duplication.

use std::future::Future;

use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::context::DurableContext;
use crate::error::DurableError;
use crate::types::{
    BatchResult, CallbackHandle, CallbackOptions, CompensationResult, ExecutionMode, MapOptions,
    ParallelOptions, StepOptions,
};

/// Shared interface for all durable context types.
///
/// Every context type in the SDK implements this trait by delegating to the
/// underlying [`DurableContext`] or its inherent methods. Use this trait as a
/// generic bound when writing handler logic that should work across all context
/// approaches (closure, trait, builder, or core directly).
///
/// # Static dispatch only
///
/// This trait uses native async (`-> impl Future<...>`) and is designed for
/// static dispatch. Do **not** use `dyn DurableContextOps` — there is no
/// object-safe version.
///
/// # Examples
///
/// ```no_run
/// use durable_lambda_core::DurableContextOps;
/// use durable_lambda_core::error::DurableError;
///
/// async fn process_order<C: DurableContextOps>(ctx: &mut C, order_id: u64) -> Result<(), DurableError> {
///     let _result: Result<String, String> = ctx.step("validate", move || async move {
///         Ok(format!("validated:{order_id}"))
///     }).await?;
///     ctx.log("order processed");
///     Ok(())
/// }
/// ```
pub trait DurableContextOps {
    // -------------------------------------------------------------------------
    // Async operation methods
    // -------------------------------------------------------------------------

    /// Execute a named step with checkpointing.
    ///
    /// See [`DurableContext::step`](crate::context::DurableContext) for full
    /// documentation.
    fn step<T, E, F, Fut>(
        &mut self,
        name: &str,
        f: F,
    ) -> impl Future<Output = Result<Result<T, E>, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        E: Serialize + DeserializeOwned + Send + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, E>> + Send + 'static;

    /// Execute a named step with checkpointing and retry configuration.
    ///
    /// See [`DurableContext::step_with_options`](crate::context::DurableContext) for full
    /// documentation.
    fn step_with_options<T, E, F, Fut>(
        &mut self,
        name: &str,
        options: StepOptions,
        f: F,
    ) -> impl Future<Output = Result<Result<T, E>, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        E: Serialize + DeserializeOwned + Send + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, E>> + Send + 'static;

    /// Suspend execution for the specified duration.
    ///
    /// See [`DurableContext::wait`](crate::context::DurableContext) for full
    /// documentation.
    fn wait(
        &mut self,
        name: &str,
        duration_secs: i32,
    ) -> impl Future<Output = Result<(), DurableError>> + Send;

    /// Register a callback and return a handle with the server-generated callback ID.
    ///
    /// See [`DurableContext::create_callback`](crate::context::DurableContext) for full
    /// documentation.
    fn create_callback(
        &mut self,
        name: &str,
        options: CallbackOptions,
    ) -> impl Future<Output = Result<CallbackHandle, DurableError>> + Send;

    /// Durably invoke another Lambda function and return its result.
    ///
    /// See [`DurableContext::invoke`](crate::context::DurableContext) for full
    /// documentation.
    fn invoke<T, P>(
        &mut self,
        name: &str,
        function_name: &str,
        payload: &P,
    ) -> impl Future<Output = Result<T, DurableError>> + Send
    where
        T: DeserializeOwned + Send,
        P: Serialize + Sync;

    /// Execute multiple branches concurrently and return their results.
    ///
    /// See [`DurableContext::parallel`](crate::context::DurableContext) for full
    /// documentation.
    fn parallel<T, F, Fut>(
        &mut self,
        name: &str,
        branches: Vec<F>,
        options: ParallelOptions,
    ) -> impl Future<Output = Result<BatchResult<T>, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        F: FnOnce(DurableContext) -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, DurableError>> + Send + 'static;

    /// Execute an isolated subflow with its own checkpoint namespace.
    ///
    /// See [`DurableContext::child_context`](crate::context::DurableContext) for full
    /// documentation.
    fn child_context<T, F, Fut>(
        &mut self,
        name: &str,
        f: F,
    ) -> impl Future<Output = Result<T, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send,
        F: FnOnce(DurableContext) -> Fut + Send,
        Fut: Future<Output = Result<T, DurableError>> + Send;

    /// Process a collection of items in parallel and return their results.
    ///
    /// See [`DurableContext::map`](crate::context::DurableContext) for full
    /// documentation.
    fn map<T, I, F, Fut>(
        &mut self,
        name: &str,
        items: Vec<I>,
        options: MapOptions,
        f: F,
    ) -> impl Future<Output = Result<BatchResult<T>, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        I: Send + 'static,
        F: FnOnce(I, DurableContext) -> Fut + Send + 'static + Clone,
        Fut: Future<Output = Result<T, DurableError>> + Send + 'static;

    // -------------------------------------------------------------------------
    // Compensation (saga pattern) methods
    // -------------------------------------------------------------------------

    /// Execute a forward step and register a compensation closure on success.
    ///
    /// See [`DurableContext::step_with_compensation`](crate::context::DurableContext) for full
    /// documentation.
    fn step_with_compensation<T, E, F, Fut, G, GFut>(
        &mut self,
        name: &str,
        forward_fn: F,
        compensate_fn: G,
    ) -> impl Future<Output = Result<Result<T, E>, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        E: Serialize + DeserializeOwned + Send + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, E>> + Send + 'static,
        G: FnOnce(T) -> GFut + Send + 'static,
        GFut: Future<Output = Result<(), DurableError>> + Send + 'static;

    /// Execute a forward step (with options) and register a compensation closure on success.
    ///
    /// See [`DurableContext::step_with_compensation_opts`](crate::context::DurableContext) for full
    /// documentation.
    fn step_with_compensation_opts<T, E, F, Fut, G, GFut>(
        &mut self,
        name: &str,
        options: StepOptions,
        forward_fn: F,
        compensate_fn: G,
    ) -> impl Future<Output = Result<Result<T, E>, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        E: Serialize + DeserializeOwned + Send + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, E>> + Send + 'static,
        G: FnOnce(T) -> GFut + Send + 'static,
        GFut: Future<Output = Result<(), DurableError>> + Send + 'static;

    /// Execute all registered compensations in reverse registration order.
    ///
    /// See [`DurableContext::run_compensations`](crate::context::DurableContext) for full
    /// documentation.
    fn run_compensations(
        &mut self,
    ) -> impl Future<Output = Result<CompensationResult, DurableError>> + Send;

    // -------------------------------------------------------------------------
    // Sync operation method
    // -------------------------------------------------------------------------

    /// Check the result of a previously created callback.
    ///
    /// See [`DurableContext::callback_result`](crate::context::DurableContext) for full
    /// documentation.
    fn callback_result<T: DeserializeOwned>(
        &self,
        handle: &CallbackHandle,
    ) -> Result<T, DurableError>;

    // -------------------------------------------------------------------------
    // State query methods
    // -------------------------------------------------------------------------

    /// Return the current execution mode (Replaying or Executing).
    fn execution_mode(&self) -> ExecutionMode;

    /// Return whether the context is currently replaying from history.
    fn is_replaying(&self) -> bool;

    /// Return a reference to the durable execution ARN.
    fn arn(&self) -> &str;

    /// Return the current checkpoint token.
    fn checkpoint_token(&self) -> &str;

    // -------------------------------------------------------------------------
    // Log methods
    // -------------------------------------------------------------------------

    /// Emit a replay-safe info-level log message.
    fn log(&self, message: &str);

    /// Emit a replay-safe info-level log message with structured data.
    fn log_with_data(&self, message: &str, data: &serde_json::Value);

    /// Emit a replay-safe debug-level log message.
    fn log_debug(&self, message: &str);

    /// Emit a replay-safe warn-level log message.
    fn log_warn(&self, message: &str);

    /// Emit a replay-safe error-level log message.
    fn log_error(&self, message: &str);

    /// Emit a replay-safe debug-level log message with structured data.
    fn log_debug_with_data(&self, message: &str, data: &serde_json::Value);

    /// Emit a replay-safe warn-level log message with structured data.
    fn log_warn_with_data(&self, message: &str, data: &serde_json::Value);

    /// Emit a replay-safe error-level log message with structured data.
    fn log_error_with_data(&self, message: &str, data: &serde_json::Value);

    // -------------------------------------------------------------------------
    // Batch checkpoint methods
    // -------------------------------------------------------------------------

    /// Enable batch checkpoint mode.
    ///
    /// See [`DurableContext::enable_batch_mode`](crate::context::DurableContext) for full
    /// documentation.
    fn enable_batch_mode(&mut self);

    /// Flush accumulated batch checkpoint updates.
    ///
    /// See [`DurableContext::flush_batch`](crate::context::DurableContext) for full
    /// documentation.
    fn flush_batch(&mut self) -> impl Future<Output = Result<(), DurableError>> + Send;
}

impl DurableContextOps for DurableContext {
    fn step<T, E, F, Fut>(
        &mut self,
        name: &str,
        f: F,
    ) -> impl Future<Output = Result<Result<T, E>, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        E: Serialize + DeserializeOwned + Send + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, E>> + Send + 'static,
    {
        DurableContext::step(self, name, f)
    }

    fn step_with_options<T, E, F, Fut>(
        &mut self,
        name: &str,
        options: StepOptions,
        f: F,
    ) -> impl Future<Output = Result<Result<T, E>, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        E: Serialize + DeserializeOwned + Send + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, E>> + Send + 'static,
    {
        DurableContext::step_with_options(self, name, options, f)
    }

    fn wait(
        &mut self,
        name: &str,
        duration_secs: i32,
    ) -> impl Future<Output = Result<(), DurableError>> + Send {
        DurableContext::wait(self, name, duration_secs)
    }

    fn create_callback(
        &mut self,
        name: &str,
        options: CallbackOptions,
    ) -> impl Future<Output = Result<CallbackHandle, DurableError>> + Send {
        DurableContext::create_callback(self, name, options)
    }

    fn invoke<T, P>(
        &mut self,
        name: &str,
        function_name: &str,
        payload: &P,
    ) -> impl Future<Output = Result<T, DurableError>> + Send
    where
        T: DeserializeOwned + Send,
        P: Serialize + Sync,
    {
        DurableContext::invoke(self, name, function_name, payload)
    }

    fn parallel<T, F, Fut>(
        &mut self,
        name: &str,
        branches: Vec<F>,
        options: ParallelOptions,
    ) -> impl Future<Output = Result<BatchResult<T>, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        F: FnOnce(DurableContext) -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, DurableError>> + Send + 'static,
    {
        DurableContext::parallel(self, name, branches, options)
    }

    fn child_context<T, F, Fut>(
        &mut self,
        name: &str,
        f: F,
    ) -> impl Future<Output = Result<T, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send,
        F: FnOnce(DurableContext) -> Fut + Send,
        Fut: Future<Output = Result<T, DurableError>> + Send,
    {
        DurableContext::child_context(self, name, f)
    }

    fn map<T, I, F, Fut>(
        &mut self,
        name: &str,
        items: Vec<I>,
        options: MapOptions,
        f: F,
    ) -> impl Future<Output = Result<BatchResult<T>, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        I: Send + 'static,
        F: FnOnce(I, DurableContext) -> Fut + Send + 'static + Clone,
        Fut: Future<Output = Result<T, DurableError>> + Send + 'static,
    {
        DurableContext::map(self, name, items, options, f)
    }

    fn step_with_compensation<T, E, F, Fut, G, GFut>(
        &mut self,
        name: &str,
        forward_fn: F,
        compensate_fn: G,
    ) -> impl Future<Output = Result<Result<T, E>, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        E: Serialize + DeserializeOwned + Send + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, E>> + Send + 'static,
        G: FnOnce(T) -> GFut + Send + 'static,
        GFut: Future<Output = Result<(), DurableError>> + Send + 'static,
    {
        DurableContext::step_with_compensation(self, name, forward_fn, compensate_fn)
    }

    fn step_with_compensation_opts<T, E, F, Fut, G, GFut>(
        &mut self,
        name: &str,
        options: StepOptions,
        forward_fn: F,
        compensate_fn: G,
    ) -> impl Future<Output = Result<Result<T, E>, DurableError>> + Send
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        E: Serialize + DeserializeOwned + Send + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, E>> + Send + 'static,
        G: FnOnce(T) -> GFut + Send + 'static,
        GFut: Future<Output = Result<(), DurableError>> + Send + 'static,
    {
        DurableContext::step_with_compensation_opts(self, name, options, forward_fn, compensate_fn)
    }

    fn run_compensations(
        &mut self,
    ) -> impl Future<Output = Result<CompensationResult, DurableError>> + Send {
        DurableContext::run_compensations(self)
    }

    fn callback_result<T: DeserializeOwned>(
        &self,
        handle: &CallbackHandle,
    ) -> Result<T, DurableError> {
        DurableContext::callback_result(self, handle)
    }

    fn execution_mode(&self) -> ExecutionMode {
        DurableContext::execution_mode(self)
    }

    fn is_replaying(&self) -> bool {
        DurableContext::is_replaying(self)
    }

    fn arn(&self) -> &str {
        DurableContext::arn(self)
    }

    fn checkpoint_token(&self) -> &str {
        DurableContext::checkpoint_token(self)
    }

    fn log(&self, message: &str) {
        DurableContext::log(self, message);
    }

    fn log_with_data(&self, message: &str, data: &serde_json::Value) {
        DurableContext::log_with_data(self, message, data);
    }

    fn log_debug(&self, message: &str) {
        DurableContext::log_debug(self, message);
    }

    fn log_warn(&self, message: &str) {
        DurableContext::log_warn(self, message);
    }

    fn log_error(&self, message: &str) {
        DurableContext::log_error(self, message);
    }

    fn log_debug_with_data(&self, message: &str, data: &serde_json::Value) {
        DurableContext::log_debug_with_data(self, message, data);
    }

    fn log_warn_with_data(&self, message: &str, data: &serde_json::Value) {
        DurableContext::log_warn_with_data(self, message, data);
    }

    fn log_error_with_data(&self, message: &str, data: &serde_json::Value) {
        DurableContext::log_error_with_data(self, message, data);
    }

    fn enable_batch_mode(&mut self) {
        DurableContext::enable_batch_mode(self);
    }

    fn flush_batch(&mut self) -> impl Future<Output = Result<(), DurableError>> + Send {
        DurableContext::flush_batch(self)
    }
}