From 0d903f4d0d68ca9e9d42ee77023763fd304a806d Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Mon, 9 Oct 2023 17:00:10 -0400 Subject: [PATCH] Clean up theme loading --- crates/storybook2/src/storybook2.rs | 2 +- crates/storybook2/src/themes.rs | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/storybook2/src/storybook2.rs b/crates/storybook2/src/storybook2.rs index 8b840b75ab..30fbb079a2 100644 --- a/crates/storybook2/src/storybook2.rs +++ b/crates/storybook2/src/storybook2.rs @@ -51,7 +51,7 @@ fn main() { let story_selector = args.story.clone(); let theme_name = args.theme.unwrap_or("One Dark".to_string()); - let theme = themes::load_theme(theme_name); + let theme = themes::load_theme(theme_name).unwrap(); let asset_source = Arc::new(Assets); gpui3::App::production(asset_source).run(move |cx| { diff --git a/crates/storybook2/src/themes.rs b/crates/storybook2/src/themes.rs index b348019e53..8a677952ed 100644 --- a/crates/storybook2/src/themes.rs +++ b/crates/storybook2/src/themes.rs @@ -2,6 +2,7 @@ mod rose_pine; pub use rose_pine::*; +use anyhow::{Context, Result}; use gpui3::serde_json; use serde::Deserialize; use ui::Theme; @@ -14,14 +15,16 @@ struct LegacyTheme { } /// Loads the [`Theme`] with the given name. -pub fn load_theme(name: String) -> Theme { +pub fn load_theme(name: String) -> Result { let theme_contents = Assets::get(&format!("themes/{name}.json")) - .unwrap_or_else(|| panic!("failed to load theme: {name}.json")); + .with_context(|| format!("theme file not found: '{name}'"))?; let legacy_theme: LegacyTheme = - serde_json::from_str(std::str::from_utf8(&theme_contents.data).unwrap()).unwrap(); + serde_json::from_str(std::str::from_utf8(&theme_contents.data)?) + .context("failed to parse legacy theme")?; - let theme: Theme = serde_json::from_value(legacy_theme.base_theme.clone()).unwrap(); + let theme: Theme = serde_json::from_value(legacy_theme.base_theme.clone()) + .context("failed to parse `base_theme`")?; - theme + Ok(theme) }