1
use std::{ops::RangeInclusive, process::Command};
2

            
3
use rand::{distributions::uniform::SampleUniform, Rng};
4
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
5

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

            
17
25
pub fn format_nanoseconds(nanoseconds: f64) -> String {
18
25
    if nanoseconds <= f64::EPSILON {
19
1
        String::from("0s")
20
24
    } else if nanoseconds < 1_000. {
21
        format_float(nanoseconds, "ns")
22
24
    } else if nanoseconds < 1_000_000. {
23
        format_float(nanoseconds / 1_000., "us")
24
24
    } else if nanoseconds < 1_000_000_000. {
25
2
        format_float(nanoseconds / 1_000_000., "ms")
26
22
    } else if nanoseconds < 1_000_000_000_000. {
27
22
        format_float(nanoseconds / 1_000_000_000., "s")
28
    } else {
29
        // this hopefully is unreachable...
30
        format_float(nanoseconds / 1_000_000_000. / 60., "m")
31
    }
32
25
}
33

            
34
24
fn format_float(value: f64, suffix: &str) -> String {
35
24
    if value < 10. {
36
19
        format!("{:.3}{}", value, suffix)
37
5
    } else if value < 100. {
38
3
        format!("{:.2}{}", value, suffix)
39
    } else {
40
2
        format!("{:.1}{}", value, suffix)
41
    }
42
24
}
43

            
44
5
pub fn current_timestamp_string() -> String {
45
5
    OffsetDateTime::now_utc().format(&Rfc3339).unwrap()
46
5
}
47

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