Skip to main content

run/
runtime.rs

1use std::sync::{LazyLock, Mutex};
2
3#[derive(Debug, Default, Clone, Copy)]
4struct RuntimeSettings {
5    timeout_secs: Option<u64>,
6    timing: bool,
7}
8
9static SETTINGS: LazyLock<Mutex<RuntimeSettings>> =
10    LazyLock::new(|| Mutex::new(RuntimeSettings::default()));
11
12/// Apply defaults from project configuration without overriding explicit CLI settings.
13pub fn apply_config_defaults(timeout_secs: Option<u64>, timing: Option<bool>) {
14    if let Ok(mut settings) = SETTINGS.lock() {
15        if settings.timeout_secs.is_none() {
16            settings.timeout_secs = timeout_secs;
17        }
18        if let Some(timing) = timing {
19            settings.timing |= timing;
20        }
21    }
22}
23
24/// Override the execution timeout from CLI arguments.
25pub fn set_timeout(timeout_secs: Option<u64>) {
26    if let Ok(mut settings) = SETTINGS.lock() {
27        settings.timeout_secs = timeout_secs;
28    }
29}
30
31/// Enable timing output from CLI arguments.
32pub fn enable_timing() {
33    if let Ok(mut settings) = SETTINGS.lock() {
34        settings.timing = true;
35    }
36}
37
38/// Return the configured timeout in seconds, where zero means unlimited.
39pub fn timeout_secs() -> u64 {
40    if let Ok(value) = std::env::var("RUN_TIMEOUT_SECS")
41        && let Ok(parsed) = value.parse::<u64>()
42    {
43        return parsed;
44    }
45
46    SETTINGS
47        .lock()
48        .ok()
49        .and_then(|settings| settings.timeout_secs)
50        .unwrap_or(0)
51}
52
53/// Return whether execution timing should be shown.
54pub fn timing_enabled() -> bool {
55    if std::env::var("RUN_TIMING").is_ok_and(|v| v == "1" || v == "true") {
56        return true;
57    }
58
59    SETTINGS.lock().is_ok_and(|settings| settings.timing)
60}