1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//! A progress' report.
use std::borrow::Cow;
use crate::{generation::Generation, task::State, ProgressId};
/// A progress' report.
#[derive(Clone, PartialEq, Debug)]
pub struct Report {
/// The associated progress' identifier.
pub progress_id: ProgressId,
/// The associated progress' label.
pub label: Option<Cow<'static, str>>,
/// The number of accumulative completed units of work
/// (i.e. including sub-reports' completed units).
pub completed: usize,
/// The number of accumulative total units of work
/// (i.e. including sub-reports' total units).
pub total: usize,
/// A fractional representation of accumulative progress
/// (i.e. including sub-reports) within range of `0.0..=1.0`.
pub fraction: f64,
/// A boolean value that indicates whether the tracked progress is indeterminate.
pub is_indeterminate: bool,
/// The associated progress' state.
pub state: State,
/// The reports of the associated progress' children.
pub subreports: Vec<Report>,
/// The generation at which the associated task,
/// or any of its sub-tasks, were most recently changed.
pub(crate) last_change: Generation,
}
impl Report {
pub(crate) fn new(
progress_id: ProgressId,
label: Option<Cow<'static, str>>,
completed: usize,
total: usize,
state: State,
subreports: Vec<Report>,
last_change: Generation,
) -> Self {
let completed = Self::completed(completed, total);
let total = Self::total(completed, total);
let fraction = Self::fraction(completed, total);
let is_indeterminate = Self::is_indeterminate(completed, total);
Self {
progress_id,
label,
completed,
total,
fraction,
is_indeterminate,
state,
subreports,
last_change,
}
}
/// Returns the last change's generation.
pub fn last_change(&self) -> Generation {
self.last_change
}
/// Returns a pruned version with all subreports older than
/// `min_last_change` removed, or `None` if `self` itself is older.
pub fn to_pruned(&self, min_last_change: Generation) -> Option<Self> {
self.clone().into_pruned(min_last_change)
}
/// Consumes the `Report` and returns a pruned version with all subreports
/// older than `min_last_change` removed, or `None` if `self` itself is older.
pub fn into_pruned(mut self, min_last_change: Generation) -> Option<Self> {
if self.prune(min_last_change) {
Some(self)
} else {
None
}
}
fn prune(&mut self, min_last_change: Generation) -> bool {
self.subreports
.retain_mut(|report| report.prune(min_last_change));
self.last_change >= min_last_change
}
fn completed(completed: usize, total: usize) -> usize {
completed.min(total)
}
fn total(completed: usize, total: usize) -> usize {
completed.max(total)
}
fn fraction(completed: usize, total: usize) -> f64 {
match (completed, total) {
(0, 0) => 0.0,
(_, 0) => 1.0,
(completed, total) => 1.0 * (completed as f64) / (total as f64),
}
}
fn is_indeterminate(completed: usize, total: usize) -> bool {
(completed == 0) && (total == 0)
}
pub(crate) fn discrete(&self) -> (usize, usize) {
(self.completed, self.total)
}
}
#[cfg(test)]
mod tests {
use super::*;
mod to_pruned {
use super::*;
#[test]
fn prunes_self() {
let report = Report {
progress_id: ProgressId::new_unique(),
label: None,
completed: 0,
total: 0,
fraction: 0.0,
is_indeterminate: false,
state: State::Running,
subreports: vec![],
last_change: Generation(0),
};
assert_eq!(report.to_pruned(Generation(1)), None);
}
#[test]
fn prunes_subreports() {
let parent_id = ProgressId::new_unique();
let child_id = ProgressId::new_unique();
let grand_child_id = ProgressId::new_unique();
let report = Report {
progress_id: parent_id,
label: None,
completed: 0,
total: 0,
fraction: 0.0,
is_indeterminate: false,
state: State::Running,
subreports: vec![
Report {
progress_id: ProgressId::new_unique(),
label: None,
completed: 0,
total: 0,
fraction: 0.0,
is_indeterminate: false,
state: State::Running,
subreports: vec![],
last_change: Generation(1),
},
Report {
progress_id: child_id,
label: None,
completed: 0,
total: 0,
fraction: 0.0,
is_indeterminate: false,
state: State::Running,
subreports: vec![Report {
progress_id: grand_child_id,
label: None,
completed: 0,
total: 0,
fraction: 0.0,
is_indeterminate: false,
state: State::Running,
subreports: vec![],
last_change: Generation(2),
}],
last_change: Generation(2),
},
],
last_change: Generation(2),
};
let parent = report.to_pruned(Generation(2)).unwrap();
assert_eq!(parent.progress_id, parent_id);
assert_eq!(parent.subreports.len(), 1);
let child = &parent.subreports[0];
assert_eq!(child.progress_id, child_id);
assert_eq!(child.subreports.len(), 1);
let grand_child = &child.subreports[0];
assert_eq!(grand_child.progress_id, grand_child_id);
assert_eq!(grand_child.subreports.len(), 0);
}
}
}