use fusevm::RustSugar;
fn emit(b64: &str, line: usize) -> String {
format!("BEGIN {{ __rust_compile(\"{b64}\", {line}) }}")
}
pub const SUGAR: RustSugar = RustSugar {
keyword: "rust",
line_comments: &["#"],
block_comment: None,
newline_boundary: true,
emit,
};
pub fn desugar(src: &str) -> String {
SUGAR.desugar(src)
}
#[cfg(test)]
mod tests {
#[test]
fn desugars_top_level_rust_block_to_begin_rule() {
let src = "rust { pub extern \"C\" fn add(a: i64, b: i64) -> i64 { a + b } }\nBEGIN { print add(2, 3) }\n";
let out = super::desugar(src);
assert!(out.contains("BEGIN {"), "no BEGIN wrap: {out}");
assert!(out.contains("__rust_compile("), "no builtin call: {out}");
assert!(!out.contains("pub extern"), "Rust body leaked: {out}");
assert!(out.contains("print add(2, 3)"), "user rule dropped: {out}");
}
#[test]
fn leaves_ordinary_awk_untouched() {
let src = "BEGIN { print length(\"hi\") }\n";
assert_eq!(super::desugar(src), src);
}
#[test]
fn hash_comment_is_not_a_false_boundary() {
let src = "# a rust { } comment\nBEGIN { print 1 }\n";
assert_eq!(super::desugar(src), src);
}
}