Merge pull request #4173 from dotCooCoo/fix/theme-builtins-load-once

theme: the built-in themes load without searching them for reference cycles
This commit is contained in:
BFlat authored and GitHub committed 2026-11-29 08:12:50 +07:00
commit fb0dd5ef59
3 files changed
+56 -3

No files matched your search

+34 -3
View File
@@ -23,12 +23,16 @@ pub(super) const SOURCES: [(&str, &str); 12] = [
("vectorcraft-modern-medium", include_str!("../themes/vectorcraft-modern-medium.json")),
];
static BUILTINS: LazyLock<Vec<ThemeDef>> = 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<Vec<ThemeDef>> = LazyLock::new(|| {
fn load() -> Vec<ThemeDef> {
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<String> = errors.0.iter().map(ToString::to_string).collect();
@@ -36,7 +40,7 @@ static BUILTINS: LazyLock<Vec<ThemeDef>> = 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);
}
}
+3
View File
@@ -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<crate::AppSizes, FormatErrors> {
+19
View File
@@ -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<ThemeFile, FormatErrors> {
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<ThemeFile, FormatErrors> {
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<usize> = const { std::cell::Cell::new(0) };
}
fn parse_with(bytes: &[u8], find_cycles: bool) -> Result<ThemeFile, FormatErrors> {
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<ThemeFile, FormatErrors> {
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)) }
}