From 20c2afb5ad23cd4277dc717737f18e06bbd8c1a3 Mon Sep 17 00:00:00 2001 From: Simon Bernier St-Pierre Date: Sat, 8 Aug 2026 22:41:32 -0400 Subject: [PATCH] simplify static assets --- src/assets.rs | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/assets.rs b/src/assets.rs index f9434c9..39a0ece 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -48,12 +48,23 @@ fn content_hash(data: &[u8]) -> String { hex::encode(hasher.finalize()) } -/// Constructs a `StaticAssetStore` from `name => path` pairs, embedding each -/// file's bytes at compile time via `include_bytes!`. +/// Returns the file name portion of a path, e.g. `"../static/style.css"` +/// becomes `"style.css"`. Used to derive an asset's registry key from its +/// path. +fn file_name(path: &'static str) -> &'static str { + match path.rfind('/') { + Some(i) => &path[i + 1..], + None => path, + } +} + +/// Constructs a `StaticAssetStore` from paths, embedding each file's bytes at +/// compile time via `include_bytes!`. Each asset is keyed by its file name +/// (derived from the path), so there's no need to repeat the name. macro_rules! static_assets { - ($($name:literal => $path:literal),* $(,)?) => { + ($($path:literal),* $(,)?) => { StaticAssetStore::new(&[ - $(($name, include_bytes!($path))),* + $((file_name($path), include_bytes!($path))),* ]) }; } @@ -61,18 +72,18 @@ macro_rules! static_assets { /// The app's static assets, loaded once on first use. pub static STORE: LazyLock = LazyLock::new(|| { static_assets! { - "style.css" => "../static/style.css", - "passkey-login.js" => "../static/passkey-login.js", - "passkey-register.js" => "../static/passkey-register.js", - "password-toggle.js" => "../static/password-toggle.js", - "rewards.js" => "../static/rewards.js", - "htmx.min.js" => "../static/htmx.min.js", - "idiomorph-ext.min.js" => "../static/idiomorph-ext.min.js", - "htmx-ws.min.js" => "../static/htmx-ws.min.js", - "favicon.ico" => "../static/favicon.ico", - "favicon-32x32.png" => "../static/favicon-32x32.png", - "apple-touch-icon.png" => "../static/apple-touch-icon.png", - "logo.svg" => "../static/logo.svg", + "../static/style.css", + "../static/passkey-login.js", + "../static/passkey-register.js", + "../static/password-toggle.js", + "../static/rewards.js", + "../static/htmx.min.js", + "../static/idiomorph-ext.min.js", + "../static/htmx-ws.min.js", + "../static/favicon.ico", + "../static/favicon-32x32.png", + "../static/apple-touch-icon.png", + "../static/logo.svg", } });