aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
blob: 9bc7cb9350add4c091cafc05b30a6954ab62b755 (plain)
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
118
119
120
121
122
123
124
125
use chrono::Duration;

pub fn sanitize_filename(s: &str) -> String {
    s.split('.')
        .map(sanitize_filename_part)
        .collect::<Vec<String>>()
        .join(".")
}

pub fn sanitize_filename_part(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() {
                c.to_lowercase().collect::<String>()
            } else {
                " ".to_string()
            }
        })
        .collect::<String>()
        .split_whitespace()
        .collect::<Vec<&str>>()
        .join("-")
}

pub fn pretty_print_duration(d: Duration) -> String {
    if d.num_days() > 0 {
        let plural = if d.num_days() > 1 { "s" } else { "" };
        format!("{} day{}", d.num_days(), plural)
    } else if d.num_hours() > 0 {
        format!("{} h", d.num_hours())
    } else if d.num_minutes() > 0 {
        format!("{} min", d.num_minutes())
    } else {
        format!("{} s", d.num_seconds())
    }
}

pub fn pretty_print_bytes(bytes: usize) -> String {
    let ko = bytes / 1024;
    let mo = ko / 1024;
    let go = mo / 1024;
    if go > 0 {
        format!("{} GB", go)
    } else if mo > 0 {
        format!("{} MB", mo)
    } else if ko > 0 {
        format!("{} KB", ko)
    } else {
        format!("{} B", bytes)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sanitize_filename() {
        assert_eq!(sanitize_filename(""), "");
        assert_eq!(sanitize_filename("foo bar 123"), "foo-bar-123");
        assert_eq!(sanitize_filename("foo bar.123"), "foo-bar.123");
        assert_eq!(sanitize_filename("foo ( test+2 ).xml"), "foo-test-2.xml");
    }

    #[test]
    fn test_sanitize_filename_part() {
        assert_eq!(sanitize_filename_part(""), "");
        assert_eq!(sanitize_filename_part("foo123BAZ"), "foo123baz");
        assert_eq!(sanitize_filename_part("foo-123-BAZ"), "foo-123-baz");
        assert_eq!(sanitize_filename_part("[()] */+-!;?<'> ?:"), "");
        assert_eq!(sanitize_filename_part("foo [bar] -- BAZ3"), "foo-bar-baz3");
    }

    #[test]
    fn test_pretty_print_duration() {
        assert_eq!(
            pretty_print_duration(Duration::days(2)),
            "2 days".to_string()
        );
        assert_eq!(
            pretty_print_duration(Duration::hours(30)),
            "1 day".to_string()
        );
        assert_eq!(
            pretty_print_duration(Duration::days(1)),
            "1 day".to_string()
        );
        assert_eq!(
            pretty_print_duration(Duration::hours(15)),
            "15 h".to_string()
        );
        assert_eq!(
            pretty_print_duration(Duration::minutes(70)),
            "1 h".to_string()
        );
        assert_eq!(
            pretty_print_duration(Duration::minutes(44)),
            "44 min".to_string()
        );
        assert_eq!(
            pretty_print_duration(Duration::seconds(100)),
            "1 min".to_string()
        );
        assert_eq!(
            pretty_print_duration(Duration::seconds(7)),
            "7 s".to_string()
        );
        assert_eq!(pretty_print_duration(Duration::zero()), "0 s".to_string());
    }

    #[test]
    fn test_pretty_print_bytes() {
        assert_eq!(pretty_print_bytes(0), "0 B");
        assert_eq!(pretty_print_bytes(10), "10 B");
        assert_eq!(pretty_print_bytes(1024), "1 KB");
        assert_eq!(pretty_print_bytes(1100), "1 KB");
        assert_eq!(pretty_print_bytes(54 * 1024), "54 KB");
        assert_eq!(pretty_print_bytes(1024 * 1024), "1 MB");
        assert_eq!(pretty_print_bytes(1300 * 1024), "1 MB");
        assert_eq!(pretty_print_bytes(79 * 1024 * 1024), "79 MB");
        assert_eq!(pretty_print_bytes(1024 * 1024 * 1024), "1 GB");
        assert_eq!(pretty_print_bytes(1300 * 1024 * 1024), "1 GB");
        assert_eq!(pretty_print_bytes(245 * 1024 * 1024 * 1024), "245 GB");
    }
}