feature_flags.rs

  1use futures::{channel::oneshot, FutureExt as _};
  2use gpui::{AppContext, Global, Subscription, ViewContext};
  3use std::{
  4    future::Future,
  5    pin::Pin,
  6    task::{Context, Poll},
  7};
  8
  9#[derive(Default)]
 10struct FeatureFlags {
 11    flags: Vec<String>,
 12    staff: bool,
 13}
 14
 15impl FeatureFlags {
 16    fn has_flag<T: FeatureFlag>(&self) -> bool {
 17        if self.staff && T::enabled_for_staff() {
 18            return true;
 19        }
 20
 21        self.flags.iter().any(|f| f.as_str() == T::NAME)
 22    }
 23}
 24
 25impl Global for FeatureFlags {}
 26
 27/// To create a feature flag, implement this trait on a trivial type and use it as
 28/// a generic parameter when called [`FeatureFlagAppExt::has_flag`].
 29///
 30/// Feature flags are enabled for members of Zed staff by default. To disable this behavior
 31/// so you can test flags being disabled, set ZED_DISABLE_STAFF=1 in your environment,
 32/// which will force Zed to treat the current user as non-staff.
 33pub trait FeatureFlag {
 34    const NAME: &'static str;
 35
 36    /// Returns whether this feature flag is enabled for Zed staff.
 37    fn enabled_for_staff() -> bool {
 38        true
 39    }
 40}
 41
 42pub struct Assistant2FeatureFlag;
 43
 44impl FeatureFlag for Assistant2FeatureFlag {
 45    const NAME: &'static str = "assistant2";
 46
 47    fn enabled_for_staff() -> bool {
 48        false
 49    }
 50}
 51
 52pub struct Remoting {}
 53impl FeatureFlag for Remoting {
 54    const NAME: &'static str = "remoting";
 55}
 56
 57pub struct LanguageModels {}
 58impl FeatureFlag for LanguageModels {
 59    const NAME: &'static str = "language-models";
 60}
 61
 62pub struct LlmClosedBeta {}
 63impl FeatureFlag for LlmClosedBeta {
 64    const NAME: &'static str = "llm-closed-beta";
 65}
 66
 67pub struct ZedPro {}
 68impl FeatureFlag for ZedPro {
 69    const NAME: &'static str = "zed-pro";
 70}
 71
 72pub struct NotebookFeatureFlag;
 73
 74impl FeatureFlag for NotebookFeatureFlag {
 75    const NAME: &'static str = "notebooks";
 76}
 77
 78pub struct AutoCommand {}
 79impl FeatureFlag for AutoCommand {
 80    const NAME: &'static str = "auto-command";
 81
 82    fn enabled_for_staff() -> bool {
 83        false
 84    }
 85}
 86
 87pub trait FeatureFlagViewExt<V: 'static> {
 88    fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
 89    where
 90        F: Fn(bool, &mut V, &mut ViewContext<V>) + Send + Sync + 'static;
 91}
 92
 93impl<V> FeatureFlagViewExt<V> for ViewContext<'_, V>
 94where
 95    V: 'static,
 96{
 97    fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
 98    where
 99        F: Fn(bool, &mut V, &mut ViewContext<V>) + 'static,
100    {
101        self.observe_global::<FeatureFlags>(move |v, cx| {
102            let feature_flags = cx.global::<FeatureFlags>();
103            callback(feature_flags.has_flag::<T>(), v, cx);
104        })
105    }
106}
107
108pub trait FeatureFlagAppExt {
109    fn wait_for_flag<T: FeatureFlag>(&mut self) -> WaitForFlag;
110    fn update_flags(&mut self, staff: bool, flags: Vec<String>);
111    fn set_staff(&mut self, staff: bool);
112    fn has_flag<T: FeatureFlag>(&self) -> bool;
113    fn is_staff(&self) -> bool;
114
115    fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
116    where
117        F: FnMut(bool, &mut AppContext) + 'static;
118}
119
120impl FeatureFlagAppExt for AppContext {
121    fn update_flags(&mut self, staff: bool, flags: Vec<String>) {
122        let feature_flags = self.default_global::<FeatureFlags>();
123        feature_flags.staff = staff;
124        feature_flags.flags = flags;
125    }
126
127    fn set_staff(&mut self, staff: bool) {
128        let feature_flags = self.default_global::<FeatureFlags>();
129        feature_flags.staff = staff;
130    }
131
132    fn has_flag<T: FeatureFlag>(&self) -> bool {
133        self.try_global::<FeatureFlags>()
134            .map(|flags| flags.has_flag::<T>())
135            .unwrap_or(false)
136    }
137
138    fn is_staff(&self) -> bool {
139        self.try_global::<FeatureFlags>()
140            .map(|flags| flags.staff)
141            .unwrap_or(false)
142    }
143
144    fn observe_flag<T: FeatureFlag, F>(&mut self, mut callback: F) -> Subscription
145    where
146        F: FnMut(bool, &mut AppContext) + 'static,
147    {
148        self.observe_global::<FeatureFlags>(move |cx| {
149            let feature_flags = cx.global::<FeatureFlags>();
150            callback(feature_flags.has_flag::<T>(), cx);
151        })
152    }
153
154    fn wait_for_flag<T: FeatureFlag>(&mut self) -> WaitForFlag {
155        let (tx, rx) = oneshot::channel::<bool>();
156        let mut tx = Some(tx);
157        let subscription: Option<Subscription>;
158
159        match self.try_global::<FeatureFlags>() {
160            Some(feature_flags) => {
161                subscription = None;
162                tx.take().unwrap().send(feature_flags.has_flag::<T>()).ok();
163            }
164            None => {
165                subscription = Some(self.observe_global::<FeatureFlags>(move |cx| {
166                    let feature_flags = cx.global::<FeatureFlags>();
167                    if let Some(tx) = tx.take() {
168                        tx.send(feature_flags.has_flag::<T>()).ok();
169                    }
170                }));
171            }
172        }
173
174        WaitForFlag(rx, subscription)
175    }
176}
177
178pub struct WaitForFlag(oneshot::Receiver<bool>, Option<Subscription>);
179
180impl Future for WaitForFlag {
181    type Output = bool;
182
183    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
184        self.0.poll_unpin(cx).map(|result| {
185            self.1.take();
186            result.unwrap_or(false)
187        })
188    }
189}