flowbuilder_core/
flow.rs

1use crate::{FlowExecutor, Step};
2use anyhow::Result;
3use flowbuilder_context::FlowContext;
4use std::sync::Arc;
5use tokio::sync::Mutex;
6
7/// Represents a flow that can be executed
8pub struct Flow {
9    steps: Vec<Step>,
10}
11
12impl Flow {
13    pub(crate) fn new(steps: Vec<Step>) -> Self {
14        Self { steps }
15    }
16
17    /// Execute the flow with a default context
18    pub async fn execute(self) -> Result<FlowContext> {
19        let context = FlowContext::default();
20        self.execute_with_context(context).await
21    }
22
23    /// Execute the flow with a custom context
24    pub async fn execute_with_context(
25        self,
26        context: FlowContext,
27    ) -> Result<FlowContext> {
28        let shared_context = Arc::new(Mutex::new(context));
29        let executor = FlowExecutor::new();
30
31        executor
32            .execute_steps(self.steps, shared_context.clone())
33            .await?;
34
35        let final_context = Arc::try_unwrap(shared_context)
36            .map_err(|_| anyhow::anyhow!("Failed to unwrap shared context"))?
37            .into_inner();
38
39        Ok(final_context)
40    }
41}