Skip to main content

qubit_progress/error/
start_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 encountered while starting a progress operation.
9// qubit-style: allow source-test-pair
10
11use std::error::Error;
12use std::fmt;
13
14use crate::error::ConfigurationError;
15use crate::error::DeliveryError;
16use crate::error::EmissionError;
17
18/// Failure before a usable progress operation can be returned.
19#[derive(Clone, Debug)]
20#[non_exhaustive]
21pub enum StartError {
22    /// Fixed operation metadata is invalid.
23    InvalidConfiguration(ConfigurationError),
24    /// The process-local operation ID space is exhausted.
25    OperationIdExhausted,
26    /// Started was attempted but rejected by the reporter.
27    Delivery(DeliveryError),
28}
29
30impl fmt::Display for StartError {
31    /// Formats the start failure.
32    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            Self::InvalidConfiguration(error) => error.fmt(formatter),
35            Self::OperationIdExhausted => {
36                formatter.write_str("progress operation IDs are exhausted")
37            }
38            Self::Delivery(error) => error.fmt(formatter),
39        }
40    }
41}
42
43impl Error for StartError {
44    /// Returns the nested configuration or delivery failure.
45    fn source(&self) -> Option<&(dyn Error + 'static)> {
46        match self {
47            Self::InvalidConfiguration(error) => Some(error),
48            Self::OperationIdExhausted => None,
49            Self::Delivery(error) => Some(error),
50        }
51    }
52}
53
54impl From<ConfigurationError> for StartError {
55    /// Converts configuration validation into a start failure.
56    fn from(error: ConfigurationError) -> Self {
57        Self::InvalidConfiguration(error)
58    }
59}
60
61impl From<EmissionError> for StartError {
62    /// Converts the only possible start-time emission failures.
63    fn from(error: EmissionError) -> Self {
64        match error {
65            EmissionError::Delivery(error) => Self::Delivery(error),
66            EmissionError::SequenceExhausted => Self::OperationIdExhausted,
67        }
68    }
69}