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
mod checklist;
mod coolid;
pub mod fs;
pub mod metrics;
pub mod net;
pub mod rand;
pub mod uri;
use std::fmt::Debug;
use std::ops::Range;
use std::str::FromStr;
pub use checklist::{print_checklist, run_checklist, BLUE_COLOR, GREEN_COLOR, RED_COLOR};
pub use coolid::new_coolid;
use tracing::{error, info};
pub fn chunk_range(range: Range<usize>, chunk_size: usize) -> impl Iterator<Item = Range<usize>> {
range.clone().step_by(chunk_size).map(move |block_start| {
let block_end = (block_start + chunk_size).min(range.end);
block_start..block_end
})
}
pub fn into_u64_range(range: Range<usize>) -> Range<u64> {
range.start as u64..range.end as u64
}
pub fn setup_logging_for_tests() {
use std::sync::Once;
static INIT: Once = Once::new();
INIT.call_once(|| {
env_logger::builder().format_timestamp(None).init();
});
}
pub fn split_file(split_id: &str) -> String {
format!("{}.split", split_id)
}
pub fn get_from_env<T: FromStr + Debug>(key: &str, default_value: T) -> T {
if let Ok(value_str) = std::env::var(key) {
if let Ok(value) = T::from_str(&value_str) {
info!(value=?value, "Setting `{}` from environment", key);
return value;
} else {
error!(value_str=%value_str, "Failed to parse `{}` from environment", key);
}
}
info!(value=?default_value, "Setting `{}` from default", key);
default_value
}
#[cfg(test)]
mod tests {
#[test]
fn test_get_from_env() {
const TEST_KEY: &str = "TEST_KEY";
assert_eq!(super::get_from_env(TEST_KEY, 10), 10);
std::env::set_var(TEST_KEY, "15");
assert_eq!(super::get_from_env(TEST_KEY, 10), 15);
std::env::set_var(TEST_KEY, "1invalidnumber");
assert_eq!(super::get_from_env(TEST_KEY, 10), 10);
}
}