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`.
10#[must_use]
11pub fn parse_bool_like(value: &str) -> Option<bool> {
12    match value.trim().to_ascii_lowercase().as_str() {
13        "true" | "1" | "yes" | "on" => Some(true),
14        "false" | "0" | "no" | "off" => Some(false),
15        _ => None,
16    }
17}
18
19#[cfg(test)]
20mod tests {
21    use super::parse_bool_like;
22
23    #[test]
24    fn parses_supported_true_values() {
25        for value in ["true", " TRUE ", "1", "yes", "on"] {
26            assert_eq!(parse_bool_like(value), Some(true));
27        }
28    }
29
30    #[test]
31    fn parses_supported_false_values() {
32        for value in ["false", " FALSE ", "0", "no", "off"] {
33            assert_eq!(parse_bool_like(value), Some(false));
34        }
35    }
36
37    #[test]
38    fn rejects_unknown_values() {
39        for value in ["", "  ", "maybe", "truthy"] {
40            assert_eq!(parse_bool_like(value), None);
41        }
42    }
43}