metrale_gpu_sys/
env.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! 2026-09-26: Environment lookups over a key list the caller passes: the RDMA
4//! clients read their device names, GID indices and depths through these. No
5//! key or fallback chain is fixed here.
6//!
7//! Owner: metrale-gpu-sys.
8//! Invariants: none beyond the types.
9
10/// 2026-09-26: The value of the first key that is set, even to the empty
11/// string; `default` when none is.
12pub fn first_set(keys: &[&str], default: &str) -> String {
13    for k in keys {
14        if let Ok(v) = std::env::var(k) {
15            return v;
16        }
17    }
18    default.to_string()
19}
20
21/// 2026-09-26: The value of the first key that is set and non-empty;
22/// `default` when none is.
23pub fn first_nonempty(keys: &[&str], default: &str) -> String {
24    for k in keys {
25        if let Ok(v) = std::env::var(k)
26            && !v.is_empty()
27        {
28            return v;
29        }
30    }
31    default.to_string()
32}
33
34/// 2026-09-26: The first value that parses as `u32`; a set but unparseable key
35/// is skipped. `default` when none parses.
36pub fn first_set_u32(keys: &[&str], default: u32) -> u32 {
37    for k in keys {
38        if let Some(v) = std::env::var(k).ok().and_then(|s| s.parse().ok()) {
39            return v;
40        }
41    }
42    default
43}