use std::collections::HashMap;
use std::env;
use std::fmt::Write as _;
use std::fs;
use std::path::PathBuf;
const SKIN_TONES: [u32; 5] = [0x1F3FB, 0x1F3FC, 0x1F3FD, 0x1F3FE, 0x1F3FF];
fn is_skin_tone(cp: u32) -> bool {
SKIN_TONES.contains(&cp)
}
#[derive(Clone)]
struct RawEntry {
codepoints: Vec<u32>,
emoji: String,
name: String,
group: String,
subgroup: String,
}
const VARIATION_SELECTOR_16: u32 = 0xFE0F;
impl RawEntry {
fn family_key(&self) -> Vec<u32> {
self.codepoints
.iter()
.copied()
.filter(|cp| !is_skin_tone(*cp) && *cp != VARIATION_SELECTOR_16)
.collect()
}
fn modifier_count(&self) -> usize {
self.codepoints.iter().filter(|cp| is_skin_tone(**cp)).count()
}
fn single_tone_index(&self) -> Option<usize> {
let mods: Vec<u32> = self
.codepoints
.iter()
.copied()
.filter(|cp| is_skin_tone(*cp))
.collect();
match mods.as_slice() {
[m] => SKIN_TONES.iter().position(|t| t == m),
_ => None,
}
}
}
struct Row {
emoji: String,
name: String,
group: String,
subgroup: String,
tones: Option<[String; 5]>,
}
fn main() {
let manifest_dir =
env::var("CARGO_MANIFEST_DIR").expect("run this via `cargo run`, not directly");
let data_path = PathBuf::from(&manifest_dir).join("data/emoji-test.txt");
let source = fs::read_to_string(&data_path)
.unwrap_or_else(|e| panic!("failed to read {}: {e}", data_path.display()));
let (raw, all_groups) = parse(&source);
let rows = build_rows(&raw);
let out_path = PathBuf::from(&manifest_dir).join("src/generated.rs");
fs::write(&out_path, render(&rows, &all_groups))
.unwrap_or_else(|e| panic!("failed to write {}: {e}", out_path.display()));
let with_tones = rows.iter().filter(|r| r.tones.is_some()).count();
eprintln!(
"wrote {} ({} grid entries, {} support tones, {} groups)",
out_path.display(),
rows.len(),
with_tones,
all_groups.len()
);
}
fn parse(source: &str) -> (Vec<RawEntry>, Vec<String>) {
let mut entries = Vec::new();
let mut all_groups: Vec<String> = Vec::new();
let mut current_group = String::new();
let mut current_subgroup = String::new();
for (line_no, line) in source.lines().enumerate() {
let line_no = line_no + 1;
let trimmed = line.trim();
if let Some(rest) = trimmed.strip_prefix("# group:") {
current_group = rest.trim().to_string();
all_groups.push(current_group.clone());
continue;
}
if let Some(rest) = trimmed.strip_prefix("# subgroup:") {
current_subgroup = rest.trim().to_string();
continue;
}
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
let mut parts = trimmed.splitn(2, ';');
let codepoints_field = parts
.next()
.unwrap_or_else(|| panic!("line {line_no}: missing codepoint field"));
let rest = parts
.next()
.unwrap_or_else(|| panic!("line {line_no}: missing ';' separator"));
let mut rest_parts = rest.splitn(2, '#');
let status = rest_parts
.next()
.unwrap_or_else(|| panic!("line {line_no}: missing status field"))
.trim();
let comment = rest_parts
.next()
.unwrap_or_else(|| panic!("line {line_no}: missing '#' comment field"))
.trim();
if status != "fully-qualified" {
continue;
}
let codepoints: Vec<u32> = codepoints_field
.split_whitespace()
.map(|hex| {
u32::from_str_radix(hex, 16)
.unwrap_or_else(|e| panic!("line {line_no}: bad codepoint {hex:?}: {e}"))
})
.collect();
if codepoints.is_empty() {
panic!("line {line_no}: no codepoints parsed");
}
let emoji: String = codepoints
.iter()
.map(|&cp| {
char::from_u32(cp)
.unwrap_or_else(|| panic!("line {line_no}: {cp:#X} is not a valid char"))
})
.collect();
let mut tokens = comment.split_whitespace();
tokens.next().unwrap_or_else(|| {
panic!("line {line_no}: comment has no rendered-emoji token: {comment:?}")
});
let version = tokens.next().unwrap_or_else(|| {
panic!("line {line_no}: comment has no version token: {comment:?}")
});
if !is_version_token(version) {
panic!("line {line_no}: expected a version token like \"E1.0\", got {version:?}");
}
let name = tokens.collect::<Vec<_>>().join(" ");
if name.is_empty() {
panic!("line {line_no}: parsed an empty name from comment {comment:?}");
}
if current_group.is_empty() || current_subgroup.is_empty() {
panic!("line {line_no}: data line before any # group:/# subgroup: header");
}
entries.push(RawEntry {
codepoints,
emoji,
name,
group: current_group.clone(),
subgroup: current_subgroup.clone(),
});
}
if entries.is_empty() {
panic!("parsed zero entries — did the file format change?");
}
if all_groups.len() != 10 {
panic!(
"expected 10 '# group:' headers, found {} — did the file format change?",
all_groups.len()
);
}
(entries, all_groups)
}
fn is_version_token(token: &str) -> bool {
let Some(rest) = token.strip_prefix('E') else {
return false;
};
let mut saw_digit = false;
let mut saw_dot = false;
for c in rest.chars() {
if c.is_ascii_digit() {
saw_digit = true;
} else if c == '.' && !saw_dot {
saw_dot = true;
} else {
return false;
}
}
saw_digit
}
const TONE_SUFFIXES: [&str; 5] = [
": light skin tone",
": medium-light skin tone",
": medium skin tone",
": medium-dark skin tone",
": dark skin tone",
];
fn strip_tone_suffix(name: &str) -> &str {
for suffix in TONE_SUFFIXES {
if let Some(stripped) = name.strip_suffix(suffix) {
return stripped;
}
}
name
}
fn build_rows(raw: &[RawEntry]) -> Vec<Row> {
let mut families: HashMap<Vec<u32>, Vec<&RawEntry>> = HashMap::new();
for entry in raw {
families.entry(entry.family_key()).or_default().push(entry);
}
let mut rows = Vec::new();
let mut emitted_keys: std::collections::HashSet<Vec<u32>> = std::collections::HashSet::new();
for entry in raw {
let key = entry.family_key();
if !emitted_keys.insert(key.clone()) {
continue; }
let members = &families[&key];
let neutral = members.iter().find(|m| m.modifier_count() == 0);
let max_modifiers = members.iter().map(|m| m.modifier_count()).max().unwrap_or(0);
if max_modifiers >= 2 {
if let Some(neutral) = neutral {
rows.push(Row {
emoji: neutral.emoji.clone(),
name: neutral.name.clone(),
group: neutral.group.clone(),
subgroup: neutral.subgroup.clone(),
tones: None,
});
}
continue;
}
let single_tone_members: Vec<&&RawEntry> = members
.iter()
.filter(|m| m.modifier_count() == 1)
.collect();
if single_tone_members.is_empty() {
let neutral = neutral.unwrap_or_else(|| {
panic!(
"family with key {key:?} has neither a neutral form nor toned variants"
)
});
rows.push(Row {
emoji: neutral.emoji.clone(),
name: neutral.name.clone(),
group: neutral.group.clone(),
subgroup: neutral.subgroup.clone(),
tones: None,
});
continue;
}
let mut variants: [Option<String>; 5] = Default::default();
for member in &single_tone_members {
let idx = member.single_tone_index().unwrap_or_else(|| {
panic!(
"entry {:?} has an unrecognised single modifier",
member.name
)
});
if variants[idx].is_some() {
panic!("family with key {key:?} has two entries for the same tone");
}
variants[idx] = Some(member.emoji.clone());
}
let tones: [String; 5] = variants.map(|v| {
v.unwrap_or_else(|| {
panic!("family with key {key:?} is missing a tone variant — expected all 5")
})
});
let (canonical_emoji, canonical_name, canonical_group, canonical_subgroup) =
if let Some(neutral) = neutral {
(
neutral.emoji.clone(),
neutral.name.clone(),
neutral.group.clone(),
neutral.subgroup.clone(),
)
} else {
let medium = single_tone_members
.iter()
.find(|m| m.single_tone_index() == Some(2))
.expect("medium tone variant must exist alongside the other 4");
(
medium.emoji.clone(),
strip_tone_suffix(&medium.name).to_string(),
medium.group.clone(),
medium.subgroup.clone(),
)
};
rows.push(Row {
emoji: canonical_emoji,
name: canonical_name,
group: canonical_group,
subgroup: canonical_subgroup,
tones: Some(tones),
});
}
rows
}
fn render(rows: &[Row], all_groups: &[String]) -> String {
let mut out = String::new();
out.push_str(
"// GENERATED FILE. Do not edit by hand.\n\
//\n\
// Produced by `cargo run -p hyprforge-emoji --bin generate` from\n\
// `data/emoji-test.txt`. See that binary's doc comment for what it\n\
// does and why it is a checked-in table rather than a build.rs.\n\n\
use crate::Emoji;\n\n",
);
out.push_str("pub(crate) const GROUPS: &[&str] = &[\n");
for group in all_groups {
writeln!(out, " {group:?},").unwrap();
}
out.push_str("];\n\n");
writeln!(out, "pub(crate) const EMOJIS: &[Emoji] = &[").unwrap();
for row in rows {
match &row.tones {
None => writeln!(
out,
" Emoji {{ emoji: {:?}, name: {:?}, group: {:?}, subgroup: {:?}, tones: None }},",
row.emoji, row.name, row.group, row.subgroup
)
.unwrap(),
Some(tones) => writeln!(
out,
" Emoji {{ emoji: {:?}, name: {:?}, group: {:?}, subgroup: {:?}, tones: Some([{:?}, {:?}, {:?}, {:?}, {:?}]) }},",
row.emoji, row.name, row.group, row.subgroup,
tones[0], tones[1], tones[2], tones[3], tones[4],
)
.unwrap(),
}
}
out.push_str("];\n");
out
}