waveless_commons/http_executor/
execution_atom.rs1use crate::*;
5
6use super::*;
7
8#[serde_as]
10#[derive(Clone, Constructor, Serialize, Deserialize, Getters, MutGetters, Debug)]
11#[getset(get = "pub", get_mut = "pub")]
12pub struct ExecutionAtom {
13 #[serde(default, skip_serializing_if = "should_skip_option")]
16 id: Option<ExecutionAtomId>,
17
18 #[serde_as(as = "IfIsHumanReadable<_, JsonString>")] executor: Arc<dyn AnyHttpExecutor>,
21
22 #[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 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 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 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()), }
74 });
75
76 future as BoxFuture<_> }
78}