1use gpui_shared_string::SharedString;
2
3#[derive(Clone)]
4pub struct EnvVar {
5 pub name: SharedString,
6 /// Value of the environment variable. Also `None` when set to an empty string.
7 pub value: Option<String>,
8}
9
10impl EnvVar {
11 pub fn new(name: SharedString) -> Self {
12 let value = std::env::var(name.as_str()).ok();
13 if value.as_ref().is_some_and(|v| v.is_empty()) {
14 Self { name, value: None }
15 } else {
16 Self { name, value }
17 }
18 }
19
20 pub fn or(self, other: EnvVar) -> EnvVar {
21 if self.value.is_some() { self } else { other }
22 }
23}
24
25/// Creates a `LazyLock<EnvVar>` expression for use in a `static` declaration.
26#[macro_export]
27macro_rules! env_var {
28 ($name:expr) => {
29 ::std::sync::LazyLock::new(|| $crate::EnvVar::new(($name).into()))
30 };
31}
32
33/// Generates a `LazyLock<bool>` expression for use in a `static` declaration. Checks if the
34/// environment variable exists and is non-empty.
35#[macro_export]
36macro_rules! bool_env_var {
37 ($name:expr) => {
38 ::std::sync::LazyLock::new(|| $crate::EnvVar::new(($name).into()).value.is_some())
39 };
40}