Skip to main content

qubit_progress/reporter/impls/
json_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 super::formatted_progress_reporter::FormattedProgressReporter;
16use crate::{
17    model::ProgressEvent,
18    reporter::{
19        ProgressReporter,
20        format::JsonMetricSnapshotFormatter,
21    },
22};
23
24/// Progress reporter that emits JSON metric snapshot strings to a consumer.
25pub struct JsonProgressReporter<C = ArcConsumer<String>> {
26    /// Formatted reporter using the JSON metric snapshot formatter.
27    inner: FormattedProgressReporter<JsonMetricSnapshotFormatter, C>,
28}
29
30impl<C> JsonProgressReporter<C> {
31    /// Creates a JSON progress reporter.
32    ///
33    /// # Parameters
34    ///
35    /// * `consumer` - Consumer receiving compact JSON strings.
36    ///
37    /// # Returns
38    ///
39    /// A JSON progress reporter using the default snapshot JSON formatter.
40    #[inline]
41    pub fn new(consumer: C) -> Self {
42        Self {
43            inner: FormattedProgressReporter::new(JsonMetricSnapshotFormatter::new(), consumer),
44        }
45    }
46
47    /// Returns the inner formatted reporter.
48    ///
49    /// # Returns
50    ///
51    /// A shared reference to the inner reporter.
52    #[inline]
53    pub const fn inner(&self) -> &FormattedProgressReporter<JsonMetricSnapshotFormatter, C> {
54        &self.inner
55    }
56}
57
58impl<C> ProgressReporter for JsonProgressReporter<C>
59where
60    C: Consumer<String> + Send + Sync,
61{
62    /// Reports one event through the JSON formatter.
63    ///
64    /// # Parameters
65    ///
66    /// * `event` - Event to report.
67    #[inline]
68    fn report(&self, event: &ProgressEvent) {
69        self.inner.report(event);
70    }
71}