forked from gitui-org/gitui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress.rs
54 lines (45 loc) · 987 Bytes
/
progress.rs
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
//!
use easy_cast::{Conv, ConvFloat};
use std::cmp;
///
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
pub struct ProgressPercent {
/// percent 0..100
pub progress: u8,
}
impl ProgressPercent {
///
pub fn new(current: usize, total: usize) -> Self {
let total = f64::conv(cmp::max(current, total));
let progress = f64::conv(current) / total * 100.0;
let progress = u8::try_conv_nearest(progress).unwrap_or(100);
Self { progress }
}
///
pub const fn empty() -> Self {
Self { progress: 0 }
}
///
pub const fn full() -> Self {
Self { progress: 100 }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_progress_zero_total() {
let prog = ProgressPercent::new(1, 0);
assert_eq!(prog.progress, 100);
}
#[test]
fn test_progress_zero_all() {
let prog = ProgressPercent::new(0, 0);
assert_eq!(prog.progress, 100);
}
#[test]
fn test_progress_rounding() {
let prog = ProgressPercent::new(2, 10);
assert_eq!(prog.progress, 20);
}
}