howudoin/
report.rs

1//! The public structures of progress reports.
2//!
3//! The data structures are serialisable with the `serde` feature.
4use std::fmt;
5
6// ###### PROGRESS #############################################################
7
8/// A progress node.
9///
10/// This structure is serialisable with the `serde` feature.
11#[derive(Debug, Clone, PartialEq)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub struct Progress {
14    /// The report status.
15    #[cfg_attr(feature = "serde", serde(flatten))]
16    pub report: Report,
17
18    /// Any sub nodes.
19    #[cfg_attr(feature = "serde", serde(default))]
20    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Vec::is_empty"))]
21    pub children: Vec<Progress>,
22}
23
24// ###### REPORT ###############################################################
25
26/// The report status.
27///
28/// This structure is serialisable with the `serde` feature.
29#[derive(Default, Clone, Debug, PartialEq)]
30#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
31#[cfg_attr(feature = "serde", serde(default))]
32pub struct Report {
33    /// The report's label.
34    pub label: String,
35
36    /// The report's description. Leave empty if not used.
37    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "String::is_empty"))]
38    pub desc: String,
39
40    /// The progress state.
41    pub state: State,
42
43    /// Accumulation messages.
44    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Vec::is_empty"))]
45    pub accums: Vec<Message>,
46}
47
48// ###### STATE ################################################################
49
50/// The state of the progress.
51///
52/// This structure is serialisable with the `serde` feature.
53#[derive(Debug, Clone, Copy, PartialEq)]
54#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
55pub enum State {
56    /// The report is in progress.
57    InProgress {
58        /// Optional length, if empty, the report is indeterminate.
59        #[cfg_attr(feature = "serde", serde(default))]
60        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
61        len: Option<u64>,
62
63        /// Current report position.
64        pos: u64,
65
66        /// The len/pos should be formatted in bytes.
67        bytes: bool,
68
69        /// **Seconds** remaining.
70        remaining: f32,
71    },
72    /// The progress reporter is finished.
73    ///
74    /// This occurs when [`Tx::finish`] is called.
75    ///
76    /// [`Tx::finish`]: crate::Tx::finish
77    Completed {
78        /// Duration, in **seconds**.
79        duration: f32,
80    },
81    /// The progress was cancelled.
82    Cancelled,
83}
84
85impl Default for State {
86    fn default() -> Self {
87        State::InProgress {
88            len: None,
89            pos: 0,
90            bytes: false,
91            remaining: f32::INFINITY,
92        }
93    }
94}
95
96// ###### MESSAGE ##############################################################
97
98/// An accumulation message.
99///
100/// This structure is serialisable with the `serde` feature.
101#[derive(Debug, Clone, PartialEq)]
102#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
103pub struct Message {
104    /// The severity of the message.
105    pub severity: Severity,
106    /// The message.
107    pub msg: String,
108}
109
110/// Message severity.
111///
112/// This structure is serialisable with the `serde` feature.
113#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
114#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
115#[allow(missing_docs)]
116pub enum Severity {
117    Error,
118    Warn,
119    #[default]
120    Info,
121}
122
123impl fmt::Display for Severity {
124    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125        match self {
126            Self::Error => write!(f, "ERROR"),
127            Self::Warn => write!(f, "WARN"),
128            Self::Info => write!(f, "INFO"),
129        }
130    }
131}