1pub use env_vars::*;
2
3mod env_vars;
4
5use std::borrow::Cow;
6
7use thiserror::Error;
8
9pub const ASTRAL_MIRROR_BASE_URL: &str = "https://releases.astral.sh";
11
12pub 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
23pub 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
30pub 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
43pub fn parse_boolish_environment_variable(
47 name: &'static str,
48) -> Result<Option<bool>, InvalidEnvironmentVariable> {
49 const TRUE_LITERALS: [&str; 6] = ["y", "yes", "t", "true", "on", "1"];
54
55 const FALSE_LITERALS: [&str; 6] = ["n", "no", "f", "false", "off", "0"];
57
58 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}