zed/crates/storybook2/src/themes.rs

31 lines
795 B
Rust
Raw Normal View History

2023-10-05 20:42:11 +00:00
mod rose_pine;
2023-09-26 19:42:58 +00:00
2023-10-05 20:42:11 +00:00
pub use rose_pine::*;
2023-10-09 20:37:20 +00:00
2023-10-09 21:00:10 +00:00
use anyhow::{Context, Result};
2023-10-09 20:37:20 +00:00
use gpui3::serde_json;
use serde::Deserialize;
use ui::Theme;
use crate::assets::Assets;
#[derive(Deserialize)]
struct LegacyTheme {
pub base_theme: serde_json::Value,
}
/// Loads the [`Theme`] with the given name.
2023-10-09 21:00:10 +00:00
pub fn load_theme(name: String) -> Result<Theme> {
let theme_contents = Assets::get(&format!("themes/{name}.json"))
2023-10-09 21:00:10 +00:00
.with_context(|| format!("theme file not found: '{name}'"))?;
2023-10-09 20:37:20 +00:00
let legacy_theme: LegacyTheme =
2023-10-09 21:00:10 +00:00
serde_json::from_str(std::str::from_utf8(&theme_contents.data)?)
.context("failed to parse legacy theme")?;
2023-10-09 20:37:20 +00:00
2023-10-09 21:00:10 +00:00
let theme: Theme = serde_json::from_value(legacy_theme.base_theme.clone())
.context("failed to parse `base_theme`")?;
2023-10-09 20:37:20 +00:00
2023-10-09 21:00:10 +00:00
Ok(theme)
2023-10-09 20:37:20 +00:00
}