use super::icons::{IconRole, IconSet, icon_name};
#[must_use]
#[cfg_attr(
not(any(feature = "material-icons", feature = "lucide-icons")),
allow(dead_code)
)]
pub(crate) fn bundled_icon_svg(role: IconRole, set: IconSet) -> Option<&'static [u8]> {
icon_name(role, set).and_then(|name| bundled_icon_by_name(name, set))
}
#[must_use]
#[allow(unreachable_patterns, unused_variables)]
#[cfg_attr(
not(any(feature = "material-icons", feature = "lucide-icons")),
allow(dead_code)
)]
pub(crate) fn bundled_icon_by_name(name: &str, set: IconSet) -> Option<&'static [u8]> {
match set {
#[cfg(feature = "material-icons")]
IconSet::Material => material_svg_by_name(name),
#[cfg(feature = "lucide-icons")]
IconSet::Lucide => lucide_svg_by_name(name),
_ => None,
}
}
#[cfg(feature = "lucide-icons")]
include!(concat!(env!("OUT_DIR"), "/lucide_svg_by_name.rs"));
#[cfg(feature = "material-icons")]
include!(concat!(env!("OUT_DIR"), "/material_svg_by_name.rs"));
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
#[cfg(feature = "material-icons")]
fn material_icons_cover_all_roles() {
for role in IconRole::ALL.iter().copied() {
let svg = bundled_icon_svg(role, IconSet::Material);
assert!(svg.is_some(), "Material icons missing SVG for {:?}", role);
let bytes = svg.unwrap();
let content = std::str::from_utf8(bytes).expect("SVG should be valid UTF-8");
assert!(
content.contains("<svg"),
"Material {:?} does not contain <svg tag",
role
);
}
}
#[test]
#[cfg(feature = "material-icons")]
fn material_icons_total_size_under_200kb() {
let total: usize = IconRole::ALL
.iter()
.filter_map(|role| bundled_icon_svg(*role, IconSet::Material))
.map(|svg| svg.len())
.sum();
assert!(
total < 400 * 1024,
"Material icons total size {} bytes exceeds 400KB budget",
total
);
}
#[test]
#[cfg(feature = "lucide-icons")]
fn lucide_icons_cover_all_roles() {
for role in IconRole::ALL.iter().copied() {
let svg = bundled_icon_svg(role, IconSet::Lucide);
assert!(svg.is_some(), "Lucide icons missing SVG for {:?}", role);
let bytes = svg.unwrap();
let content = std::str::from_utf8(bytes).expect("SVG should be valid UTF-8");
assert!(
content.contains("<svg"),
"Lucide {:?} does not contain <svg tag",
role
);
}
}
#[test]
#[cfg(feature = "lucide-icons")]
fn lucide_icons_total_size_under_100kb() {
let total: usize = IconRole::ALL
.iter()
.filter_map(|role| bundled_icon_svg(*role, IconSet::Lucide))
.map(|svg| svg.len())
.sum();
assert!(
total < 200 * 1024,
"Lucide icons total size {} bytes exceeds 200KB budget",
total
);
}
#[test]
#[cfg(any(feature = "material-icons", feature = "lucide-icons"))]
fn every_role_with_a_name_has_the_bundled_file_of_that_name() {
for set in [IconSet::Material, IconSet::Lucide] {
for role in IconRole::ALL.iter().copied() {
if let Some(name) = icon_name(role, set) {
assert_eq!(
bundled_icon_svg(role, set),
bundled_icon_by_name(name, set),
"{role:?} in {set:?}"
);
}
}
}
}
#[test]
fn non_bundled_sets_return_none() {
assert!(
bundled_icon_svg(IconRole::ActionCopy, IconSet::SfSymbols).is_none(),
"SfSymbols should not be a bundled set"
);
assert!(
bundled_icon_svg(IconRole::ActionCopy, IconSet::Freedesktop).is_none(),
"Freedesktop should not be a bundled set"
);
assert!(
bundled_icon_svg(IconRole::ActionCopy, IconSet::SegoeFluent).is_none(),
"SegoeFluent should not be a bundled set"
);
}
#[test]
#[cfg(all(feature = "lucide-icons", feature = "material-icons"))]
fn retired_bundle_names_return_none() {
for name in [
"close",
"dash",
"inspect",
"resize-corner",
"sort-ascending",
"sort-descending",
"window-close",
"window-maximize",
"window-minimize",
"window-restore",
"trash-2",
] {
assert!(
bundled_icon_by_name(name, IconSet::Lucide).is_none(),
"{name} should be gone"
);
}
assert!(bundled_icon_by_name("star_border", IconSet::Material).is_none());
assert!(bundled_icon_by_name("font_size", IconSet::Material).is_none());
assert!(bundled_icon_by_name("trash", IconSet::Lucide).is_some());
assert!(bundled_icon_by_name("star_fill1", IconSet::Material).is_some());
assert!(bundled_icon_by_name("format_size", IconSet::Material).is_some());
}
#[test]
fn by_name_non_bundled_sets_return_none() {
assert!(bundled_icon_by_name("check", IconSet::SfSymbols).is_none());
assert!(bundled_icon_by_name("check", IconSet::Freedesktop).is_none());
assert!(bundled_icon_by_name("check", IconSet::SegoeFluent).is_none());
}
#[test]
fn by_name_unknown_name_returns_none() {
assert!(bundled_icon_by_name("nonexistent-icon-xyz", IconSet::Lucide).is_none());
assert!(bundled_icon_by_name("nonexistent_icon_xyz", IconSet::Material).is_none());
}
#[cfg(any(feature = "lucide-icons", feature = "material-icons"))]
fn bundled_files(dir: &str) -> Vec<String> {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("icons")
.join(dir);
let mut names: Vec<String> = std::fs::read_dir(&path)
.expect("icon directory exists")
.map(|entry| {
entry
.expect("readable entry")
.file_name()
.to_string_lossy()
.into_owned()
})
.filter_map(|file| file.strip_suffix(".svg").map(str::to_owned))
.collect();
names.sort();
assert!(!names.is_empty(), "icons/{dir} must not be empty");
names
}
#[test]
#[cfg(feature = "lucide-icons")]
fn generated_lucide_table_covers_every_bundled_file() {
for name in bundled_files("lucide") {
let svg = bundled_icon_by_name(&name, IconSet::Lucide)
.unwrap_or_else(|| panic!("Lucide table misses bundled file {name}.svg"));
assert!(std::str::from_utf8(svg).expect("UTF-8").contains("<svg"));
}
}
#[test]
#[cfg(feature = "material-icons")]
fn generated_material_table_covers_every_bundled_file() {
for name in bundled_files("material") {
let svg = bundled_icon_by_name(&name, IconSet::Material)
.unwrap_or_else(|| panic!("Material table misses bundled file {name}.svg"));
assert!(std::str::from_utf8(svg).expect("UTF-8").contains("<svg"));
}
}
}