zed_env_vars.rs

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