diff --git a/crates/theme/src/builtins.rs b/crates/theme/src/builtins.rs index 9c5edd0c2a..e038897b69 100644 --- a/crates/theme/src/builtins.rs +++ b/crates/theme/src/builtins.rs @@ -23,12 +23,16 @@ pub(super) const SOURCES: [(&str, &str); 12] = [ ("vectorcraft-modern-medium", include_str!("../themes/vectorcraft-modern-medium.json")), ]; +static BUILTINS: LazyLock> = LazyLock::new(load); + +/// Parses every embedded file, without the search for reference cycles that +/// `every_builtin_passes_the_full_check` runs on each of them. // An invalid embedded file is a build defect that the built-in tests catch. #[allow(clippy::panic)] -static BUILTINS: LazyLock> = LazyLock::new(|| { +fn load() -> Vec { SOURCES .iter() - .map(|(name, text)| match format::parse(text.as_bytes()) { + .map(|(name, text)| match format::parse_builtin(text.as_bytes()) { Ok(file) => file.theme, Err(errors) => { let list: Vec = errors.0.iter().map(ToString::to_string).collect(); @@ -36,7 +40,7 @@ static BUILTINS: LazyLock> = LazyLock::new(|| { } }) .collect() -}); +} /// The built-in themes, in picker order. Panics if an embedded file is invalid, which the /// built-in tests rule out. @@ -50,3 +54,30 @@ pub fn builtins() -> &'static [ThemeDef] { pub(super) fn suite_default() -> &'static ThemeDef { builtins().iter().find(|t| t.id.as_str() == SUITE_DEFAULT).expect("SUITE_DEFAULT is a built-in") } + +#[cfg(test)] +mod tests { + use super::*; + + /// Every embedded file passes the full check of a theme file, the reference cycles included. + #[test] + fn every_builtin_passes_the_full_check() { + for (name, text) in SOURCES { + if let Err(errors) = format::parse(text.as_bytes()) { + panic!("{name}.json: {errors:?}"); + } + } + } + + /// Loading the built-ins parses each file once and does not search it for reference cycles. + #[test] + fn loading_the_builtins_does_not_search_for_reference_cycles() { + format::CYCLE_SEARCHES.set(0); + let themes = load(); + assert_eq!(themes.len(), SOURCES.len()); + assert_eq!(format::CYCLE_SEARCHES.get(), 0, "cycle searches while loading the built-ins"); + // A file read with the full check is searched. + assert!(format::parse(SOURCES[0].1.as_bytes()).is_ok()); + assert_eq!(format::CYCLE_SEARCHES.get(), 1); + } +} diff --git a/crates/theme/src/format/mod.rs b/crates/theme/src/format/mod.rs index 729099f079..cded6fea39 100644 --- a/crates/theme/src/format/mod.rs +++ b/crates/theme/src/format/mod.rs @@ -17,7 +17,10 @@ mod sizes; mod validate; mod write; +#[cfg(test)] +pub(crate) use parse::CYCLE_SEARCHES; pub use parse::parse; +pub(crate) use parse::parse_builtin; /// Reads an app's size overrides: [`AppSizes::parse`](crate::AppSizes::parse). pub fn parse_app_sizes(bytes: &[u8]) -> Result { diff --git a/crates/theme/src/format/parse.rs b/crates/theme/src/format/parse.rs index f0e6dde89a..30a65c8c9d 100644 --- a/crates/theme/src/format/parse.rs +++ b/crates/theme/src/format/parse.rs @@ -21,6 +21,22 @@ use crate::{AppId, IconPackId, ThemeId}; /// kept for [`write`](super::write). Every field error is reported, not /// only the first. pub fn parse(bytes: &[u8]) -> Result { + parse_with(bytes, true) +} + +/// [`parse`] without the search for reference cycles, for the built-in themes. The built-in tests +/// run [`parse`] on each of them. +pub(crate) fn parse_builtin(bytes: &[u8]) -> Result { + parse_with(bytes, false) +} + +#[cfg(test)] +thread_local! { + /// How many times this thread searched a parsed theme for reference cycles. + pub(crate) static CYCLE_SEARCHES: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +fn parse_with(bytes: &[u8], find_cycles: bool) -> Result { let stop = |path: &[&str], kind| Err(FormatErrors(vec![FormatError { path: path.iter().map(|s| (*s).to_owned()).collect(), kind }])); if bytes.len() > MAX_THEME_BYTES { return stop(&[], ErrorKind::TooLarge { len: bytes.len() }); @@ -46,7 +62,10 @@ pub fn parse(bytes: &[u8]) -> Result { let mut p = Parser::new(); let theme = p.theme(&raw); match theme { + Some(theme) if p.errors.is_empty() && !find_cycles => Ok(ThemeFile { theme, raw }), Some(theme) if p.errors.is_empty() => { + #[cfg(test)] + CYCLE_SEARCHES.set(CYCLE_SEARCHES.get() + 1); let cycles = theme.reference_errors(); if cycles.is_empty() { Ok(ThemeFile { theme, raw }) } else { Err(FormatErrors(cycles)) } }