1#![allow(deprecated)]
31
32use std::sync::Arc;
33
34#[deprecated(since = "3.16.0", note = "nothing used it; declared workflows run through backbone_core::workflow (WorkflowSpec, WorkflowDefinition and WorkflowEngine)")]
37#[derive(Debug, Clone, thiserror::Error)]
41pub enum FlowError {
42 #[error("No current step to execute")]
43 NoCurrentStep,
44
45 #[error("Step execution failed: {0}")]
46 StepFailed(String),
47
48 #[error("Condition evaluation failed: {0}")]
49 ConditionFailed(String),
50
51 #[error("Compensation failed: {0}")]
52 CompensationFailed(String),
53
54 #[error("Flow timed out")]
55 Timeout,
56
57 #[error("Flow cancelled")]
58 Cancelled,
59
60 #[error("Invalid state transition: {from} -> {to}")]
61 InvalidTransition { from: String, to: String },
62}
63
64#[deprecated(since = "3.16.0", note = "nothing used it; declared workflows run through backbone_core::workflow (WorkflowSpec, WorkflowDefinition and WorkflowEngine)")]
67#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
71#[serde(rename_all = "snake_case")]
72pub enum FlowStatus {
73 Pending,
75 Running,
77 Waiting,
79 Completed,
81 Failed,
83 Cancelled,
85 Compensating,
87}
88
89#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
96#[serde(bound(
97 serialize = "S: serde::Serialize",
98 deserialize = "S: serde::de::DeserializeOwned",
99))]
100#[deprecated(since = "3.16.0", note = "nothing used it; declared workflows run through backbone_core::workflow (WorkflowSpec, WorkflowDefinition and WorkflowEngine)")]
101pub struct FlowInstance<S>
102where
103 S: std::fmt::Debug + Clone + serde::Serialize + serde::de::DeserializeOwned,
104{
105 pub id: String,
106 pub status: FlowStatus,
107 pub current_step: Option<S>,
108 pub context: serde_json::Value,
109 pub completed_steps: Vec<S>,
110 pub error: Option<String>,
111 pub created_at: chrono::DateTime<chrono::Utc>,
112 pub updated_at: chrono::DateTime<chrono::Utc>,
113}
114
115impl<S> FlowInstance<S>
116where
117 S: std::fmt::Debug + Clone + serde::Serialize + serde::de::DeserializeOwned,
118{
119 pub fn new(id: impl Into<String>) -> Self {
120 let now = chrono::Utc::now();
121 Self {
122 id: id.into(),
123 status: FlowStatus::Pending,
124 current_step: None,
125 context: serde_json::json!({}),
126 completed_steps: Vec::new(),
127 error: None,
128 created_at: now,
129 updated_at: now,
130 }
131 }
132
133 pub fn is_complete(&self) -> bool {
134 matches!(
135 self.status,
136 FlowStatus::Completed | FlowStatus::Failed | FlowStatus::Cancelled
137 )
138 }
139
140 pub fn is_running(&self) -> bool {
141 matches!(self.status, FlowStatus::Running | FlowStatus::Waiting)
142 }
143
144 pub fn set_context(&mut self, key: &str, value: serde_json::Value) {
145 if let serde_json::Value::Object(ref mut map) = self.context {
146 map.insert(key.to_string(), value);
147 }
148 self.updated_at = chrono::Utc::now();
149 }
150
151 pub fn get_context(&self, key: &str) -> Option<&serde_json::Value> {
152 if let serde_json::Value::Object(ref map) = self.context {
153 map.get(key)
154 } else {
155 None
156 }
157 }
158}
159
160impl<S> WorkflowContext<FlowInstance<S>> for FlowInstance<S>
163where
164 S: std::fmt::Debug + Clone + serde::Serialize + serde::de::DeserializeOwned + Send + Sync,
165{
166 fn entity(&self) -> &FlowInstance<S> { self }
167 fn set_var(&mut self, key: &str, value: serde_json::Value) { self.set_context(key, value); }
168 fn get_var(&self, key: &str) -> Option<&serde_json::Value> { self.get_context(key) }
169}
170
171#[deprecated(since = "3.16.0", note = "nothing used it; declared workflows run through backbone_core::workflow (WorkflowSpec, WorkflowDefinition and WorkflowEngine)")]
174pub struct FlowExecutor<H> {
208 pub handler: Arc<H>,
209}
210
211impl<H: Send + Sync + 'static> FlowExecutor<H> {
212 pub fn new(handler: Arc<H>) -> Self {
213 Self { handler }
214 }
215
216 pub fn start<S>(&self, id: impl Into<String>, first_step: S) -> FlowInstance<S>
218 where
219 S: std::fmt::Debug + Clone + serde::Serialize + serde::de::DeserializeOwned,
220 {
221 let mut instance = FlowInstance::new(id);
222 instance.status = FlowStatus::Running;
223 instance.current_step = Some(first_step);
224 instance
225 }
226
227 pub fn cancel<S>(&self, instance: &mut FlowInstance<S>)
229 where
230 S: std::fmt::Debug + Clone + serde::Serialize + serde::de::DeserializeOwned,
231 {
232 instance.status = FlowStatus::Cancelled;
233 instance.updated_at = chrono::Utc::now();
234 }
235
236 pub fn fail<S>(&self, instance: &mut FlowInstance<S>, error: impl Into<String>)
238 where
239 S: std::fmt::Debug + Clone + serde::Serialize + serde::de::DeserializeOwned,
240 {
241 instance.status = FlowStatus::Failed;
242 instance.error = Some(error.into());
243 instance.updated_at = chrono::Utc::now();
244 }
245}
246
247#[deprecated(since = "3.16.0", note = "nothing used it; declared workflows run through backbone_core::workflow (WorkflowSpec, WorkflowDefinition and WorkflowEngine)")]
250pub trait WorkflowContext<E>: Send + Sync {
252 fn entity(&self) -> &E;
253 fn set_var(&mut self, key: &str, value: serde_json::Value);
254 fn get_var(&self, key: &str) -> Option<&serde_json::Value>;
255}
256
257#[deprecated(since = "3.16.0", note = "nothing used it; declared workflows run through backbone_core::workflow (WorkflowSpec, WorkflowDefinition and WorkflowEngine)")]
258pub trait WorkflowStep: Send + Sync {
260 fn name(&self) -> &'static str;
261}