batch_processing/sync/job/job_builder.rs
1use crate::sync::job::Job;
2use crate::sync::step::SyncStep;
3
4/// A trait for building synchronous jobs.
5pub trait JobBuilderTrait {
6 /// Validates the builder configuration.
7 ///
8 /// # Returns
9 ///
10 /// Returns a modified builder instance if validation succeeds.
11 fn validate(self) -> Self;
12
13 /// Adds a step to the job.
14 ///
15 /// # Arguments
16 ///
17 /// * `step` - The synchronous step to add.
18 ///
19 /// # Returns `Self`
20 ///
21 /// Returns a modified builder instance.
22 fn step(self, step: SyncStep) -> Self;
23
24 /// Configures the job to run in multithreaded mode.
25 ///
26 /// # Arguments
27 ///
28 /// * `max_threads` - The maximum number of threads allowed for multithreaded execution.
29 ///
30 /// # Returns `Self`
31 ///
32 /// Returns a modified builder instance.
33 fn multi_threaded(self, max_threads: usize) -> Self;
34
35 /// Initializes a new builder instance with the given name.
36 ///
37 /// # Arguments
38 ///
39 /// * `name` - The name of the job.
40 ///
41 /// # Returns `Self`
42 ///
43 /// Returns a new builder instance.
44 fn get(name: String) -> Self;
45
46 /// Builds and returns the configured synchronous job.
47 ///
48 /// # Returns `Job`
49 ///
50 /// Returns the configured synchronous job.
51 fn build(self) -> Job;
52}
53
54/// A builder struct for constructing synchronous jobs.
55pub struct JobBuilder {
56 /// The job being constructed.
57 job: Job,
58}
59
60impl JobBuilderTrait for JobBuilder {
61 /// Validates the builder configuration by ensuring at least one step is added to the job.
62 ///
63 /// # Returns `Self`
64 ///
65 /// Returns a modified builder instance if validation succeeds.
66 fn validate(self) -> Self {
67 self
68 }
69
70 /// Adds a step to the job.
71 ///
72 /// # Arguments
73 ///
74 /// * `step` - The synchronous step to add.
75 ///
76 /// # Returns `Self`
77 ///
78 /// Returns a modified builder instance.
79 fn step(mut self, step: SyncStep) -> Self {
80 self.job.steps.push(step);
81 self
82 }
83
84 /// Configures the job to run in multithreaded mode.
85 ///
86 /// # Arguments
87 ///
88 /// * `max_threads` - The maximum number of threads allowed for multithreaded execution.
89 ///
90 /// # Returns `Self`
91 ///
92 /// Returns a modified builder instance.
93 fn multi_threaded(self, max_threads: usize) -> Self {
94 JobBuilder {
95 job: Job {
96 max_threads: Some(max_threads),
97 multi_threaded: Some(true),
98 ..self.job
99 }
100 }
101 }
102
103 /// Initializes a new builder instance with the given name.
104 ///
105 /// # Arguments
106 ///
107 /// * `name` - The name of the job.
108 ///
109 /// # Returns `Self`
110 ///
111 /// Returns a new builder instance.
112 #[inline]
113 fn get(name: String) -> Self {
114 JobBuilder {
115 job: Job {
116 name,
117 start_time: None,
118 end_time: None,
119 steps: Vec::new(),
120 multi_threaded: None,
121 max_threads: None,
122 }
123 }
124 }
125
126 /// Builds and returns the configured synchronous job.
127 ///
128 /// # Returns `Job`
129 ///
130 /// Returns the configured synchronous job.
131 fn build(self) -> Job {
132 let current_self = self.validate();
133 return current_self.job;
134 }
135}