qubit_progress/reporter/format/json_metric_snapshot_formatter.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 super::metric_snapshot_formatter::MetricSnapshotFormatter;
11use crate::model::ProgressMetricSnapshot;
12
13/// Formats metric snapshots as compact JSON strings.
14#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
15pub struct JsonMetricSnapshotFormatter;
16
17impl JsonMetricSnapshotFormatter {
18 /// Creates a JSON metric snapshot formatter.
19 ///
20 /// # Returns
21 ///
22 /// A formatter that serializes each snapshot as one compact JSON string.
23 #[inline]
24 pub const fn new() -> Self {
25 Self
26 }
27}
28
29impl MetricSnapshotFormatter for JsonMetricSnapshotFormatter {
30 /// Formats one metric snapshot as compact JSON.
31 ///
32 /// # Parameters
33 ///
34 /// * `snapshot` - Snapshot to serialize.
35 ///
36 /// # Returns
37 ///
38 /// A JSON string representing `snapshot`.
39 ///
40 /// # Panics
41 ///
42 /// Panics if serde serialization unexpectedly fails.
43 #[inline]
44 fn format(&self, snapshot: &ProgressMetricSnapshot) -> String {
45 serde_json::to_string(snapshot).expect("progress metric snapshot should serialize")
46 }
47}