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
115
pub fn gen_range<S: Rng, T>(rng: &mut S, range: RangeInclusive<T>) -> T
7
115
where
8
115
    T: SampleUniform + PartialEq + PartialOrd + Clone,
9
115
{
10
115
    if range.start() == range.end() {
11
15
        range.start().clone()
12
    } else {
13
100
        rng.gen_range(range)
14
    }
15
115
}
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
1
        format_float(nanoseconds / 1_000_000., "ms")
26
23
    } else if nanoseconds < 1_000_000_000_000. {
27
23
        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
23
        format!("{:.3}{}", value, suffix)
37
1
    } else if value < 100. {
38
        format!("{:.2}{}", value, suffix)
39
    } else {
40
1
        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
}