Skip to main content

uv_static/
lib.rs

1pub use env_vars::*;
2
3mod env_vars;
4
5use thiserror::Error;
6
7#[derive(Debug, Error)]
8#[error("Failed to parse environment variable `{name}` with invalid value `{value}`: {err}")]
9pub struct InvalidEnvironmentVariable {
10    pub name: String,
11    pub value: String,
12    pub err: String,
13}
14
15/// Parse a boolean environment variable.
16///
17/// Adapted from Clap's `BoolishValueParser` which is dual licensed under the MIT and Apache-2.0.
18pub fn parse_boolish_environment_variable(
19    name: &'static str,
20) -> Result<Option<bool>, InvalidEnvironmentVariable> {
21    // See `clap_builder/src/util/str_to_bool.rs`
22    // We want to match Clap's accepted values
23
24    // True values are `y`, `yes`, `t`, `true`, `on`, and `1`.
25    const TRUE_LITERALS: [&str; 6] = ["y", "yes", "t", "true", "on", "1"];
26
27    // False values are `n`, `no`, `f`, `false`, `off`, and `0`.
28    const FALSE_LITERALS: [&str; 6] = ["n", "no", "f", "false", "off", "0"];
29
30    // Converts a string literal representation of truth to true or false.
31    //
32    // `false` values are `n`, `no`, `f`, `false`, `off`, and `0` (case insensitive).
33    //
34    // Any other value will be considered as `true`.
35    fn str_to_bool(val: impl AsRef<str>) -> Option<bool> {
36        let pat: &str = &val.as_ref().to_lowercase();
37        if TRUE_LITERALS.contains(&pat) {
38            Some(true)
39        } else if FALSE_LITERALS.contains(&pat) {
40            Some(false)
41        } else {
42            None
43        }
44    }
45
46    let Some(value) = std::env::var_os(name) else {
47        return Ok(None);
48    };
49
50    let Some(value) = value.to_str() else {
51        return Err(InvalidEnvironmentVariable {
52            name: name.to_string(),
53            value: value.to_string_lossy().to_string(),
54            err: "expected a valid UTF-8 string".to_string(),
55        });
56    };
57
58    let Some(value) = str_to_bool(value) else {
59        return Err(InvalidEnvironmentVariable {
60            name: name.to_string(),
61            value: value.to_string(),
62            err: "expected a boolish value".to_string(),
63        });
64    };
65
66    Ok(Some(value))
67}