orcher-sdk 0.5.2

Rust SDK for Orcher, a durable workflow engine: workflows, tasks, actors, workers and a client
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! Point-in-time copies of workflow state, and diffs between them.

use crate::error::{Error, Result};
use crate::workflow::WorkflowContext;
use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashMap;
use std::time::SystemTime;

/// A copy of workflow state at a point in time.
///
/// Values are stored as JSON bytes, so two snapshots compare equal on a key
/// when the serialized values are byte-identical.
#[derive(Debug, Clone)]
pub struct StateSnapshot {
    /// ID of the workflow the snapshot belongs to.
    pub workflow_id: String,

    /// Wall-clock time at which the snapshot was taken.
    pub timestamp: SystemTime,

    /// State values keyed by name, each serialized as JSON.
    state: HashMap<String, Vec<u8>>,

    label: Option<String>,
}

impl StateSnapshot {
    /// Capture a snapshot from a workflow context.
    ///
    /// The snapshot records the workflow ID and the capture time. It does not
    /// read the context's state, so it starts empty; add values with
    /// [`set`](Self::set).
    ///
    /// # Example
    ///
    /// ```rust
    /// # use orcher_sdk::debugging::StateSnapshot;
    /// # use orcher_sdk::workflow::WorkflowContext;
    /// # fn example(ctx: &WorkflowContext) {
    /// let snapshot = StateSnapshot::capture(ctx);
    /// println!("Captured {} state keys", snapshot.keys().len());
    /// # }
    /// ```
    pub fn capture(ctx: &WorkflowContext) -> Self {
        Self {
            workflow_id: ctx.workflow_id().to_string(),
            timestamp: SystemTime::now(),
            state: HashMap::new(),
            label: None,
        }
    }

    /// Capture a snapshot from a workflow context and label it.
    ///
    /// Behaves like [`capture`](Self::capture).
    ///
    /// # Example
    ///
    /// ```rust
    /// # use orcher_sdk::debugging::StateSnapshot;
    /// # use orcher_sdk::workflow::WorkflowContext;
    /// # fn example(ctx: &WorkflowContext) {
    /// let snapshot = StateSnapshot::capture_with_label(ctx, "after_payment");
    /// # }
    /// ```
    pub fn capture_with_label(ctx: &WorkflowContext, label: impl Into<String>) -> Self {
        let mut snapshot = Self::capture(ctx);
        snapshot.label = Some(label.into());
        snapshot
    }

    /// Create an empty snapshot for the given workflow ID.
    pub fn new(workflow_id: impl Into<String>) -> Self {
        Self {
            workflow_id: workflow_id.into(),
            timestamp: SystemTime::now(),
            state: HashMap::new(),
            label: None,
        }
    }

    /// Store a value under `key`, replacing any existing value.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Serialization`] if the value cannot be serialized to JSON.
    pub fn set<T: Serialize>(&mut self, key: impl Into<String>, value: &T) -> Result<()> {
        let key = key.into();
        let value_bytes = serde_json::to_vec(value)
            .map_err(|e| Error::Serialization(format!("Failed to serialize value: {}", e)))?;
        self.state.insert(key, value_bytes);
        Ok(())
    }

    /// Read the value stored under `key`, or `None` if the key is absent.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Serialization`] if the stored value does not deserialize as `T`.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use orcher_sdk::debugging::StateSnapshot;
    /// # fn example() -> orcher_sdk::Result<()> {
    /// # let snapshot = StateSnapshot::new("wf-123");
    /// let counter: Option<i32> = snapshot.get("counter")?;
    /// if let Some(value) = counter {
    ///     println!("Counter: {}", value);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>> {
        match self.state.get(key) {
            Some(value_bytes) => {
                let value: T = serde_json::from_slice(value_bytes).map_err(|e| {
                    Error::Serialization(format!("Failed to deserialize value: {}", e))
                })?;
                Ok(Some(value))
            }
            None => Ok(None),
        }
    }

    /// Return whether the snapshot holds a value for `key`.
    pub fn contains(&self, key: &str) -> bool {
        self.state.contains_key(key)
    }

    /// Return all keys, in no particular order.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use orcher_sdk::debugging::StateSnapshot;
    /// # fn example(snapshot: &StateSnapshot) {
    /// let keys = snapshot.keys();
    /// println!("State keys: {:?}", keys);
    /// # }
    /// ```
    pub fn keys(&self) -> Vec<String> {
        self.state.keys().cloned().collect()
    }

    /// Return the number of keys.
    pub fn len(&self) -> usize {
        self.state.len()
    }

    /// Return whether the snapshot holds no values.
    pub fn is_empty(&self) -> bool {
        self.state.is_empty()
    }

    /// Return the snapshot's label, if it has one.
    pub fn label(&self) -> Option<&str> {
        self.label.as_deref()
    }

    /// Compare this snapshot with a later one.
    ///
    /// `self` is the "from" side: keys only in `other` are added, keys only in
    /// `self` are removed, and keys in both with different values are changed.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use orcher_sdk::debugging::StateSnapshot;
    /// # fn example() {
    /// # let snapshot1 = StateSnapshot::new("wf-123");
    /// # let snapshot2 = StateSnapshot::new("wf-123");
    /// let diff = snapshot1.diff(&snapshot2);
    /// println!("Changed keys: {:?}", diff.changed_keys());
    /// println!("Added keys: {:?}", diff.added_keys());
    /// println!("Removed keys: {:?}", diff.removed_keys());
    /// # }
    /// ```
    pub fn diff(&self, other: &StateSnapshot) -> StateDiff {
        let mut added = Vec::new();
        let mut removed = Vec::new();
        let mut changed = Vec::new();

        for key in other.keys() {
            if !self.contains(&key) {
                added.push(key.clone());
            } else if self.state.get(&key) != other.state.get(&key) {
                changed.push(key.clone());
            }
        }

        for key in self.keys() {
            if !other.contains(&key) {
                removed.push(key.clone());
            }
        }

        StateDiff {
            from_label: self.label.clone(),
            to_label: other.label.clone(),
            added_keys: added,
            removed_keys: removed,
            changed_keys: changed,
        }
    }

    /// Set the label, replacing any existing one (builder-style).
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Return the underlying map of keys to JSON-encoded values.
    pub fn raw_state(&self) -> &HashMap<String, Vec<u8>> {
        &self.state
    }
}

/// Differences between two state snapshots, produced by [`StateSnapshot::diff`].
#[derive(Debug, Clone)]
pub struct StateDiff {
    /// Label of the earlier snapshot.
    pub from_label: Option<String>,

    /// Label of the later snapshot.
    pub to_label: Option<String>,

    /// Keys present only in the later snapshot.
    pub added_keys: Vec<String>,

    /// Keys present only in the earlier snapshot.
    pub removed_keys: Vec<String>,

    /// Keys present in both snapshots with different values.
    pub changed_keys: Vec<String>,
}

impl StateDiff {
    /// Return whether any key was added, removed or changed.
    pub fn has_changes(&self) -> bool {
        !self.added_keys.is_empty()
            || !self.removed_keys.is_empty()
            || !self.changed_keys.is_empty()
    }

    /// Return added, removed and changed keys, in that order.
    pub fn all_changed_keys(&self) -> Vec<String> {
        let mut keys = Vec::new();
        keys.extend(self.added_keys.iter().cloned());
        keys.extend(self.removed_keys.iter().cloned());
        keys.extend(self.changed_keys.iter().cloned());
        keys
    }

    /// Return the total number of added, removed and changed keys.
    pub fn change_count(&self) -> usize {
        self.added_keys.len() + self.removed_keys.len() + self.changed_keys.len()
    }

    /// Return a short summary such as `"1 added, 2 changed"`, or `"No changes"`.
    pub fn summary(&self) -> String {
        let mut parts = Vec::new();

        if !self.added_keys.is_empty() {
            parts.push(format!("{} added", self.added_keys.len()));
        }
        if !self.removed_keys.is_empty() {
            parts.push(format!("{} removed", self.removed_keys.len()));
        }
        if !self.changed_keys.is_empty() {
            parts.push(format!("{} changed", self.changed_keys.len()));
        }

        if parts.is_empty() {
            "No changes".to_string()
        } else {
            parts.join(", ")
        }
    }

    /// Return the keys present only in the later snapshot.
    pub fn added_keys(&self) -> &[String] {
        &self.added_keys
    }

    /// Return the keys present only in the earlier snapshot.
    pub fn removed_keys(&self) -> &[String] {
        &self.removed_keys
    }

    /// Return the keys whose values differ between the snapshots.
    pub fn changed_keys(&self) -> &[String] {
        &self.changed_keys
    }
}

/// An ordered series of snapshots of one workflow's state.
#[derive(Debug, Clone)]
#[allow(dead_code)] // Not re-exported from `debugging`; exercised by the tests below.
pub struct StateHistory {
    workflow_id: String,

    /// Snapshots in the order they were added.
    snapshots: Vec<StateSnapshot>,
}

#[allow(dead_code)] // See the note on the struct.
impl StateHistory {
    /// Create an empty history for the given workflow ID.
    pub fn new(workflow_id: impl Into<String>) -> Self {
        Self {
            workflow_id: workflow_id.into(),
            snapshots: Vec::new(),
        }
    }

    /// Append a snapshot.
    pub fn add(&mut self, snapshot: StateSnapshot) {
        self.snapshots.push(snapshot);
    }

    /// Capture a snapshot from a context, optionally labeled, and append it.
    pub fn capture(&mut self, ctx: &WorkflowContext, label: Option<String>) {
        let snapshot = if let Some(label) = label {
            StateSnapshot::capture_with_label(ctx, label)
        } else {
            StateSnapshot::capture(ctx)
        };
        self.add(snapshot);
    }

    /// Return all snapshots in the order they were added.
    pub fn snapshots(&self) -> &[StateSnapshot] {
        &self.snapshots
    }

    /// Return the most recently added snapshot.
    pub fn latest(&self) -> Option<&StateSnapshot> {
        self.snapshots.last()
    }

    /// Return the first snapshot added.
    pub fn first(&self) -> Option<&StateSnapshot> {
        self.snapshots.first()
    }

    /// Return the number of snapshots.
    pub fn len(&self) -> usize {
        self.snapshots.len()
    }

    /// Return whether the history holds no snapshots.
    pub fn is_empty(&self) -> bool {
        self.snapshots.is_empty()
    }

    /// Return the first snapshot with the given label.
    pub fn get_by_label(&self, label: &str) -> Option<&StateSnapshot> {
        self.snapshots
            .iter()
            .find(|s| s.label.as_deref() == Some(label))
    }

    /// Diff the snapshots at two indexes, or return `None` if either is out of range.
    pub fn diff_between(&self, from_idx: usize, to_idx: usize) -> Option<StateDiff> {
        if from_idx >= self.snapshots.len() || to_idx >= self.snapshots.len() {
            return None;
        }
        Some(self.snapshots[from_idx].diff(&self.snapshots[to_idx]))
    }

    /// Diff the first snapshot against the latest, or return `None` if there are
    /// fewer than two.
    pub fn overall_diff(&self) -> Option<StateDiff> {
        if self.snapshots.len() < 2 {
            return None;
        }
        let first_idx = 0;
        let last_idx = self.snapshots.len() - 1;
        self.diff_between(first_idx, last_idx)
    }

    /// Remove all snapshots.
    pub fn clear(&mut self) {
        self.snapshots.clear();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_state_snapshot_creation() {
        let snapshot = StateSnapshot::new("wf-123");
        assert_eq!(snapshot.workflow_id, "wf-123");
        assert!(snapshot.is_empty());
        assert!(snapshot.label.is_none());
    }

    #[test]
    fn test_state_snapshot_set_get() {
        let mut snapshot = StateSnapshot::new("wf-123");
        snapshot.set("counter", &42i32).unwrap();
        snapshot.set("name", &"test".to_string()).unwrap();

        let counter: Option<i32> = snapshot.get("counter").unwrap();
        assert_eq!(counter, Some(42));

        let name: Option<String> = snapshot.get("name").unwrap();
        assert_eq!(name, Some("test".to_string()));

        let missing: Option<String> = snapshot.get("missing").unwrap();
        assert_eq!(missing, None);
    }

    #[test]
    fn test_state_snapshot_keys() {
        let mut snapshot = StateSnapshot::new("wf-123");
        snapshot.set("key1", &1).unwrap();
        snapshot.set("key2", &2).unwrap();

        let keys = snapshot.keys();
        assert_eq!(keys.len(), 2);
        assert!(keys.contains(&"key1".to_string()));
        assert!(keys.contains(&"key2".to_string()));
    }

    #[test]
    fn test_state_snapshot_contains() {
        let mut snapshot = StateSnapshot::new("wf-123");
        snapshot.set("exists", &true).unwrap();

        assert!(snapshot.contains("exists"));
        assert!(!snapshot.contains("missing"));
    }

    #[test]
    fn test_state_snapshot_with_label() {
        let snapshot = StateSnapshot::new("wf-123").with_label("after_payment");
        assert_eq!(snapshot.label(), Some("after_payment"));
    }

    #[test]
    fn test_state_diff_no_changes() {
        let mut s1 = StateSnapshot::new("wf-123");
        s1.set("key", &42).unwrap();

        let mut s2 = StateSnapshot::new("wf-123");
        s2.set("key", &42).unwrap();

        let diff = s1.diff(&s2);
        assert!(!diff.has_changes());
        assert_eq!(diff.change_count(), 0);
    }

    #[test]
    fn test_state_diff_added_keys() {
        let s1 = StateSnapshot::new("wf-123");

        let mut s2 = StateSnapshot::new("wf-123");
        s2.set("new_key", &1).unwrap();

        let diff = s1.diff(&s2);
        assert!(diff.has_changes());
        assert_eq!(diff.added_keys.len(), 1);
        assert!(diff.added_keys.contains(&"new_key".to_string()));
    }

    #[test]
    fn test_state_diff_removed_keys() {
        let mut s1 = StateSnapshot::new("wf-123");
        s1.set("old_key", &1).unwrap();

        let s2 = StateSnapshot::new("wf-123");

        let diff = s1.diff(&s2);
        assert!(diff.has_changes());
        assert_eq!(diff.removed_keys.len(), 1);
        assert!(diff.removed_keys.contains(&"old_key".to_string()));
    }

    #[test]
    fn test_state_diff_changed_keys() {
        let mut s1 = StateSnapshot::new("wf-123");
        s1.set("key", &1).unwrap();

        let mut s2 = StateSnapshot::new("wf-123");
        s2.set("key", &2).unwrap();

        let diff = s1.diff(&s2);
        assert!(diff.has_changes());
        assert_eq!(diff.changed_keys.len(), 1);
        assert!(diff.changed_keys.contains(&"key".to_string()));
    }

    #[test]
    fn test_state_diff_summary() {
        let mut s1 = StateSnapshot::new("wf-123");
        s1.set("old", &1).unwrap();
        s1.set("changed", &1).unwrap();

        let mut s2 = StateSnapshot::new("wf-123");
        s2.set("new", &2).unwrap();
        s2.set("changed", &2).unwrap();

        let diff = s1.diff(&s2);
        let summary = diff.summary();
        assert!(summary.contains("added"));
        assert!(summary.contains("removed"));
        assert!(summary.contains("changed"));
    }

    #[test]
    fn test_state_history_creation() {
        let history = StateHistory::new("wf-123");
        assert_eq!(history.workflow_id, "wf-123");
        assert!(history.is_empty());
    }

    #[test]
    fn test_state_history_add() {
        let mut history = StateHistory::new("wf-123");

        let snapshot1 = StateSnapshot::new("wf-123").with_label("snapshot1");
        let snapshot2 = StateSnapshot::new("wf-123").with_label("snapshot2");

        history.add(snapshot1);
        history.add(snapshot2);

        assert_eq!(history.len(), 2);
        assert_eq!(history.first().unwrap().label(), Some("snapshot1"));
        assert_eq!(history.latest().unwrap().label(), Some("snapshot2"));
    }

    #[test]
    fn test_state_history_get_by_label() {
        let mut history = StateHistory::new("wf-123");

        let snapshot = StateSnapshot::new("wf-123").with_label("test_label");
        history.add(snapshot);

        let found = history.get_by_label("test_label");
        assert!(found.is_some());
        assert_eq!(found.unwrap().label(), Some("test_label"));

        let not_found = history.get_by_label("missing");
        assert!(not_found.is_none());
    }

    #[test]
    fn test_state_history_diff_between() {
        let mut history = StateHistory::new("wf-123");

        let mut s1 = StateSnapshot::new("wf-123");
        s1.set("key", &1).unwrap();
        history.add(s1);

        let mut s2 = StateSnapshot::new("wf-123");
        s2.set("key", &2).unwrap();
        history.add(s2);

        let diff = history.diff_between(0, 1).unwrap();
        assert!(diff.has_changes());
        assert_eq!(diff.changed_keys.len(), 1);
    }

    #[test]
    fn test_state_history_overall_diff() {
        let mut history = StateHistory::new("wf-123");

        let s1 = StateSnapshot::new("wf-123");
        history.add(s1);

        let mut s2 = StateSnapshot::new("wf-123");
        s2.set("new_key", &1).unwrap();
        history.add(s2);

        let diff = history.overall_diff().unwrap();
        assert!(diff.has_changes());
        assert_eq!(diff.added_keys.len(), 1);
    }

    #[test]
    fn test_state_history_clear() {
        let mut history = StateHistory::new("wf-123");
        history.add(StateSnapshot::new("wf-123"));
        history.add(StateSnapshot::new("wf-123"));

        assert_eq!(history.len(), 2);

        history.clear();

        assert_eq!(history.len(), 0);
        assert!(history.is_empty());
    }
}