Skip to main content

ralph_workflow/config/
parser.rs

1//! Environment variable parsing helpers.
2//!
3//! The unified config loader (`crate::config::loader`) owns the full
4//! configuration-loading flow; this module keeps only shared helpers.
5
6/// Parse a boolean from an environment variable value.
7///
8/// Accepts common truthy and falsy values:
9/// - Truthy: "1", "true", "yes", "y", "on"
10/// - Falsy: "0", "false", "no", "n", "off"
11///
12/// # Arguments
13///
14/// * `value` - The string value to parse
15///
16/// # Returns
17///
18/// Returns `Some(true)` for truthy values, `Some(false)` for falsy values,
19/// and `None` for empty or unrecognized values.
20pub fn parse_env_bool(value: &str) -> Option<bool> {
21    let normalized = value.trim().to_ascii_lowercase();
22    match normalized.as_str() {
23        "1" | "true" | "yes" | "y" | "on" => Some(true),
24        "0" | "false" | "no" | "n" | "off" => Some(false),
25        _ => None,
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_parse_env_bool() {
35        assert_eq!(parse_env_bool("1"), Some(true));
36        assert_eq!(parse_env_bool("true"), Some(true));
37        assert_eq!(parse_env_bool(" TRUE "), Some(true));
38        assert_eq!(parse_env_bool("on"), Some(true));
39        assert_eq!(parse_env_bool("yes"), Some(true));
40
41        assert_eq!(parse_env_bool("0"), Some(false));
42        assert_eq!(parse_env_bool("false"), Some(false));
43        assert_eq!(parse_env_bool(" FALSE "), Some(false));
44        assert_eq!(parse_env_bool("off"), Some(false));
45        assert_eq!(parse_env_bool("no"), Some(false));
46
47        assert_eq!(parse_env_bool(""), None);
48        assert_eq!(parse_env_bool("maybe"), None);
49    }
50}