Skip to main content

qubit_batch/execute/impls/
sequential_batch_executor_builder.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10use std::{
11    sync::Arc,
12    time::Duration,
13};
14
15use qubit_progress::reporter::{
16    NoOpProgressReporter,
17    ProgressReporter,
18};
19
20use super::SequentialBatchExecutor;
21
22/// Builder for [`SequentialBatchExecutor`].
23///
24/// Use the builder when the default progress interval or reporter should be
25/// customized.
26///
27/// ```rust
28/// use std::time::Duration;
29///
30/// use qubit_batch::SequentialBatchExecutor;
31///
32/// let executor = SequentialBatchExecutor::builder()
33///     .report_interval(Duration::ZERO)
34///     .build();
35///
36/// assert_eq!(executor.report_interval(), Duration::ZERO);
37/// ```
38pub struct SequentialBatchExecutorBuilder {
39    /// Minimum interval between progress callbacks.
40    report_interval: Duration,
41    /// Reporter receiving batch lifecycle callbacks.
42    reporter: Arc<dyn ProgressReporter>,
43}
44
45impl SequentialBatchExecutorBuilder {
46    /// Sets the progress-report interval.
47    ///
48    /// # Parameters
49    ///
50    /// * `report_interval` - Minimum time between due-based running progress
51    ///   callbacks. [`Duration::ZERO`] reports at every sequential
52    ///   between-task progress point.
53    ///
54    /// # Returns
55    ///
56    /// This builder for fluent configuration.
57    #[inline]
58    pub const fn report_interval(mut self, report_interval: Duration) -> Self {
59        self.report_interval = report_interval;
60        self
61    }
62
63    /// Sets the progress reporter used by built executors.
64    ///
65    /// # Parameters
66    ///
67    /// * `reporter` - Progress reporter used for later executions.
68    ///
69    /// # Returns
70    ///
71    /// This builder for fluent configuration.
72    #[inline]
73    pub fn reporter<R>(mut self, reporter: R) -> Self
74    where
75        R: ProgressReporter + 'static,
76    {
77        self.reporter = Arc::new(reporter);
78        self
79    }
80
81    /// Sets the shared progress reporter used by built executors.
82    ///
83    /// # Parameters
84    ///
85    /// * `reporter` - Shared progress reporter used for later executions.
86    ///
87    /// # Returns
88    ///
89    /// This builder for fluent configuration.
90    #[inline]
91    pub fn reporter_arc(mut self, reporter: Arc<dyn ProgressReporter>) -> Self {
92        self.reporter = reporter;
93        self
94    }
95
96    /// Disables progress callbacks by using [`NoOpProgressReporter`].
97    ///
98    /// # Returns
99    ///
100    /// This builder for fluent configuration.
101    #[inline]
102    pub fn no_reporter(mut self) -> Self {
103        self.reporter = Arc::new(NoOpProgressReporter);
104        self
105    }
106
107    /// Builds a [`SequentialBatchExecutor`].
108    ///
109    /// # Returns
110    ///
111    /// A sequential batch executor with this builder's configuration.
112    #[inline]
113    pub fn build(self) -> SequentialBatchExecutor {
114        SequentialBatchExecutor {
115            report_interval: self.report_interval,
116            reporter: self.reporter,
117        }
118    }
119}
120
121impl Default for SequentialBatchExecutorBuilder {
122    /// Creates a builder with default sequential batch settings.
123    ///
124    /// # Returns
125    ///
126    /// A builder using five-second progress intervals and no-op reporting.
127    fn default() -> Self {
128        Self {
129            report_interval: SequentialBatchExecutor::DEFAULT_REPORT_INTERVAL,
130            reporter: Arc::new(NoOpProgressReporter),
131        }
132    }
133}