Skip to main content

qubit_progress/reporter/
progress_reporter.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 crate::model::ProgressEvent;
11
12/// Receives immutable progress events for one logical operation.
13///
14/// A reporter normally receives one logical operation's event stream. If an
15/// implementation multiplexes multiple operations into one sink, that routing
16/// information is reporter-specific metadata and is intentionally not part of
17/// [`ProgressEvent`].
18///
19/// # Examples
20///
21/// ```
22/// use std::sync::Mutex;
23/// use std::time::Duration;
24///
25/// use qubit_progress::{
26///     ProgressEvent,
27///     ProgressMetric,
28///     ProgressPhase,
29///     ProgressReporter,
30///     ProgressSchema,
31/// };
32///
33/// #[derive(Default)]
34/// struct RecordingReporter {
35///     phases: Mutex<Vec<ProgressPhase>>,
36/// }
37///
38/// impl ProgressReporter for RecordingReporter {
39///     fn report(&self, event: &ProgressEvent) {
40///         self.phases.lock().expect("phase list should lock").push(event.phase());
41///     }
42/// }
43///
44/// let reporter = RecordingReporter::default();
45/// let schema = ProgressSchema::new(vec![ProgressMetric::new("entries", "Entries")]);
46/// reporter.report(&ProgressEvent::started(schema, Vec::new(), Duration::ZERO));
47///
48/// assert_eq!(
49///     reporter.phases.lock().expect("phase list should lock").as_slice(),
50///     &[ProgressPhase::Started],
51/// );
52/// ```
53pub trait ProgressReporter: Send + Sync {
54    /// Reports one progress event.
55    ///
56    /// # Parameters
57    ///
58    /// * `event` - Immutable progress event to report.
59    ///
60    /// # Panics
61    ///
62    /// Reporter implementations may panic if their output sink fails. Callers
63    /// decide whether reporter panics are propagated or isolated.
64    fn report(&self, event: &ProgressEvent);
65}