Skip to main content

qubit_progress/model/
progress_phase.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 std::fmt;
11
12use serde::{
13    Deserialize,
14    Serialize,
15};
16
17/// Lifecycle phase of a progress event.
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
19#[serde(rename_all = "snake_case")]
20pub enum ProgressPhase {
21    /// Operation has started.
22    Started,
23    /// Operation is still running.
24    Running,
25    /// Operation finished successfully.
26    Finished,
27    /// Operation failed.
28    Failed,
29    /// Operation was canceled.
30    Canceled,
31}
32
33impl ProgressPhase {
34    /// Returns the phase as a stable lower-case string.
35    ///
36    /// # Returns
37    ///
38    /// A stable status string suitable for text output.
39    #[inline]
40    pub const fn as_str(self) -> &'static str {
41        match self {
42            Self::Started => "started",
43            Self::Running => "running",
44            Self::Finished => "finished",
45            Self::Failed => "failed",
46            Self::Canceled => "canceled",
47        }
48    }
49
50    /// Returns `true` for terminal phases.
51    ///
52    /// # Returns
53    ///
54    /// `true` for finished, failed, or canceled phases.
55    #[inline]
56    pub const fn is_terminal(self) -> bool {
57        matches!(self, Self::Finished | Self::Failed | Self::Canceled)
58    }
59}
60
61impl fmt::Display for ProgressPhase {
62    /// Formats the phase as a lower-case status word.
63    ///
64    /// # Parameters
65    ///
66    /// * `f` - Formatter receiving the phase text.
67    ///
68    /// # Returns
69    ///
70    /// Formatter result.
71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72        f.write_str(self.as_str())
73    }
74}