Skip to main content

readstat/
progress.rs

1//! Progress reporting trait for parsing feedback.
2//!
3//! The [`ProgressCallback`] trait allows callers to receive progress updates
4//! during data parsing without coupling the library to any specific progress
5//! bar implementation. The CLI crate provides an `indicatif`-based implementation.
6
7/// Trait for receiving progress updates during data parsing.
8///
9/// Implement this trait to display a progress bar, log progress, or perform
10/// any other action when the parser makes forward progress.
11///
12/// # Example
13///
14/// ```
15/// use std::sync::Arc;
16/// use readstat::ProgressCallback;
17///
18/// struct LogProgress;
19///
20/// impl ProgressCallback for LogProgress {
21///     fn inc(&self, n: u64) {
22///         println!("Processed {n} more rows");
23///     }
24///     fn parsing_started(&self, path: &str) {
25///         println!("Parsing file: {path}");
26///     }
27/// }
28/// ```
29pub trait ProgressCallback: Send + Sync {
30    /// Called to advance progress by `n` rows. Invoked once per chunk, just
31    /// after that chunk finishes parsing, so `n` counts rows already completed
32    /// (the chunk size) — the displayed position stays in step with work done.
33    fn inc(&self, n: u64);
34
35    /// Called once when parsing begins for the file at `path`.
36    ///
37    /// Implementations should be idempotent: the contract is a single
38    /// "parsing started" notification per parse, but callers may invoke it
39    /// more than once (e.g. the CLI guards against this internally).
40    fn parsing_started(&self, path: &str);
41}