Skip to main content

sql_splitter/
progress.rs

1//! Shared progress tracking utilities.
2//!
3//! This module provides a reusable `ProgressReader` wrapper that tracks bytes read
4//! and calls a callback function, enabling byte-based progress bars across all commands.
5
6use std::io::Read;
7
8/// A reader wrapper that tracks bytes read and calls a progress callback.
9///
10/// This is used to provide byte-based progress tracking for commands that
11/// stream through SQL dump files.
12pub struct ProgressReader<R: Read> {
13    reader: R,
14    callback: Box<dyn Fn(u64)>,
15    bytes_read: u64,
16}
17
18impl<R: Read> ProgressReader<R> {
19    /// Create a new ProgressReader wrapping the given reader.
20    ///
21    /// The callback will be called with the total bytes read so far
22    /// after each successful read operation.
23    pub fn new<F>(reader: R, callback: F) -> Self
24    where
25        F: Fn(u64) + 'static,
26    {
27        Self {
28            reader,
29            callback: Box::new(callback),
30            bytes_read: 0,
31        }
32    }
33}
34
35impl<R: Read> Read for ProgressReader<R> {
36    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
37        let n = self.reader.read(buf)?;
38        self.bytes_read += n as u64;
39        (self.callback)(self.bytes_read);
40        Ok(n)
41    }
42}