Skip to main content

ai_agent/utils/
semantic_boolean.rs

1//! Semantic boolean utilities for interpreting various truthy/falsy values.
2
3/// Convert a semantic value to boolean
4pub fn to_bool(value: &str) -> bool {
5    let lower = value.trim().to_lowercase();
6
7    // Truthy values
8    if lower == "true" || lower == "yes" || lower == "1" || lower == "on" {
9        return true;
10    }
11
12    // Falsy values
13    if lower == "false" || lower == "no" || lower == "0" || lower == "off" || lower == "none" {
14        return false;
15    }
16
17    // Default to false for unknown values
18    false
19}
20
21/// Check if a value is truthy (not empty, not "false", etc.)
22pub fn is_truthy(value: &str) -> bool {
23    !value.trim().is_empty() && to_bool(value)
24}
25
26/// Check if a value is falsy (empty, "false", "no", etc.)
27pub fn is_falsy(value: &str) -> bool {
28    value.trim().is_empty() || !to_bool(value)
29}
30
31/// Parse a boolean from environment variable
32pub fn parse_env_bool(key: &str) -> Option<bool> {
33    std::env::var(key).ok().map(|v| to_bool(&v))
34}
35
36/// Check if an environment variable is truthy
37pub fn is_env_truthy(key: &str) -> bool {
38    parse_env_bool(key).unwrap_or(false)
39}