use fusevm::RustSugar;
fn emit(b64: &str, line: usize) -> String {
format!("__rust_compile(\"{b64}\", {line})")
}
pub const SUGAR: RustSugar = RustSugar {
keyword: "rust",
line_comments: &["//"],
block_comment: Some(("/*", "*/")),
newline_boundary: true,
emit,
};
pub fn desugar(src: &str) -> String {
SUGAR.desugar(src)
}
#[cfg(test)]
mod tests {
#[test]
fn desugars_top_level_block() {
let src =
"rust { pub extern \"C\" fn add(a: i64, b: i64) -> i64 { a + b } }\nconsole.log(add(2, 3))\n";
let out = super::desugar(src);
assert!(out.contains("__rust_compile("), "no builtin call: {out}");
assert!(!out.contains("pub extern"), "Rust body leaked: {out}");
assert!(
out.contains("console.log(add(2, 3))"),
"trailing code lost: {out}"
);
}
#[test]
fn leaves_ordinary_js_untouched() {
let src = "const x = \"hi\".length;\nconsole.log(x);\n";
assert_eq!(super::desugar(src), src);
}
}