1use std::collections::HashMap;
2use std::sync::Arc;
3
4use chrono::{DateTime, Utc};
5use parking_lot::RwLock;
6use serde_json::Value;
7
8pub type SharedStepFunctionsState = Arc<RwLock<StepFunctionsState>>;
9
10pub struct StepFunctionsState {
11 pub account_id: String,
12 pub region: String,
13 pub state_machines: HashMap<String, StateMachine>,
15 pub executions: HashMap<String, Execution>,
17}
18
19#[derive(Debug, Clone)]
20pub struct StateMachine {
21 pub name: String,
22 pub arn: String,
23 pub definition: String,
24 pub role_arn: String,
25 pub machine_type: StateMachineType,
26 pub status: StateMachineStatus,
27 pub creation_date: DateTime<Utc>,
28 pub update_date: DateTime<Utc>,
29 pub tags: HashMap<String, String>,
30 pub revision_id: String,
31 pub logging_configuration: Option<Value>,
32 pub tracing_configuration: Option<Value>,
33 pub description: String,
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub enum StateMachineType {
38 Standard,
39 Express,
40}
41
42impl StateMachineType {
43 pub fn as_str(&self) -> &'static str {
44 match self {
45 Self::Standard => "STANDARD",
46 Self::Express => "EXPRESS",
47 }
48 }
49
50 pub fn parse(s: &str) -> Option<Self> {
51 match s {
52 "STANDARD" => Some(Self::Standard),
53 "EXPRESS" => Some(Self::Express),
54 _ => None,
55 }
56 }
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum StateMachineStatus {
61 Active,
62 Deleting,
63}
64
65impl StateMachineStatus {
66 pub fn as_str(&self) -> &'static str {
67 match self {
68 Self::Active => "ACTIVE",
69 Self::Deleting => "DELETING",
70 }
71 }
72}
73
74#[derive(Debug, Clone)]
75pub struct Execution {
76 pub execution_arn: String,
77 pub state_machine_arn: String,
78 pub state_machine_name: String,
79 pub name: String,
80 pub status: ExecutionStatus,
81 pub input: Option<String>,
82 pub output: Option<String>,
83 pub start_date: DateTime<Utc>,
84 pub stop_date: Option<DateTime<Utc>>,
85 pub error: Option<String>,
86 pub cause: Option<String>,
87 pub history_events: Vec<HistoryEvent>,
88}
89
90#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91pub enum ExecutionStatus {
92 Running,
93 Succeeded,
94 Failed,
95 TimedOut,
96 Aborted,
97 PendingRedrive,
98}
99
100impl ExecutionStatus {
101 pub fn as_str(&self) -> &'static str {
102 match self {
103 Self::Running => "RUNNING",
104 Self::Succeeded => "SUCCEEDED",
105 Self::Failed => "FAILED",
106 Self::TimedOut => "TIMED_OUT",
107 Self::Aborted => "ABORTED",
108 Self::PendingRedrive => "PENDING_REDRIVE",
109 }
110 }
111}
112
113#[derive(Debug, Clone)]
114pub struct HistoryEvent {
115 pub id: i64,
116 pub event_type: String,
117 pub timestamp: DateTime<Utc>,
118 pub previous_event_id: i64,
119 pub details: Value,
120}
121
122impl StepFunctionsState {
123 pub fn new(account_id: &str, region: &str) -> Self {
124 Self {
125 account_id: account_id.to_string(),
126 region: region.to_string(),
127 state_machines: HashMap::new(),
128 executions: HashMap::new(),
129 }
130 }
131
132 pub fn reset(&mut self) {
133 self.state_machines.clear();
134 self.executions.clear();
135 }
136
137 pub fn state_machine_arn(&self, name: &str) -> String {
138 format!(
139 "arn:aws:states:{}:{}:stateMachine:{}",
140 self.region, self.account_id, name
141 )
142 }
143
144 pub fn execution_arn(&self, state_machine_name: &str, execution_name: &str) -> String {
145 format!(
146 "arn:aws:states:{}:{}:execution:{}:{}",
147 self.region, self.account_id, state_machine_name, execution_name
148 )
149 }
150}