use std::path::{Path, PathBuf};
const ICO: &[u8] = include_bytes!("../assets/icon.ico");
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.and_then(Path::parent)
.expect("the crate lives at crates/sefy-cli")
.to_path_buf()
}
struct Entry {
size: u32,
offset: usize,
length: usize,
}
fn entries(ico: &[u8]) -> Vec<Entry> {
let Some(count) = ico
.get(4..6)
.map(|bytes| u16::from_le_bytes([bytes[0], bytes[1]]) as usize)
else {
return Vec::new();
};
let mut out = Vec::with_capacity(count);
for index in 0..count {
let at = 6 + index * 16;
let Some(entry) = ico.get(at..at + 16) else {
break;
};
let size = if entry[0] == 0 {
256
} else {
u32::from(entry[0])
};
let length = u32::from_le_bytes([entry[8], entry[9], entry[10], entry[11]]) as usize;
let offset = u32::from_le_bytes([entry[12], entry[13], entry[14], entry[15]]) as usize;
if ico.len() < offset + length {
continue;
}
out.push(Entry {
size,
offset,
length,
});
}
out
}
#[derive(Debug, PartialEq, Eq)]
enum Level {
Small,
Medium,
Large,
}
fn level_of(image: &image::RgbaImage) -> Level {
let (width, height) = image.dimensions();
let brightness = |x: u32, y: u32| {
let pixel = image.get_pixel(x, y).0;
assert!(
pixel[3] > 40,
"the image is transparent at {x},{y}, where the tile should be"
);
u32::from(pixel[0]) + u32::from(pixel[1]) + u32::from(pixel[2])
};
if brightness(width / 4, height / 2) > 300 {
return Level::Small;
}
if brightness(width / 2, height * 2 / 3) > 170 {
Level::Large
} else {
Level::Medium
}
}
fn decode(entry: &Entry) -> image::RgbaImage {
let payload = &ICO[entry.offset..entry.offset + entry.length];
image::load_from_memory_with_format(payload, image::ImageFormat::Png)
.unwrap_or_else(|e| panic!("the {}px entry is not a PNG: {e}", entry.size))
.to_rgba8()
}
#[test]
fn the_embedded_icon_has_images() {
let entries = entries(ICO);
assert!(!entries.is_empty(), "icon.ico gave no images");
assert_eq!(
u16::from_le_bytes([ICO[2], ICO[3]]),
1,
"icon.ico is not an icon resource"
);
for entry in &entries {
assert!(entry.length > 0, "the {}px image is empty", entry.size);
}
}
#[test]
fn every_size_carries_the_level_that_reads_at_it() {
for entry in entries(ICO) {
let image = decode(&entry);
assert_eq!(
image.width(),
entry.size,
"the {}px entry holds a {}px image",
entry.size,
image.width()
);
let wanted = match entry.size {
..=27 => Level::Small,
28..=63 => Level::Medium,
_ => Level::Large,
};
assert_eq!(
level_of(&image),
wanted,
"the {}px image is not the {wanted:?} master; the level rule of the line is \
S up to 27px, M to 63px, L above",
entry.size
);
}
}
#[test]
fn the_largest_image_comes_first() {
let sizes: Vec<u32> = entries(ICO).iter().map(|entry| entry.size).collect();
let mut sorted = sizes.clone();
sorted.sort_unstable_by(|a, b| b.cmp(a));
assert_eq!(sizes, sorted, "the images are not ordered largest first");
assert!(
sizes.contains(&256) && sizes.contains(&16),
"the icon does not span the sizes Windows asks for: {sizes:?}"
);
}
#[test]
fn all_three_masters_reach_the_icon() {
let levels: Vec<Level> = entries(ICO).iter().map(|e| level_of(&decode(e))).collect();
assert!(
levels.contains(&Level::Small)
&& levels.contains(&Level::Medium)
&& levels.contains(&Level::Large),
"icon.ico does not carry all three levels of the mark: {levels:?}"
);
}
#[test]
fn the_icon_has_no_second_copy() {
let stray = repo_root().join("assets/icon.ico");
assert!(
!stray.exists(),
"assets/icon.ico exists again; the build reads crates/sefy-cli/assets/icon.ico, \
so the two would drift and the stale one is the one nobody looks at"
);
}
#[test]
fn the_docs_site_carries_the_same_mark_as_the_repository() {
for (site, repo) in [
("docs/src/assets/logo.svg", "assets/logo.svg"),
("docs/public/favicon.svg", "assets/logo-s.svg"),
(
"docs/public/apple-touch-icon.png",
"assets/apple-touch-icon.png",
),
] {
let from_site = std::fs::read(repo_root().join(site))
.unwrap_or_else(|e| panic!("cannot read {site}: {e}"));
let from_repo = std::fs::read(repo_root().join(repo))
.unwrap_or_else(|e| panic!("cannot read {repo}: {e}"));
assert!(
from_site == from_repo,
"{site} differs from {repo}; the docs site shows a different mark \
than the repository - re-run the asset exporter"
);
}
}
#[test]
fn the_packaged_crate_carries_the_icon() {
let inside = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets/icon.ico");
assert!(
inside.is_file(),
"the icon is not under the crate directory; cargo would not package it and \
`cargo install sefy` would fail to build on crates.io"
);
}