Skip to main content

swf_builders/services/
workflow.rs

1use std::collections::HashMap;
2use swf_core::models::authentication::ReferenceableAuthenticationPolicy;
3use swf_core::models::timeout::*;
4use swf_core::models::workflow::*;
5
6use super::authentication::AuthenticationPolicyDefinitionBuilder;
7use super::task::TaskDefinitionBuilder;
8use super::timeout::TimeoutDefinitionBuilder;
9
10/// Builder for WorkflowDefinition
11pub struct WorkflowBuilder {
12    workflow: WorkflowDefinition,
13}
14
15impl WorkflowBuilder {
16    pub fn new() -> Self {
17        Self {
18            workflow: WorkflowDefinition::default(),
19        }
20    }
21
22    pub fn use_dsl(&mut self, version: &str) -> &mut Self {
23        self.workflow.document.dsl = version.to_string();
24        self
25    }
26
27    pub fn with_namespace(&mut self, namespace: &str) -> &mut Self {
28        self.workflow.document.namespace = namespace.to_string();
29        self
30    }
31
32    pub fn with_name(&mut self, name: &str) -> &mut Self {
33        self.workflow.document.name = name.to_string();
34        self
35    }
36
37    pub fn with_version(&mut self, version: &str) -> &mut Self {
38        self.workflow.document.version = version.to_string();
39        self
40    }
41
42    pub fn with_title(&mut self, title: &str) -> &mut Self {
43        self.workflow.document.title = Some(title.to_string());
44        self
45    }
46
47    pub fn with_summary(&mut self, summary: &str) -> &mut Self {
48        self.workflow.document.summary = Some(summary.to_string());
49        self
50    }
51
52    pub fn with_tags(&mut self, tags: HashMap<String, String>) -> &mut Self {
53        self.workflow.document.tags = Some(tags);
54        self
55    }
56
57    pub fn with_timeout(
58        &mut self,
59        configure: impl FnOnce(&mut TimeoutDefinitionBuilder),
60    ) -> &mut Self {
61        let mut timeout_builder = TimeoutDefinitionBuilder::new();
62        configure(&mut timeout_builder);
63        self.workflow.timeout = Some(OneOfTimeoutDefinitionOrReference::Timeout(
64            timeout_builder.build(),
65        ));
66        self
67    }
68
69    pub fn use_authentication(
70        &mut self,
71        name: &str,
72        configure: impl FnOnce(&mut AuthenticationPolicyDefinitionBuilder),
73    ) -> &mut Self {
74        let mut auth_builder = AuthenticationPolicyDefinitionBuilder::new();
75        configure(&mut auth_builder);
76        let auth_policy = auth_builder.build();
77
78        if self.workflow.use_.is_none() {
79            self.workflow.use_ = Some(ComponentDefinitionCollection::default());
80        }
81        if let Some(ref mut use_) = self.workflow.use_ {
82            if use_.authentications.is_none() {
83                use_.authentications = Some(HashMap::new());
84            }
85            if let Some(ref mut authentications) = use_.authentications {
86                authentications.insert(
87                    name.to_string(),
88                    ReferenceableAuthenticationPolicy::Policy(Box::new(auth_policy)),
89                );
90            }
91        }
92        self
93    }
94
95    pub fn do_(
96        &mut self,
97        name: &str,
98        configure: impl FnOnce(&mut TaskDefinitionBuilder),
99    ) -> &mut Self {
100        let mut task_builder = TaskDefinitionBuilder::new();
101        configure(&mut task_builder);
102        let task = task_builder.build();
103        self.workflow.do_.add(name.to_string(), task);
104        self
105    }
106
107    pub fn build(&mut self) -> WorkflowDefinition {
108        std::mem::take(&mut self.workflow)
109    }
110}
111
112impl Default for WorkflowBuilder {
113    fn default() -> Self {
114        Self::new()
115    }
116}