//! 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 `` tag, which holds everything needed to choose a definition. fn header(text: &str) -> &str { let Some(start) = text.find(" 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(); }