Skip to main content
← dvs documentation Rust API reference

dvs/
progress.rs

1use std::io::{self, Read};
2use std::path::Path;
3
4/// Wraps a `Read` and reports bytes read to a callback.
5pub struct ProgressReader<'a, R> {
6    inner: R,
7    on_bytes: &'a (dyn Fn(u64) + Send + Sync),
8}
9
10impl<'a, R: Read> ProgressReader<'a, R> {
11    pub fn new(inner: R, on_bytes: &'a (dyn Fn(u64) + Send + Sync)) -> Self {
12        Self { inner, on_bytes }
13    }
14}
15
16impl<R: Read> Read for ProgressReader<'_, R> {
17    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
18        let n = self.inner.read(buf)?;
19        if n > 0 {
20            (self.on_bytes)(n as u64);
21        }
22        Ok(n)
23    }
24}
25
26/// Callback type for `on_file_start` parameters in `add_files`/`get_files`.
27pub type OnFileStart = dyn Fn(&Path, u64) -> FileProgress + Send + Sync;
28
29pub struct FileProgress {
30    /// Called as bytes are transferred for this specific file.
31    pub on_bytes: Box<dyn Fn(u64) + Send + Sync>,
32    /// Called when this file finishes processing (success or error).
33    pub on_done: Box<dyn Fn(bool) + Send + Sync>,
34}