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>(
113 mut self,
114 attribute: &AttributeMap<T>,
115 instance: &str,
116 value: T,
117 ) -> Self {
118 self.attributes.push(InitialAttribute::for_attribute_map(
119 attribute, instance, value,
120 ));
121 self
122 }
123}
124
125impl Default for SubFlowOptions {
126 fn default() -> Self {
127 Self::new()
128 }
129}
130
131pub struct SubFlow;
133
134impl SubFlow {
135 pub fn run<SomeFlow: Flow>(
141 flow: &SomeFlow,
142 input: SomeFlow::StartInput,
143 ) -> SdkResult<Condition> {
144 Self::run_with_options(flow, input, SubFlowOptions::new())
145 }
146
147 pub fn run_with_options<SomeFlow: Flow>(
154 flow: &SomeFlow,
155 input: SomeFlow::StartInput,
156 options: SubFlowOptions,
157 ) -> SdkResult<Condition> {
158 Ok(Condition::sub_flow(SubFlowDefinition {
159 flow_type: flow.flow_type(),
160 type_id: TypeId::of::<SomeFlow>(),
161 input: value_mapper::encode(&input)?,
162 options,
163 }))
164 }
165
166 pub fn condition_result(context: &Context) -> HandlerResult<FlowResult> {
168 Self::condition_result_at(context, 0)
169 }
170
171 pub fn condition_result_at(context: &Context, index: usize) -> HandlerResult<FlowResult> {
173 context.sub_flow_result(index)
174 }
175
176 pub fn flow_id(context: &Context) -> HandlerResult<String> {
178 Self::flow_id_at(context, 0)
179 }
180
181 pub fn flow_id_at(context: &Context, index: usize) -> HandlerResult<String> {
183 context.sub_flow_id(index)
184 }
185}