1use 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)]
20pub enum SubFlowReusePolicy {
22 Attach,
24 RestartIfPreviousExitsAbnormally,
26 AlwaysRestart,
28}
29
30#[derive(Clone)]
31pub 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 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 pub fn timeout(mut self, value: Duration) -> Self {
63 self.timeout = Some(value);
64 self
65 }
66
67 pub fn timeout_policy(mut self, value: FlowTimeoutPolicy) -> Self {
69 self.timeout_policy = value;
70 self
71 }
72
73 pub fn start_delay(mut self, value: Duration) -> Self {
75 self.start_delay = Some(value);
76 self
77 }
78
79 pub fn retry_policy(mut self, value: RetryPolicy) -> Self {
81 self.retry_policy = Some(value);
82 self
83 }
84
85 pub fn config_override(mut self, value: FlowConfig) -> Self {
87 self.config_override = Some(value);
88 self
89 }
90
91 pub fn reuse_policy(mut self, value: SubFlowReusePolicy) -> Self {
93 self.reuse_policy = value;
94 self
95 }
96
97 pub fn condition_id(mut self, value: impl Into<String>) -> Self {
99 self.condition_id = Some(value.into());
100 self
101 }
102
103 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 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
130pub struct SubFlow;
132
133impl SubFlow {
134 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 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 pub fn condition_result(context: &Context) -> HandlerResult<FlowResult> {
167 Self::condition_result_at(context, 0)
168 }
169
170 pub fn condition_result_at(context: &Context, index: usize) -> HandlerResult<FlowResult> {
172 context.sub_flow_result(index)
173 }
174
175 pub fn flow_id(context: &Context) -> HandlerResult<String> {
177 Self::flow_id_at(context, 0)
178 }
179
180 pub fn flow_id_at(context: &Context, index: usize) -> HandlerResult<String> {
182 context.sub_flow_id(index)
183 }
184}