Skip to main content

lance_core/utils/
parse.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4/// Parse a string into a boolean value.
5pub fn str_is_truthy(val: &str) -> bool {
6    val.eq_ignore_ascii_case("1")
7        | val.eq_ignore_ascii_case("true")
8        | val.eq_ignore_ascii_case("on")
9        | val.eq_ignore_ascii_case("yes")
10        | val.eq_ignore_ascii_case("y")
11}
12
13/// Parse an environment variable as a truthy-only boolean.
14///
15/// Returns `default_value` if the env var is not set.
16/// Returns `true` only for truthy values (1/true/on/yes/y, case-insensitive).
17/// Returns `false` for all other set values.
18pub fn parse_env_as_bool(env_var_name: &str, default_value: bool) -> bool {
19    std::env::var(env_var_name)
20        .ok()
21        .map(|value| str_is_truthy(value.trim()))
22        .unwrap_or(default_value)
23}