Skip to main content

waveless_commons/http_executor/
execution_atom.rs

1// Waveless
2// Copyright (C) 2026 Oscar Alvarez Gonzalez
3
4use crate::*;
5
6use super::*;
7
8/// Execution atom (node) for composing the tree-like structure.
9#[serde_as]
10#[derive(Clone, Constructor, Serialize, Deserialize, Getters, MutGetters, Debug)]
11#[getset(get = "pub", get_mut = "pub")]
12pub struct ExecutionAtom {
13    /// Unique identifier for each executor.
14    /// NOTE: if the id is not set the parent node won't be able to select this step if it has multiple children.
15    #[serde(default, skip_serializing_if = "should_skip_option")]
16    id: Option<ExecutionAtomId>,
17
18    /// Establishes the execute handler.
19    #[serde_as(as = "IfIsHumanReadable<_, JsonString>")] // Explore müsli to avoid this.
20    executor: Arc<dyn AnyHttpExecutor>,
21
22    /// All children of the current node.
23    #[serde(default, skip_serializing_if = "should_skip_cheapvec")]
24    children: CheapVec<Arc<ExecutionAtom>>,
25}
26
27impl<T: AnyHttpExecutor> From<Arc<T>> for ExecutionAtom {
28    fn from(value: Arc<T>) -> Self {
29        ExecutionAtom::new(None, value, CheapVec::new_const())
30    }
31}
32
33impl ExecutionAtom {
34    /// Given an execution atom execute it and execute all it's children recursively.
35    pub fn execute<'a>(
36        &'a self,
37        cx: PipelineCx,
38        db_conns: DbConns,
39    ) -> BoxFuture<'a, Result<PipelineCx, RequestError>> {
40        let future: Pin<_> = Box::pin(async move {
41            let ExecutionAtom {
42                id,
43                executor,
44                children,
45                ..
46            } = self;
47
48            // Run the current executor.
49            let (PipelineCx { request, response }, action) =
50                executor.execute(cx, db_conns.to_owned()).await?;
51
52            match action {
53                PipelineAction::Continue(child_id) if !children.is_empty() => {
54                    // Get the children with the matching id.
55                    let child = {
56                        match &child_id {
57                            Some(child_id) => children.iter().find(|child| {
58                                child.id().to_owned().unwrap_or_default() == child_id
59                            }),
60                            None => children.iter().next(),
61                        }
62                    }
63                    .wrap_err(format!(
64                        "Execution atom `{}` doesn't have a child with the id `{}`",
65                        id.to_owned().unwrap_or("no id".into()),
66                        child_id.to_owned().unwrap_or_default()
67                    ))?
68                    .to_owned();
69
70                    Ok(child.execute((request, response).into(), db_conns).await?)
71                }
72                _ => Ok((request, response).into()), // Discards any further execution and returns the current response.
73            }
74        });
75
76        future as BoxFuture<_> // `rust-analyzer` complains here.
77    }
78}