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::error::Error;
12use std::fmt;
13
14/// Metric state that prevents checked successful completion.
15#[derive(Clone, Debug, Eq, PartialEq)]
16#[non_exhaustive]
17pub enum CompletionError {
18    /// Active work remains for one metric.
19    ActiveWork {
20        /// Stable metric ID.
21        metric_id: String,
22        /// Number of active work items.
23        active: u64,
24    },
25    /// A known metric total has not been reached.
26    IncompleteTotal {
27        /// Stable metric ID.
28        metric_id: String,
29        /// Number of completed work items.
30        completed: u64,
31        /// Required total work items.
32        total: u64,
33    },
34}
35
36impl fmt::Display for CompletionError {
37    /// Formats the first completion invariant that failed.
38    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            Self::ActiveWork { metric_id, active } => write!(
41                formatter,
42                "metric {metric_id:?} still has {active} active work items at finish"
43            ),
44            Self::IncompleteTotal {
45                metric_id,
46                completed,
47                total,
48            } => write!(
49                formatter,
50                "metric {metric_id:?} completed {completed} work items but total is {total}"
51            ),
52        }
53    }
54}
55
56impl Error for CompletionError {}