Skip to main content

tree_table/utils/
parse_bool.rs

1use alloc::format;
2use alloc::string::{String, ToString};
3
4const BOOL_TRUE_VALUES: &[&str] = &["true", "yes", "y", "on", "t", "enabled", "active"];
5const BOOL_FALSE_VALUES: &[&str] = &["false", "no", "n", "off", "f", "disabled", "inactive"];
6
7/// Flexible bool coercion used when the user/developer explicitly requests
8/// a conversion to boolean (e.g. in table column formatting).
9///
10/// - Numbers are language-independent and extremely useful.
11/// - Common English/config words are accepted case-insensitively.
12/// - Everything else fails with a clear error (no silent `false`).
13pub fn try_parse_bool(s: &str) -> Result<bool, String> {
14    let trimmed = s.trim();
15
16    if trimmed.is_empty() {
17        return Err("Cannot convert empty value to boolean".to_string());
18    }
19
20    // 1. Numeric coercion first (works in any language)
21    if let Ok(num) = trimmed.parse::<f64>() {
22        return Ok(num != 0.0);
23    }
24
25    // 2. String aliases (case-insensitive)
26    let lower = trimmed.to_lowercase();
27
28    if BOOL_TRUE_VALUES.contains(&lower.as_str()) {
29        Ok(true)
30    } else if BOOL_FALSE_VALUES.contains(&lower.as_str()) {
31        Ok(false)
32    } else {
33        Err(format!(
34            "Cannot coerce '{}' to boolean.\n\n\
35                 Accepted values (case-insensitive):\n\
36                 • true / yes / y / on / t / enabled / active\n\
37                 • false / no / n / off / f / disabled / inactive\n\
38                 • Any non-zero number → true\n\
39                 • 0 → false",
40            s
41        ))
42    }
43}