Skip to main content

qubit_progress/error/
completion_error.rs

1// =============================================================================
2//    Copyright (c) 2025 - 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Errors explaining why checked successful completion is unavailable.
9// qubit-style: allow source-test-pair
10
11use std::{
12    error::Error,
13    fmt,
14};
15
16/// Metric state that prevents checked successful completion.
17#[derive(Clone, Debug, Eq, PartialEq)]
18#[non_exhaustive]
19pub enum CompletionError {
20    /// Active work remains for one metric.
21    ActiveWork {
22        /// Stable metric ID.
23        metric_id: String,
24        /// Number of active work items.
25        active: u64,
26    },
27    /// A known metric total has not been reached.
28    IncompleteTotal {
29        /// Stable metric ID.
30        metric_id: String,
31        /// Number of completed work items.
32        completed: u64,
33        /// Required total work items.
34        total: u64,
35    },
36}
37
38impl fmt::Display for CompletionError {
39    /// Formats the first completion invariant that failed.
40    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Self::ActiveWork { metric_id, active } => write!(
43                formatter,
44                "metric {metric_id:?} still has {active} active work items at finish"
45            ),
46            Self::IncompleteTotal {
47                metric_id,
48                completed,
49                total,
50            } => write!(
51                formatter,
52                "metric {metric_id:?} completed {completed} work items but total is {total}"
53            ),
54        }
55    }
56}
57
58impl Error for CompletionError {}