qubit_progress/reporter/impls/metric_snapshot_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 qubit_function::{
11 ArcConsumer,
12 Consumer,
13};
14
15use crate::{
16 model::{
17 ProgressEvent,
18 ProgressMetricSnapshot,
19 },
20 reporter::ProgressReporter,
21};
22
23/// Progress reporter that sends metric snapshot objects to a consumer.
24///
25/// This reporter is useful for GUI, database, metrics, and monitoring adapters
26/// that want structured metric snapshots instead of preformatted strings.
27pub struct MetricSnapshotProgressReporter<C = ArcConsumer<ProgressMetricSnapshot>> {
28 /// Consumer receiving metric snapshots.
29 consumer: C,
30}
31
32impl<C> MetricSnapshotProgressReporter<C> {
33 /// Creates a metric snapshot reporter.
34 ///
35 /// # Parameters
36 ///
37 /// * `consumer` - Consumer receiving structured metric snapshots.
38 ///
39 /// # Returns
40 ///
41 /// A reporter that sends one snapshot per event counter to `consumer`.
42 #[inline]
43 pub const fn new(consumer: C) -> Self {
44 Self { consumer }
45 }
46
47 /// Returns the configured consumer.
48 ///
49 /// # Returns
50 ///
51 /// A shared reference to the metric snapshot consumer.
52 #[inline]
53 pub const fn consumer(&self) -> &C {
54 &self.consumer
55 }
56}
57
58impl<C> ProgressReporter for MetricSnapshotProgressReporter<C>
59where
60 C: Consumer<ProgressMetricSnapshot> + Send + Sync,
61{
62 /// Sends every metric snapshot in the event to the configured consumer.
63 ///
64 /// # Parameters
65 ///
66 /// * `event` - Event whose metric snapshots should be consumed.
67 #[inline]
68 fn report(&self, event: &ProgressEvent) {
69 for snapshot in event.metric_snapshots() {
70 self.consumer.accept(&snapshot);
71 }
72 }
73}