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