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

            
17
2885
pub fn format_nanoseconds(nanoseconds: f64) -> String {
18
2885
    if nanoseconds <= f64::EPSILON {
19
69
        String::from("0s")
20
2816
    } else if nanoseconds < 1_000. {
21
        format_float(nanoseconds, "ns")
22
2816
    } else if nanoseconds < 1_000_000. {
23
293
        format_float(nanoseconds / 1_000., "us")
24
2523
    } else if nanoseconds < 1_000_000_000. {
25
2382
        format_float(nanoseconds / 1_000_000., "ms")
26
141
    } else if nanoseconds < 1_000_000_000_000. {
27
141
        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
2885
}
33

            
34
2816
fn format_float(value: f64, suffix: &str) -> String {
35
2816
    if value < 10. {
36
1568
        format!("{:.3}{}", value, suffix)
37
1248
    } else if value < 100. {
38
470
        format!("{:.2}{}", value, suffix)
39
    } else {
40
778
        format!("{:.1}{}", value, suffix)
41
    }
42
2816
}
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
}