easy_archive/
tool.rs

1pub fn clean(s: &str) -> String {
2    path_clean::clean(s)
3        .to_string_lossy()
4        .to_string()
5        .replace("\\", "/")
6}
7
8pub fn mode_to_string(mode: u32, is_dir: bool) -> String {
9    let rwx_mapping = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"];
10    let owner = rwx_mapping[((mode >> 6) & 0b111) as usize];
11    let group = rwx_mapping[((mode >> 3) & 0b111) as usize];
12    let others = rwx_mapping[(mode & 0b111) as usize];
13    let d = if is_dir { "d" } else { "-" };
14    format!("{d}{owner}{group}{others}")
15}
16
17fn round(value: f64) -> String {
18    let mut s = format!("{value:.1}");
19    if s.contains('.') {
20        while s.ends_with('0') {
21            s.pop();
22        }
23        if s.ends_with('.') {
24            s.pop();
25        }
26    }
27    s
28}
29
30pub fn human_size(bytes: usize) -> String {
31    if bytes == 0 {
32        return "0".to_string();
33    }
34    let units = ["", "K", "M", "G", "T", "P", "E", "Z", "Y"];
35    let b = bytes as f64;
36    let exponent = (b.log(1024.0)).floor() as usize;
37    let value = b / 1024f64.powi(exponent as i32);
38    let rounded = round(value);
39    format!("{}{}", rounded, units[exponent])
40}