Skip to main content

durable_execution_sdk/state/
replay_status.rs

1//! Replay status tracking for durable executions.
2//!
3//! This module provides the [`ReplayStatus`] enum for tracking whether
4//! the execution is replaying previously checkpointed operations or
5//! executing new operations.
6
7/// Replay status indicating whether we're replaying or executing new operations.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[repr(u8)]
10pub enum ReplayStatus {
11    /// Currently replaying previously checkpointed operations
12    Replay = 0,
13    /// Executing new operations (past the replay point)
14    New = 1,
15}
16
17impl ReplayStatus {
18    /// Returns true if currently in replay mode.
19    pub fn is_replay(&self) -> bool {
20        matches!(self, Self::Replay)
21    }
22
23    /// Returns true if executing new operations.
24    pub fn is_new(&self) -> bool {
25        matches!(self, Self::New)
26    }
27}
28
29impl From<u8> for ReplayStatus {
30    fn from(value: u8) -> Self {
31        match value {
32            0 => Self::Replay,
33            _ => Self::New,
34        }
35    }
36}
37
38impl From<ReplayStatus> for u8 {
39    fn from(status: ReplayStatus) -> Self {
40        status as u8
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_replay_status_replay() {
50        let status = ReplayStatus::Replay;
51        assert!(status.is_replay());
52        assert!(!status.is_new());
53    }
54
55    #[test]
56    fn test_replay_status_new() {
57        let status = ReplayStatus::New;
58        assert!(!status.is_replay());
59        assert!(status.is_new());
60    }
61
62    #[test]
63    fn test_replay_status_from_u8() {
64        assert_eq!(ReplayStatus::from(0), ReplayStatus::Replay);
65        assert_eq!(ReplayStatus::from(1), ReplayStatus::New);
66        assert_eq!(ReplayStatus::from(2), ReplayStatus::New);
67    }
68
69    #[test]
70    fn test_replay_status_to_u8() {
71        assert_eq!(u8::from(ReplayStatus::Replay), 0);
72        assert_eq!(u8::from(ReplayStatus::New), 1);
73    }
74}