browser_automation_cli/config.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Configuration loading surface (rules layout: `src/config.rs`).
3//!
4//! Product law: **XDG only** — no product environment variables at runtime.
5//! This module re-exports [`crate::xdg`] so the clap rules layout name exists
6//! while a single implementation remains the source of truth.
7
8pub use crate::xdg::*;
9
10/// Resolve effective global wall-clock timeout in seconds.
11///
12/// Priority: explicit CLI `--timeout` when `> 0`, else XDG config `timeout`, else `0`
13/// (no override; per-operation defaults apply).
14pub fn resolve_global_timeout(cli_timeout_secs: u64) -> u64 {
15 if cli_timeout_secs > 0 {
16 return cli_timeout_secs;
17 }
18 load_config()
19 .ok()
20 .and_then(|c| c.timeout)
21 .filter(|&t| t > 0)
22 .unwrap_or(0)
23}
24
25/// Resolve per-step timeout for `run` scripts.
26///
27/// Priority: CLI `--step-timeout` when `> 0`, else inherit `global_timeout_secs`.
28pub fn resolve_step_timeout(cli_step_timeout_secs: u64, global_timeout_secs: u64) -> u64 {
29 if cli_step_timeout_secs > 0 {
30 cli_step_timeout_secs
31 } else {
32 global_timeout_secs
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39
40 #[test]
41 fn resolve_global_timeout_prefers_cli() {
42 // CLI override must win regardless of XDG (we cannot force XDG here).
43 assert_eq!(resolve_global_timeout(42), 42);
44 }
45
46 #[test]
47 fn resolve_step_timeout_inherits_global() {
48 assert_eq!(resolve_step_timeout(0, 30), 30);
49 assert_eq!(resolve_step_timeout(5, 30), 5);
50 assert_eq!(resolve_step_timeout(0, 0), 0);
51 }
52}