1
use std::ops::RangeInclusive;
2
use std::process::Command;
3

            
4
use rand::distributions::uniform::SampleUniform;
5
use rand::Rng;
6
use time::format_description::well_known::Rfc3339;
7
use time::OffsetDateTime;
8

            
9
152
pub fn gen_range<S: Rng, T>(rng: &mut S, range: RangeInclusive<T>) -> T
10
152
where
11
152
    T: SampleUniform + PartialEq + PartialOrd + Clone,
12
152
{
13
152
    if range.start() == range.end() {
14
15
        range.start().clone()
15
    } else {
16
137
        rng.gen_range(range)
17
    }
18
152
}
19

            
20
3054
pub fn format_nanoseconds(nanoseconds: f64) -> String {
21
3054
    if nanoseconds <= f64::EPSILON {
22
69
        String::from("0s")
23
2985
    } else if nanoseconds < 1_000. {
24
        format_float(nanoseconds, "ns")
25
2985
    } else if nanoseconds < 1_000_000. {
26
294
        format_float(nanoseconds / 1_000., "us")
27
2691
    } else if nanoseconds < 1_000_000_000. {
28
2525
        format_float(nanoseconds / 1_000_000., "ms")
29
166
    } else if nanoseconds < 1_000_000_000_000. {
30
166
        format_float(nanoseconds / 1_000_000_000., "s")
31
    } else {
32
        // this hopefully is unreachable...
33
        format_float(nanoseconds / 1_000_000_000. / 60., "m")
34
    }
35
3054
}
36

            
37
2985
fn format_float(value: f64, suffix: &str) -> String {
38
2985
    if value < 10. {
39
1648
        format!("{value:.3}{suffix}")
40
1337
    } else if value < 100. {
41
523
        format!("{value:.2}{suffix}")
42
    } else {
43
814
        format!("{value:.1}{suffix}")
44
    }
45
2985
}
46

            
47
5
pub fn current_timestamp_string() -> String {
48
5
    OffsetDateTime::now_utc().format(&Rfc3339).unwrap()
49
5
}
50

            
51
5
pub fn local_git_rev() -> String {
52
5
    let command_output = Command::new("git")
53
5
        .args(["rev-parse", "HEAD"])
54
5
        .output()
55
5
        .unwrap();
56
5
    String::from_utf8(command_output.stdout)
57
5
        .unwrap()
58
5
        .trim()
59
5
        .to_string()
60
5
}