crb_download/
progress.rs

1#[derive(Debug)]
2pub enum Progress<T> {
3    Working(Option<u8>),
4    Done(T),
5}
6
7#[derive(Debug)]
8pub struct ProgressCalc {
9    total: Option<u64>,
10    processed: u64,
11}
12
13impl ProgressCalc {
14    pub fn new(total: Option<u64>) -> Self {
15        Self {
16            total,
17            processed: 0,
18        }
19    }
20
21    pub fn change_total(&mut self, total: Option<u64>) {
22        self.total = total;
23    }
24
25    pub fn inc(&mut self, value: u64) {
26        self.processed += value;
27    }
28
29    pub fn set(&mut self, value: u64) {
30        self.processed = value;
31    }
32
33    pub fn progress(&self) -> Option<u8> {
34        self.total
35            .as_ref()
36            .map(|total| self.processed * 100 / total)
37            .map(|value| value.clamp(0, 100))
38            .and_then(|pct| pct.try_into().ok())
39    }
40}