fast_rich/
nested_progress.rs1pub struct NestedProgress {
7 #[allow(dead_code)] description: String,
9 total: u64,
10 current: u64,
11 children: Vec<NestedProgress>,
12}
13
14impl NestedProgress {
15 pub fn new(description: impl Into<String>, total: u64) -> Self {
17 NestedProgress {
18 description: description.into(),
19 total,
20 current: 0,
21 children: Vec::new(),
22 }
23 }
24
25 pub fn add_child(&mut self, description: impl Into<String>, total: u64) -> &mut NestedProgress {
27 self.children.push(NestedProgress::new(description, total));
28 self.children.last_mut().unwrap()
29 }
30
31 pub fn update(&mut self, amount: u64) {
33 self.current = (self.current + amount).min(self.total);
34 }
35
36 pub fn percent(&self) -> f64 {
38 if self.total == 0 {
39 100.0
40 } else {
41 (self.current as f64 / self.total as f64) * 100.0
42 }
43 }
44
45 pub fn child_count(&self) -> usize {
47 self.children.len()
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_nested_progress_creation() {
57 let nested = NestedProgress::new("Root task", 100);
58 assert_eq!(nested.child_count(), 0);
59 }
60
61 #[test]
62 fn test_add_child() {
63 let mut nested = NestedProgress::new("Parent", 100);
64 nested.add_child("Child task", 50);
65 assert_eq!(nested.child_count(), 1);
66 }
67
68 #[test]
69 fn test_update() {
70 let mut nested = NestedProgress::new("Task", 100);
71 nested.update(50);
72 assert_eq!(nested.percent(), 50.0);
73 }
74}