gpui: Add Global marker trait (#7095)
This should prevent a class of bugs where one queries the wrong type of global, which results in oddities at runtime. Release Notes: - N/A --------- Co-authored-by: Marshall <marshall@zed.dev> Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
This commit is contained in:
parent
7bfa584eb6
commit
e6ebe7974d
59 changed files with 449 additions and 237 deletions
10
crates/release_channel/Cargo.toml
Normal file
10
crates/release_channel/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "release_channel"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[dependencies]
|
||||
gpui = { path = "../gpui" }
|
||||
once_cell = "1.19.0"
|
1
crates/release_channel/LICENSE-GPL
Symbolic link
1
crates/release_channel/LICENSE-GPL
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../LICENSE-GPL
|
130
crates/release_channel/src/lib.rs
Normal file
130
crates/release_channel/src/lib.rs
Normal file
|
@ -0,0 +1,130 @@
|
|||
use gpui::{AppContext, Global};
|
||||
use once_cell::sync::Lazy;
|
||||
use std::env;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub static RELEASE_CHANNEL_NAME: Lazy<String> = if cfg!(debug_assertions) {
|
||||
Lazy::new(|| {
|
||||
env::var("ZED_RELEASE_CHANNEL")
|
||||
.unwrap_or_else(|_| include_str!("../../zed/RELEASE_CHANNEL").to_string())
|
||||
})
|
||||
} else {
|
||||
Lazy::new(|| include_str!("../../zed/RELEASE_CHANNEL").to_string())
|
||||
};
|
||||
#[doc(hidden)]
|
||||
pub static RELEASE_CHANNEL: Lazy<ReleaseChannel> =
|
||||
Lazy::new(|| match RELEASE_CHANNEL_NAME.as_str().trim() {
|
||||
"dev" => ReleaseChannel::Dev,
|
||||
"nightly" => ReleaseChannel::Nightly,
|
||||
"preview" => ReleaseChannel::Preview,
|
||||
"stable" => ReleaseChannel::Stable,
|
||||
_ => panic!("invalid release channel {}", *RELEASE_CHANNEL_NAME),
|
||||
});
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppCommitSha(pub String);
|
||||
|
||||
struct GlobalAppCommitSha(AppCommitSha);
|
||||
|
||||
impl Global for GlobalAppCommitSha {}
|
||||
|
||||
impl AppCommitSha {
|
||||
pub fn try_global(cx: &AppContext) -> Option<AppCommitSha> {
|
||||
cx.try_global::<GlobalAppCommitSha>()
|
||||
.map(|sha| sha.0.clone())
|
||||
}
|
||||
|
||||
pub fn set_global(sha: AppCommitSha, cx: &mut AppContext) {
|
||||
cx.set_global(GlobalAppCommitSha(sha))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
|
||||
pub enum ReleaseChannel {
|
||||
#[default]
|
||||
Dev,
|
||||
Nightly,
|
||||
Preview,
|
||||
Stable,
|
||||
}
|
||||
|
||||
struct GlobalReleaseChannel(ReleaseChannel);
|
||||
|
||||
impl Global for GlobalReleaseChannel {}
|
||||
|
||||
impl ReleaseChannel {
|
||||
pub fn init(cx: &mut AppContext) {
|
||||
cx.set_global(GlobalReleaseChannel(*RELEASE_CHANNEL))
|
||||
}
|
||||
|
||||
pub fn global(cx: &AppContext) -> Self {
|
||||
cx.global::<GlobalReleaseChannel>().0
|
||||
}
|
||||
|
||||
pub fn try_global(cx: &AppContext) -> Option<Self> {
|
||||
cx.try_global::<GlobalReleaseChannel>()
|
||||
.map(|channel| channel.0)
|
||||
}
|
||||
|
||||
pub fn display_name(&self) -> &'static str {
|
||||
match self {
|
||||
ReleaseChannel::Dev => "Zed Dev",
|
||||
ReleaseChannel::Nightly => "Zed Nightly",
|
||||
ReleaseChannel::Preview => "Zed Preview",
|
||||
ReleaseChannel::Stable => "Zed",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dev_name(&self) -> &'static str {
|
||||
match self {
|
||||
ReleaseChannel::Dev => "dev",
|
||||
ReleaseChannel::Nightly => "nightly",
|
||||
ReleaseChannel::Preview => "preview",
|
||||
ReleaseChannel::Stable => "stable",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn url_scheme(&self) -> &'static str {
|
||||
match self {
|
||||
ReleaseChannel::Dev => "zed-dev://",
|
||||
ReleaseChannel::Nightly => "zed-nightly://",
|
||||
ReleaseChannel::Preview => "zed-preview://",
|
||||
ReleaseChannel::Stable => "zed://",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn link_prefix(&self) -> &'static str {
|
||||
match self {
|
||||
ReleaseChannel::Dev => "https://zed.dev/dev/",
|
||||
ReleaseChannel::Nightly => "https://zed.dev/nightly/",
|
||||
ReleaseChannel::Preview => "https://zed.dev/preview/",
|
||||
ReleaseChannel::Stable => "https://zed.dev/",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn release_query_param(&self) -> Option<&'static str> {
|
||||
match self {
|
||||
Self::Dev => None,
|
||||
Self::Nightly => Some("nightly=1"),
|
||||
Self::Preview => Some("preview=1"),
|
||||
Self::Stable => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_zed_link(link: &str) -> Option<&str> {
|
||||
for release in [
|
||||
ReleaseChannel::Dev,
|
||||
ReleaseChannel::Nightly,
|
||||
ReleaseChannel::Preview,
|
||||
ReleaseChannel::Stable,
|
||||
] {
|
||||
if let Some(stripped) = link.strip_prefix(release.link_prefix()) {
|
||||
return Some(stripped);
|
||||
}
|
||||
if let Some(stripped) = link.strip_prefix(release.url_scheme()) {
|
||||
return Some(stripped);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue