Skip to main content

dex_sdk/
sub_flow.rs

1// Copyright (c) 2026 Super Durable, Inc.
2//
3// Licensed under the Super Durable Source License 1.0.
4// You may not use this file except in compliance with the License.
5// See the LICENSE file in the repository root.
6//
7// SPDX-License-Identifier: LicenseRef-Super-Durable-1.0
8
9use std::any::TypeId;
10use std::time::Duration;
11
12use crate::start_flow_options::InitialAttribute;
13use crate::wait::SubFlowDefinition;
14use crate::{
15    Attribute, AttributeMap, Condition, Context, Flow, FlowConfig, FlowResult, FlowTimeoutPolicy,
16    HandlerResult, RetryPolicy, SdkResult, Value, value_mapper,
17};
18
19#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20/// Controls how a generated SubFlow Flow ID resolves an existing execution.
21pub enum SubFlowReusePolicy {
22    /// Attaches to a running execution or returns its existing terminal result.
23    Attach,
24    /// Restarts abnormal executions, attaches while running, and returns completed results.
25    RestartIfPreviousExitsAbnormally,
26    /// Replaces any different existing execution, including a running one.
27    AlwaysRestart,
28}
29
30#[derive(Clone)]
31/// Configures one durable SubFlow Condition.
32///
33/// The SubFlow inherits its parent's effective Flow configuration. Dex generates its Flow ID and
34/// request ID. Builder methods preserve normal Flow start defaults when omitted.
35pub struct SubFlowOptions {
36    pub(crate) timeout: Option<Duration>,
37    pub(crate) timeout_policy: FlowTimeoutPolicy,
38    pub(crate) start_delay: Option<Duration>,
39    pub(crate) retry_policy: Option<RetryPolicy>,
40    pub(crate) config_override: Option<FlowConfig>,
41    pub(crate) attributes: Vec<InitialAttribute>,
42    pub(crate) reuse_policy: SubFlowReusePolicy,
43    pub(crate) condition_id: Option<String>,
44}
45
46impl SubFlowOptions {
47    /// Creates options using the abnormal-restart reuse policy and normal start defaults.
48    pub fn new() -> Self {
49        Self {
50            timeout: None,
51            timeout_policy: FlowTimeoutPolicy::Default,
52            start_delay: None,
53            retry_policy: None,
54            config_override: None,
55            attributes: Vec::new(),
56            reuse_policy: SubFlowReusePolicy::RestartIfPreviousExitsAbnormally,
57            condition_id: None,
58        }
59    }
60
61    /// Sets the maximum total SubFlow execution duration.
62    pub fn timeout(mut self, value: Duration) -> Self {
63        self.timeout = Some(value);
64        self
65    }
66
67    /// Selects what Dex does when the positive soft SubFlow timeout expires.
68    pub fn timeout_policy(mut self, value: FlowTimeoutPolicy) -> Self {
69        self.timeout_policy = value;
70        self
71    }
72
73    /// Delays the SubFlow starting Step after server acceptance.
74    pub fn start_delay(mut self, value: Duration) -> Self {
75        self.start_delay = Some(value);
76        self
77    }
78
79    /// Sets whole-Flow retry behavior after abnormal completion.
80    pub fn retry_policy(mut self, value: RetryPolicy) -> Self {
81        self.retry_policy = Some(value);
82        self
83    }
84
85    /// Applies fields over the inherited parent Flow configuration.
86    pub fn config_override(mut self, value: FlowConfig) -> Self {
87        self.config_override = Some(value);
88        self
89    }
90
91    /// Selects existing-execution resolution behavior.
92    pub fn reuse_policy(mut self, value: SubFlowReusePolicy) -> Self {
93        self.reuse_policy = value;
94        self
95    }
96
97    /// Assigns the stable ID required by [`crate::Wait::any_combination_of`].
98    pub fn condition_id(mut self, value: impl Into<String>) -> Self {
99        self.condition_id = Some(value.into());
100        self
101    }
102
103    /// Adds one initial singleton Attribute owned by the target SubFlow.
104    pub fn initial_attribute<T: Value>(mut self, attribute: &Attribute<T>, value: T) -> Self {
105        self.attributes
106            .push(InitialAttribute::for_attribute(attribute, value));
107        self
108    }
109
110    /// Adds one initial Attribute-map value owned by the target SubFlow.
111    pub fn initial_attribute_map<T: Value>(
112        mut self,
113        attribute: &AttributeMap<T>,
114        instance: &str,
115        value: T,
116    ) -> Self {
117        self.attributes.push(InitialAttribute::for_attribute_map(
118            attribute, instance, value,
119        ));
120        self
121    }
122}
123
124impl Default for SubFlowOptions {
125    fn default() -> Self {
126        Self::new()
127    }
128}
129
130/// Creates durable SubFlow Conditions and reads their Execute results.
131pub struct SubFlow;
132
133impl SubFlow {
134    /// Creates a SubFlow Condition with default options.
135    ///
136    /// # Errors
137    ///
138    /// Returns a value-mapping error when `input` cannot be encoded.
139    pub fn run<SomeFlow: Flow>(
140        flow: &SomeFlow,
141        input: SomeFlow::StartInput,
142    ) -> SdkResult<Condition> {
143        Self::run_with_options(flow, input, SubFlowOptions::new())
144    }
145
146    /// Creates a SubFlow Condition with explicit options.
147    ///
148    /// # Errors
149    ///
150    /// Returns a value-mapping error when `input` cannot be encoded. Worker mapping later validates
151    /// the exact registered Rust Flow type, starting Step, Attributes, and option durations.
152    pub fn run_with_options<SomeFlow: Flow>(
153        flow: &SomeFlow,
154        input: SomeFlow::StartInput,
155        options: SubFlowOptions,
156    ) -> SdkResult<Condition> {
157        Ok(Condition::sub_flow(SubFlowDefinition {
158            flow_type: flow.flow_type(),
159            type_id: TypeId::of::<SomeFlow>(),
160            input: value_mapper::encode(&input)?,
161            options,
162        }))
163    }
164
165    /// Returns the first SubFlow result during Step `execute`.
166    pub fn condition_result(context: &Context) -> HandlerResult<FlowResult> {
167        Self::condition_result_at(context, 0)
168    }
169
170    /// Returns one stable-indexed SubFlow result during Step `execute`.
171    pub fn condition_result_at(context: &Context, index: usize) -> HandlerResult<FlowResult> {
172        context.sub_flow_result(index)
173    }
174
175    /// Returns the generated Flow ID for the first SubFlow Condition during Step `execute`.
176    pub fn flow_id(context: &Context) -> HandlerResult<String> {
177        Self::flow_id_at(context, 0)
178    }
179
180    /// Returns the generated Flow ID for one stable-indexed SubFlow Condition during `execute`.
181    pub fn flow_id_at(context: &Context, index: usize) -> HandlerResult<String> {
182        context.sub_flow_id(index)
183    }
184}