diff options
| author | Fuwn <[email protected]> | 2026-05-27 09:29:35 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-05-27 09:29:35 +0000 |
| commit | 1a92b97846910bc0649c450612839dad3762a101 (patch) | |
| tree | 964288b34afecf0112dc0cb2ebc8ce8e04282cf6 /src/cache.gleam | |
| parent | refactor: Extract query parameter helpers and defaults (diff) | |
| download | mayu-1a92b97846910bc0649c450612839dad3762a101.tar.xz mayu-1a92b97846910bc0649c450612839dad3762a101.zip | |
refactor: Simplify cache loading with filter_map and dict.from_list
Diffstat (limited to 'src/cache.gleam')
| -rw-r--r-- | src/cache.gleam | 51 |
1 files changed, 25 insertions, 26 deletions
diff --git a/src/cache.gleam b/src/cache.gleam index 7cd40a5..f3baade 100644 --- a/src/cache.gleam +++ b/src/cache.gleam @@ -16,42 +16,41 @@ pub type ThemeCache = Dict(String, Dict(Int, CachedImage)) pub fn load_themes() { - list.fold( - case simplifile.read_directory("./themes") { - Ok(files) -> files - Error(_) -> { - wisp.log_error("Error reading themes directory") + let themes = case simplifile.read_directory("./themes") { + Ok(files) -> files + Error(_) -> { + wisp.log_error("Error reading themes directory") - [] - } - }, - dict.new(), - fn(accumulated_themes, theme) { - dict.insert(accumulated_themes, theme, load_theme(theme)) - }, - ) + [] + } + } + + themes + |> list.map(fn(theme) { #(theme, load_theme(theme)) }) + |> dict.from_list } fn load_theme(theme) -> Dict(Int, CachedImage) { let theme_directory = "./themes/" <> theme - - case simplifile.read_directory(theme_directory) { - Ok(files) -> - list.fold(files, dict.new(), fn(accumulated_digits, file) { - case parse_digit_filename(file) { - Ok(digit) -> - load_cached_image(theme_directory <> "/" <> file) - |> result.map(dict.insert(accumulated_digits, digit, _)) - |> result.unwrap(accumulated_digits) - Error(_) -> accumulated_digits - } - }) + let files = case simplifile.read_directory(theme_directory) { + Ok(files) -> files Error(_) -> { wisp.log_error("Error reading theme directory " <> theme_directory) - dict.new() + [] } } + + files + |> list.filter_map(fn(file) { + use digit <- result.try(parse_digit_filename(file)) + use cached_image <- result.try(load_cached_image( + theme_directory <> "/" <> file, + )) + + Ok(#(digit, cached_image)) + }) + |> dict.from_list } fn parse_digit_filename(file) { |