Skip to main content

uv_static/
lib.rs

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