Skip to main content

aws_durable_execution_sdk/
operation.rs

1//! The kind and status of a durable operation, as a test or a plugin sees them.
2
3use std::fmt;
4
5/// The kind of a durable operation.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[non_exhaustive]
8pub enum OperationKind {
9    /// A step, created by `DurableContext::step`.
10    Step,
11    /// A timer, created by `DurableContext::wait`.
12    Wait,
13    /// A child context, such as a `map` item, a `parallel` branch, a `with_retry` block with
14    /// its own record or a `with_retry` attempt.
15    Context,
16    /// A callback that an external system completes.
17    Callback,
18    /// A call to another durable function, created by `DurableContext::invoke`.
19    ChainedInvoke,
20    /// The execution itself.
21    Execution,
22}
23
24impl OperationKind {
25    /// Returns the token the service history and JS plugins use for this kind,
26    /// such as `STEP` or `CHAINED_INVOKE`.
27    #[must_use]
28    pub fn wire_str(self) -> &'static str {
29        match self {
30            Self::Step => "STEP",
31            Self::Wait => "WAIT",
32            Self::Context => "CONTEXT",
33            Self::Callback => "CALLBACK",
34            Self::ChainedInvoke => "CHAINED_INVOKE",
35            Self::Execution => "EXECUTION",
36        }
37    }
38
39    /// Converts a service model operation type, or returns `None` for a type
40    /// this enum does not list.
41    pub(crate) fn from_wire(t: &aws_sdk_lambda::types::OperationType) -> Option<Self> {
42        use aws_sdk_lambda::types::OperationType;
43        match t {
44            OperationType::Step => Some(Self::Step),
45            OperationType::Wait => Some(Self::Wait),
46            OperationType::Context => Some(Self::Context),
47            OperationType::Callback => Some(Self::Callback),
48            OperationType::ChainedInvoke => Some(Self::ChainedInvoke),
49            OperationType::Execution => Some(Self::Execution),
50            _ => None,
51        }
52    }
53}
54
55impl fmt::Display for OperationKind {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        f.write_str(match self {
58            Self::Step => "Step",
59            Self::Wait => "Wait",
60            Self::Context => "Context",
61            Self::Callback => "Callback",
62            Self::ChainedInvoke => "ChainedInvoke",
63            Self::Execution => "Execution",
64        })
65    }
66}
67
68/// The status of a durable operation.
69///
70/// This is not `lambda_types::OperationStatus`, the service model type of the
71/// same name that the `ExecutionClient` transport trait uses.
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
73#[non_exhaustive]
74pub enum OperationStatus {
75    /// The operation started and has no outcome yet.
76    Started,
77    /// The operation waits for a retry delay to elapse.
78    Pending,
79    /// The retry delay elapsed and the operation can run its next attempt.
80    Ready,
81    /// The operation completed successfully.
82    Succeeded,
83    /// The operation completed with a failure.
84    Failed,
85    /// The operation was cancelled.
86    Cancelled,
87    /// The operation reached its timeout.
88    TimedOut,
89    /// The operation was stopped.
90    Stopped,
91}
92
93impl OperationStatus {
94    /// Returns the token the service history and JS plugins use for this
95    /// status, such as `SUCCEEDED` or `TIMED_OUT`.
96    #[must_use]
97    pub fn wire_str(self) -> &'static str {
98        match self {
99            Self::Started => "STARTED",
100            Self::Pending => "PENDING",
101            Self::Ready => "READY",
102            Self::Succeeded => "SUCCEEDED",
103            Self::Failed => "FAILED",
104            Self::Cancelled => "CANCELLED",
105            Self::TimedOut => "TIMED_OUT",
106            Self::Stopped => "STOPPED",
107        }
108    }
109}
110
111impl fmt::Display for OperationStatus {
112    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113        f.write_str(match self {
114            Self::Started => "Started",
115            Self::Pending => "Pending",
116            Self::Ready => "Ready",
117            Self::Succeeded => "Succeeded",
118            Self::Failed => "Failed",
119            Self::Cancelled => "Cancelled",
120            Self::TimedOut => "TimedOut",
121            Self::Stopped => "Stopped",
122        })
123    }
124}
125
126/// How one invocation of a durable function ended, as a plugin sees it.
127#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
128#[non_exhaustive]
129pub enum InvocationStatus {
130    /// The execution completed successfully in this invocation.
131    Succeeded,
132    /// The execution failed in this invocation.
133    Failed,
134    /// The invocation suspended, and the service invokes the function
135    /// again when an operation it waits for is due.
136    Pending,
137    /// The invocation failed, and the service invokes the function again.
138    Retrying,
139}
140
141impl InvocationStatus {
142    /// Returns the token JS plugins use for this status, such as
143    /// `SUCCEEDED` or `RETRYING`.
144    #[must_use]
145    pub fn wire_str(self) -> &'static str {
146        match self {
147            Self::Succeeded => "SUCCEEDED",
148            Self::Failed => "FAILED",
149            Self::Pending => "PENDING",
150            Self::Retrying => "RETRYING",
151        }
152    }
153}
154
155impl fmt::Display for InvocationStatus {
156    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157        f.write_str(match self {
158            Self::Succeeded => "Succeeded",
159            Self::Failed => "Failed",
160            Self::Pending => "Pending",
161            Self::Retrying => "Retrying",
162        })
163    }
164}
165
166#[cfg(test)]
167mod tests {
168    use super::{InvocationStatus, OperationKind, OperationStatus};
169
170    /// Each invocation status displays its variant name and returns the
171    /// token JS plugins use.
172    #[test]
173    fn invocation_status_displays_variant_names() {
174        let all = [
175            InvocationStatus::Succeeded,
176            InvocationStatus::Failed,
177            InvocationStatus::Pending,
178            InvocationStatus::Retrying,
179        ];
180        let shown: Vec<String> = all.iter().map(ToString::to_string).collect();
181        assert_eq!(shown, ["Succeeded", "Failed", "Pending", "Retrying"]);
182        let wire: Vec<&str> = all.iter().map(|s| s.wire_str()).collect();
183        assert_eq!(wire, ["SUCCEEDED", "FAILED", "PENDING", "RETRYING"]);
184    }
185
186    const KINDS: [OperationKind; 6] = [
187        OperationKind::Step,
188        OperationKind::Wait,
189        OperationKind::Context,
190        OperationKind::Callback,
191        OperationKind::ChainedInvoke,
192        OperationKind::Execution,
193    ];
194
195    const STATUSES: [OperationStatus; 8] = [
196        OperationStatus::Started,
197        OperationStatus::Pending,
198        OperationStatus::Ready,
199        OperationStatus::Succeeded,
200        OperationStatus::Failed,
201        OperationStatus::Cancelled,
202        OperationStatus::TimedOut,
203        OperationStatus::Stopped,
204    ];
205
206    #[test]
207    fn operation_enums_display_as_variant_names() {
208        let kinds: Vec<String> = KINDS.iter().map(ToString::to_string).collect();
209        assert_eq!(
210            kinds,
211            [
212                "Step",
213                "Wait",
214                "Context",
215                "Callback",
216                "ChainedInvoke",
217                "Execution"
218            ]
219        );
220        let statuses: Vec<String> = STATUSES.iter().map(ToString::to_string).collect();
221        assert_eq!(
222            statuses,
223            [
224                "Started",
225                "Pending",
226                "Ready",
227                "Succeeded",
228                "Failed",
229                "Cancelled",
230                "TimedOut",
231                "Stopped"
232            ]
233        );
234    }
235
236    #[test]
237    fn operation_enums_wire_spelling() {
238        let kinds: Vec<&str> = KINDS.iter().map(|k| k.wire_str()).collect();
239        assert_eq!(
240            kinds,
241            [
242                "STEP",
243                "WAIT",
244                "CONTEXT",
245                "CALLBACK",
246                "CHAINED_INVOKE",
247                "EXECUTION"
248            ]
249        );
250        let statuses: Vec<&str> = STATUSES.iter().map(|s| s.wire_str()).collect();
251        assert_eq!(
252            statuses,
253            [
254                "STARTED",
255                "PENDING",
256                "READY",
257                "SUCCEEDED",
258                "FAILED",
259                "CANCELLED",
260                "TIMED_OUT",
261                "STOPPED"
262            ]
263        );
264    }
265}