Skip to main content

aster_forge_utils/
bool_like.rs

1//! Boolean-like string parsing.
2//!
3//! This module normalizes common human-facing boolean spellings used in runtime
4//! configuration rows and environment-like settings. It deliberately returns an
5//! `Option<bool>` so callers can decide whether invalid input should fail
6//! closed, fall back to a default, or surface a product-specific validation
7//! error.
8
9/// Parses common boolean spellings such as `true`, `1`, `yes`, and `on`.
10pub fn parse_bool_like(value: &str) -> Option<bool> {
11    match value.trim().to_ascii_lowercase().as_str() {
12        "true" | "1" | "yes" | "on" => Some(true),
13        "false" | "0" | "no" | "off" => Some(false),
14        _ => None,
15    }
16}
17
18#[cfg(test)]
19mod tests {
20    use super::parse_bool_like;
21
22    #[test]
23    fn parses_supported_true_values() {
24        for value in ["true", " TRUE ", "1", "yes", "on"] {
25            assert_eq!(parse_bool_like(value), Some(true));
26        }
27    }
28
29    #[test]
30    fn parses_supported_false_values() {
31        for value in ["false", " FALSE ", "0", "no", "off"] {
32            assert_eq!(parse_bool_like(value), Some(false));
33        }
34    }
35
36    #[test]
37    fn rejects_unknown_values() {
38        for value in ["", "  ", "maybe", "truthy"] {
39            assert_eq!(parse_bool_like(value), None);
40        }
41    }
42}