Damson/build.rs
astar76 11c1a7808b Build the syntax definitions into the app
Highlighting relied on the syntax-highlighting package, so removing it
left every file uncoloured. The 462 definitions now live in assets/syntax
and are compressed into the binary at build time, unpacked per language
when used. User .xml files still add to or replace them, and the licence
each file declares is recorded in assets/syntax/LICENSES.md.

Also moves files to the trash without needing gio.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-06 20:35:27 -06:00

39 lines
1.7 KiB
Rust

//! Packs the syntax definitions into the binary: each file is compressed on its own, so a language
//! is unpacked only when it's used, and its header stays readable so startup needn't unpack any.
use std::fmt::Write as _;
use std::path::Path;
/// The `<language …>` tag, which holds everything needed to choose a definition.
fn header(text: &str) -> &str {
let Some(start) = text.find("<language") else { return "" };
let mut quote = None;
for (i, c) in text[start..].char_indices() {
match (quote, c) {
(None, '"' | '\'') => quote = Some(c),
(Some(q), _) if q == c => quote = None,
(None, '>') => return &text[start..start + i + 1],
_ => {}
}
}
""
}
fn main() {
println!("cargo:rerun-if-changed=assets/syntax");
let out = std::env::var("OUT_DIR").unwrap();
let mut files: Vec<_> = std::fs::read_dir("assets/syntax").unwrap().flatten().map(|e| e.path()).filter(|p| p.extension().is_some_and(|e| e == "xml")).collect();
files.sort();
assert!(!files.is_empty(), "assets/syntax has no definitions");
let mut pack = vec![];
let mut index = String::from("pub const INDEX: &[(&str, usize, usize)] = &[\n");
for f in &files {
let text = std::fs::read_to_string(f).unwrap();
let packed = miniz_oxide::deflate::compress_to_vec(text.as_bytes(), 9);
writeln!(index, " ({:?}, {}, {}),", header(&text), pack.len(), packed.len()).unwrap();
pack.extend(packed);
}
index.push_str("];\n");
std::fs::write(Path::new(&out).join("syntax.pack"), pack).unwrap();
std::fs::write(Path::new(&out).join("syntax_index.rs"), index).unwrap();
}