pub struct NestedProgress {
#[allow(dead_code)] description: String,
total: u64,
current: u64,
children: Vec<NestedProgress>,
}
impl NestedProgress {
pub fn new(description: impl Into<String>, total: u64) -> Self {
NestedProgress {
description: description.into(),
total,
current: 0,
children: Vec::new(),
}
}
pub fn add_child(&mut self, description: impl Into<String>, total: u64) -> &mut NestedProgress {
self.children.push(NestedProgress::new(description, total));
self.children.last_mut().unwrap()
}
pub fn update(&mut self, amount: u64) {
self.current = (self.current + amount).min(self.total);
}
pub fn percent(&self) -> f64 {
if self.total == 0 {
100.0
} else {
(self.current as f64 / self.total as f64) * 100.0
}
}
pub fn child_count(&self) -> usize {
self.children.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_nested_progress_creation() {
let nested = NestedProgress::new("Root task", 100);
assert_eq!(nested.child_count(), 0);
}
#[test]
fn test_add_child() {
let mut nested = NestedProgress::new("Parent", 100);
nested.add_child("Child task", 50);
assert_eq!(nested.child_count(), 1);
}
#[test]
fn test_update() {
let mut nested = NestedProgress::new("Task", 100);
nested.update(50);
assert_eq!(nested.percent(), 50.0);
}
}