1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use crate::sync::job::Job;
use crate::sync::step::SyncStep;

/// A trait for building synchronous jobs.
pub trait JobBuilderTrait {
    /// Validates the builder configuration.
    ///
    /// # Returns
    ///
    /// Returns a modified builder instance if validation succeeds.
    fn validate(self) -> Self;

    /// Adds a step to the job.
    ///
    /// # Arguments
    ///
    /// * `step` - The synchronous step to add.
    ///
    /// # Returns `Self`
    ///
    /// Returns a modified builder instance.
    fn step(self, step: SyncStep) -> Self;

    /// Configures the job to run in multithreaded mode.
    ///
    /// # Arguments
    ///
    /// * `max_threads` - The maximum number of threads allowed for multithreaded execution.
    ///
    /// # Returns `Self`
    ///
    /// Returns a modified builder instance.
    fn multi_threaded(self, max_threads: usize) -> Self;

    /// Initializes a new builder instance with the given name.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the job.
    ///
    /// # Returns `Self`
    ///
    /// Returns a new builder instance.
    fn get(name: String) -> Self;

    /// Builds and returns the configured synchronous job.
    ///
    /// # Returns `Job`
    ///
    /// Returns the configured synchronous job.
    fn build(self) -> Job;
}

/// A builder struct for constructing synchronous jobs.
pub struct JobBuilder {
    /// The job being constructed.
    job: Job,
}

impl JobBuilderTrait for JobBuilder {
    /// Validates the builder configuration by ensuring at least one step is added to the job.
    ///
    /// # Returns `Self`
    ///
    /// Returns a modified builder instance if validation succeeds.
    fn validate(self) -> Self {
        self
    }

    /// Adds a step to the job.
    ///
    /// # Arguments
    ///
    /// * `step` - The synchronous step to add.
    ///
    /// # Returns `Self`
    ///
    /// Returns a modified builder instance.
    fn step(mut self, step: SyncStep) -> Self {
        self.job.steps.push(step);
        self
    }

    /// Configures the job to run in multithreaded mode.
    ///
    /// # Arguments
    ///
    /// * `max_threads` - The maximum number of threads allowed for multithreaded execution.
    ///
    /// # Returns `Self`
    ///
    /// Returns a modified builder instance.
    fn multi_threaded(self, max_threads: usize) -> Self {
        JobBuilder {
            job: Job {
                max_threads: Some(max_threads),
                multi_threaded: Some(true),
                ..self.job
            }
        }
    }

    /// Initializes a new builder instance with the given name.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the job.
    ///
    /// # Returns `Self`
    ///
    /// Returns a new builder instance.
    #[inline]
    fn get(name: String) -> Self {
        JobBuilder {
            job: Job {
                name,
                start_time: None,
                end_time: None,
                steps: Vec::new(),
                multi_threaded: None,
                max_threads: None,
            }
        }
    }

    /// Builds and returns the configured synchronous job.
    ///
    /// # Returns `Job`
    ///
    /// Returns the configured synchronous job.
    fn build(self) -> Job {
        let current_self = self.validate();
        return current_self.job;
    }
}