Skip to main content

qubit_batch/process/impls/
sequential_batch_processor_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_function::{
16    BoxConsumer,
17    Consumer,
18};
19use qubit_progress::reporter::{
20    NoOpProgressReporter,
21    ProgressReporter,
22};
23
24use super::SequentialBatchProcessor;
25
26/// Builder for [`SequentialBatchProcessor`].
27///
28/// Use the builder when the default progress interval or reporter should be
29/// customized.
30///
31/// ```rust
32/// use std::time::Duration;
33///
34/// use qubit_batch::SequentialBatchProcessor;
35///
36/// let processor = SequentialBatchProcessor::builder(|_item: &i32| {})
37///     .report_interval(Duration::ZERO)
38///     .build();
39///
40/// assert_eq!(processor.report_interval(), Duration::ZERO);
41/// ```
42pub struct SequentialBatchProcessorBuilder<Item> {
43    /// Consumer called once for each accepted item.
44    consumer: BoxConsumer<Item>,
45    /// Minimum interval between progress callbacks.
46    report_interval: Duration,
47    /// Reporter receiving batch lifecycle callbacks.
48    reporter: Arc<dyn ProgressReporter>,
49}
50
51impl<Item> SequentialBatchProcessorBuilder<Item> {
52    /// Creates a builder from a consumer.
53    ///
54    /// # Parameters
55    ///
56    /// * `consumer` - Consumer invoked once for each input item.
57    ///
58    /// # Returns
59    ///
60    /// A builder initialized with default sequential processor settings.
61    #[inline]
62    pub fn new<C>(consumer: C) -> Self
63    where
64        C: Consumer<Item> + 'static,
65    {
66        Self {
67            consumer: consumer.into_box(),
68            report_interval: SequentialBatchProcessor::<Item>::DEFAULT_REPORT_INTERVAL,
69            reporter: Arc::new(NoOpProgressReporter),
70        }
71    }
72
73    /// Sets the progress-report interval.
74    ///
75    /// # Parameters
76    ///
77    /// * `report_interval` - Minimum time between due-based running progress
78    ///   callbacks. [`Duration::ZERO`] reports at every sequential
79    ///   between-item progress point.
80    ///
81    /// # Returns
82    ///
83    /// This builder for fluent configuration.
84    #[inline]
85    pub const fn report_interval(mut self, report_interval: Duration) -> Self {
86        self.report_interval = report_interval;
87        self
88    }
89
90    /// Sets the progress reporter used by built processors.
91    ///
92    /// # Parameters
93    ///
94    /// * `reporter` - Progress reporter used for later processing calls.
95    ///
96    /// # Returns
97    ///
98    /// This builder for fluent configuration.
99    #[inline]
100    pub fn reporter<R>(mut self, reporter: R) -> Self
101    where
102        R: ProgressReporter + 'static,
103    {
104        self.reporter = Arc::new(reporter);
105        self
106    }
107
108    /// Sets the shared progress reporter used by built processors.
109    ///
110    /// # Parameters
111    ///
112    /// * `reporter` - Shared progress reporter used for later processing calls.
113    ///
114    /// # Returns
115    ///
116    /// This builder for fluent configuration.
117    #[inline]
118    pub fn reporter_arc(mut self, reporter: Arc<dyn ProgressReporter>) -> Self {
119        self.reporter = reporter;
120        self
121    }
122
123    /// Disables progress callbacks by using [`NoOpProgressReporter`].
124    ///
125    /// # Returns
126    ///
127    /// This builder for fluent configuration.
128    #[inline]
129    pub fn no_reporter(mut self) -> Self {
130        self.reporter = Arc::new(NoOpProgressReporter);
131        self
132    }
133
134    /// Builds a [`SequentialBatchProcessor`].
135    ///
136    /// # Returns
137    ///
138    /// A sequential batch processor with this builder's configuration.
139    #[inline]
140    pub fn build(self) -> SequentialBatchProcessor<Item> {
141        SequentialBatchProcessor {
142            consumer: self.consumer,
143            report_interval: self.report_interval,
144            reporter: self.reporter,
145        }
146    }
147}