Skip to main content

uv_static/
lib.rs

1pub use env_vars::*;
2
3mod env_vars;
4
5use std::borrow::Cow;
6
7use thiserror::Error;
8
9/// The base URL for the default Astral mirror.
10pub const ASTRAL_MIRROR_BASE_URL: &str = "https://releases.astral.sh";
11
12/// Read the user-configured Astral mirror URL from the environment, if set.
13pub fn astral_mirror_url_from_env() -> Option<String> {
14    std::env::var_os(EnvVars::UV_ASTRAL_MIRROR_URL).and_then(|url| {
15        if url.as_os_str().is_empty() {
16            None
17        } else {
18            Some(url.to_string_lossy().into_owned())
19        }
20    })
21}
22
23/// Return the effective Astral mirror base URL, using the default mirror when unset.
24pub fn astral_mirror_base_url(astral_mirror_url: Option<&str>) -> Cow<'_, str> {
25    custom_astral_mirror_url(astral_mirror_url)
26        .map(|url| Cow::Owned(url.trim_end_matches('/').to_string()))
27        .unwrap_or(Cow::Borrowed(ASTRAL_MIRROR_BASE_URL))
28}
29
30/// Return a user-configured Astral mirror URL, treating empty values as unset.
31pub fn custom_astral_mirror_url(astral_mirror_url: Option<&str>) -> Option<&str> {
32    astral_mirror_url.filter(|url| !url.is_empty())
33}
34
35#[derive(Debug, Error)]
36#[error("Failed to parse environment variable `{name}` with invalid value `{value}`: {err}")]
37pub struct InvalidEnvironmentVariable {
38    pub name: String,
39    pub value: String,
40    pub err: String,
41}
42
43/// Parse a boolean environment variable.
44///
45/// Adapted from Clap's `BoolishValueParser` which is dual licensed under the MIT and Apache-2.0.
46pub fn parse_boolish_environment_variable(
47    name: &'static str,
48) -> Result<Option<bool>, InvalidEnvironmentVariable> {
49    // See `clap_builder/src/util/str_to_bool.rs`
50    // We want to match Clap's accepted values
51
52    // True values are `y`, `yes`, `t`, `true`, `on`, and `1`.
53    const TRUE_LITERALS: [&str; 6] = ["y", "yes", "t", "true", "on", "1"];
54
55    // False values are `n`, `no`, `f`, `false`, `off`, and `0`.
56    const FALSE_LITERALS: [&str; 6] = ["n", "no", "f", "false", "off", "0"];
57
58    // Converts a string literal representation of truth to true or false.
59    //
60    // `false` values are `n`, `no`, `f`, `false`, `off`, and `0` (case insensitive).
61    //
62    // Any other value will be considered as `true`.
63    fn str_to_bool(val: impl AsRef<str>) -> Option<bool> {
64        let pat: &str = &val.as_ref().to_lowercase();
65        if TRUE_LITERALS.contains(&pat) {
66            Some(true)
67        } else if FALSE_LITERALS.contains(&pat) {
68            Some(false)
69        } else {
70            None
71        }
72    }
73
74    let Some(value) = std::env::var_os(name) else {
75        return Ok(None);
76    };
77
78    let Some(value) = value.to_str() else {
79        return Err(InvalidEnvironmentVariable {
80            name: name.to_string(),
81            value: value.to_string_lossy().to_string(),
82            err: "expected a valid UTF-8 string".to_string(),
83        });
84    };
85
86    let Some(value) = str_to_bool(value) else {
87        return Err(InvalidEnvironmentVariable {
88            name: name.to_string(),
89            value: value.to_string(),
90            err: "expected a boolish value".to_string(),
91        });
92    };
93
94    Ok(Some(value))
95}