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
use std::cmp::Ordering;
pub fn cmp_versions(ver1: &str, ver2: &str) -> Ordering {
let result = raw::cmp_versions(ver1.to_owned(), ver2.to_owned());
match result {
_ if result < 0 => Ordering::Less,
_ if result == 0 => Ordering::Equal,
_ => Ordering::Greater,
}
}
pub enum DiskSpace {
Require(u64),
Free(u64),
}
pub enum NumSys {
Binary,
Decimal,
}
pub fn unit_str(val: u64, base: NumSys) -> String {
let val = val as f64;
let (num, tera, giga, mega, kilo) = match base {
NumSys::Binary => (1024.0_f64, "TiB", "GiB", "MiB", "KiB"),
NumSys::Decimal => (1000.0_f64, "TB", "GB", "MB", "KB"),
};
let powers = [
(num.powi(4), tera),
(num.powi(3), giga),
(num.powi(2), mega),
(num, kilo),
];
for (divisor, unit) in powers {
if val > divisor {
return format!("{:.2} {unit}", val / divisor);
}
}
format!("{val} B")
}
pub fn time_str(seconds: u64) -> String {
if seconds > 60 * 60 * 24 {
return format!(
"{}d {}h {}min {}s",
seconds / 60 / 60 / 24,
(seconds / 60 / 60) % 24,
(seconds / 60) % 60,
seconds % 60,
);
}
if seconds > 60 * 60 {
return format!(
"{}h {}min {}s",
(seconds / 60 / 60) % 24,
(seconds / 60) % 60,
seconds % 60,
);
}
if seconds > 60 {
return format!("{}min {}s", (seconds / 60) % 60, seconds % 60,);
}
format!("{seconds}s")
}
#[cxx::bridge]
pub mod raw {
unsafe extern "C++" {
include!("rust-apt/apt-pkg-c/util.h");
pub fn cmp_versions(ver1: String, ver2: String) -> i32;
}
}