Skip to content

Commit 5c41f16

Browse files
committed
Simlplify icon generation in build.rs
1 parent bb3e756 commit 5c41f16

File tree

2 files changed

+8
-29
lines changed

2 files changed

+8
-29
lines changed

build.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -181,31 +181,12 @@ async fn download_tabler_icons(client: Rc<awc::Client>, sprite_url: &str) {
181181
let icon_map_path = out_dir.join("icons.rs");
182182
let mut sprite_content = Vec::with_capacity(3 * 1024 * 1024);
183183
copy_url_to_opened_file(&client, sprite_url, &mut sprite_content).await;
184-
generate_icons_rs(&icon_map_path, &sprite_content);
185-
}
186-
187-
fn generate_icons_rs(icon_map_path: &Path, sprite_content: &[u8]) {
188184
let mut file = File::create(icon_map_path).unwrap();
189-
190-
writeln!(
191-
file,
192-
"#[allow(clippy::all)]
193-
use std::collections::HashMap;
194-
use std::sync::LazyLock;
195-
"
196-
)
197-
.unwrap();
198-
writeln!(
199-
file,
200-
"pub static ICON_MAP: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {{"
201-
)
202-
.unwrap();
203-
writeln!(file, "let mut m = HashMap::new();").unwrap();
204-
205-
extract_icons_from_sprite(sprite_content, |name, content| {
206-
writeln!(file, "m.insert({name:?}, r#\"{content}\"#);").unwrap();
185+
file.write_all(b"[").unwrap();
186+
extract_icons_from_sprite(&sprite_content, |name, content| {
187+
writeln!(file, "({name:?}, r#\"{content}\"#),").unwrap();
207188
});
208-
writeln!(file, "m}});").unwrap();
189+
file.write_all(b"]").unwrap();
209190
}
210191

211192
fn extract_icons_from_sprite(sprite_content: &[u8], mut callback: impl FnMut(&str, &str)) {

src/template_helpers.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::borrow::Cow;
1+
use std::{borrow::Cow, collections::HashMap, sync::LazyLock};
22

33
use crate::{app_config::AppConfig, utils::static_filename};
44
use anyhow::Context as _;
@@ -209,10 +209,8 @@ impl CanHelp for AppConfigHelper {
209209
}
210210
}
211211

212-
#[allow(clippy::unreadable_literal)]
213-
mod icons {
214-
include!(concat!(env!("OUT_DIR"), "/icons.rs"));
215-
}
212+
pub static ICON_MAP: LazyLock<HashMap<&'static str, &'static str>> =
213+
LazyLock::new(|| include!(concat!(env!("OUT_DIR"), "/icons.rs")).into());
216214

217215
/// Generate an image with the specified icon.
218216
struct IconImgHelper;
@@ -236,7 +234,7 @@ impl HelperDef for IconImgHelper {
236234
};
237235
let size = params[1].as_u64().unwrap_or(24);
238236

239-
let Some(&inner_content) = icons::ICON_MAP.get(name.as_str()) else {
237+
let Some(&inner_content) = ICON_MAP.get(name.as_str()) else {
240238
log::warn!("icon_img: icon {name} not found");
241239
return Ok(());
242240
};

0 commit comments

Comments
 (0)