batch_processing/sync/step/simple_step/
mod.rs

1use crate::sync::step::{DeciderCallback, SyncStep, StepCallback};
2use crate::sync::step::step_builder::StepBuilderTrait;
3
4/// A trait for building simple synchronous steps.
5pub trait SimpleStepBuilderTrait<I, O> {
6    /// Configures the step with a tasklet callback.
7    ///
8    /// # Arguments
9    ///
10    /// * `step_callback` - The tasklet callback function.
11    ///
12    /// # Returns
13    ///
14    /// Returns a modified builder instance.
15    fn tasklet(self, step_callback: StepCallback) -> Self;
16}
17
18/// A builder struct for constructing simple synchronous steps.
19pub struct SimpleStepBuilder {
20    /// The step being constructed.
21    step: SyncStep,
22}
23
24impl StepBuilderTrait for SimpleStepBuilder {
25    /// Sets the decider callback for the step.
26    ///
27    /// # Arguments
28    ///
29    /// * `decider` - The decider callback function.
30    ///
31    /// # Returns
32    ///
33    /// Returns a modified builder instance.
34    fn decider(self, decider: DeciderCallback) -> Self {
35        SimpleStepBuilder {
36            step: SyncStep {
37                decider: Some(decider),
38                ..self.step
39            }
40        }
41    }
42
43    /// Configures the step to be tolerant to thrown exceptions.
44    ///
45    /// # Returns
46    ///
47    /// Returns a modified builder instance.
48    fn throw_tolerant(self) -> Self {
49        SimpleStepBuilder {
50            step: SyncStep {
51                throw_tolerant: Some(true),
52                ..self.step
53            }
54        }
55    }
56
57    /// Initializes a new builder instance with the given name.
58    ///
59    /// # Arguments
60    ///
61    /// * `name` - The name of the step.
62    ///
63    /// # Returns
64    ///
65    /// Returns a new builder instance.
66    #[inline]
67    fn get(name: String) -> Self {
68        SimpleStepBuilder {
69            step: SyncStep {
70                name,
71                callback: None,
72                decider: None,
73                end_time: None,
74                start_time: None,
75                throw_tolerant: None,
76            }
77        }
78    }
79
80    /// Validates the builder configuration.
81    ///
82    /// # Returns
83    ///
84    /// Returns a modified builder instance if validation succeeds.
85    fn validate(self) -> Self {
86        if self.step.callback.is_none() {
87            panic!("Tasklet is required");
88        }
89
90        if self.step.name.is_empty() {
91            panic!("Name is required");
92        }
93
94        return self;
95    }
96
97    /// Builds and returns the configured synchronous step.
98    ///
99    /// # Returns
100    ///
101    /// Returns the configured synchronous step.
102    fn build(self) -> SyncStep {
103        let current_self = self.validate();
104        return current_self.step;
105    }
106}
107
108impl SimpleStepBuilderTrait<fn(), fn()> for SimpleStepBuilder {
109    /// Configures the step with a tasklet callback.
110    ///
111    /// # Arguments
112    ///
113    /// * `step_callback` - The tasklet callback function.
114    ///
115    /// # Returns
116    ///
117    /// Returns a modified builder instance.
118    fn tasklet(self, step_callback: StepCallback) -> Self {
119        return SimpleStepBuilder {
120            step: SyncStep {
121                callback: Some(step_callback),
122                ..self.step
123            }
124        };
125    }
126}
127
128/// Initializes a new simple step builder with the given name.
129///
130/// # Arguments
131///
132/// * `name` - The name of the step.
133///
134/// # Returns
135///
136/// Returns a new simple step builder instance.
137pub fn get(name: String) -> SimpleStepBuilder {
138    SimpleStepBuilder::get(name)
139}