1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use regex::{Captures, NoExpand, Regex};
use std::{fs, path::Path};
macro_rules! replace {
($input:ident, $pat:expr, $replacer:expr) => {{
let regex = Regex::new($pat).unwrap();
regex.replace_all(&$input, $replacer)
}};
}
#[allow(clippy::trivial_regex)]
pub fn fix_syntax_and_write(out_path: &Path, contents: String) {
let contents = replace!(contents, r"move\((\w+)\)", "move $1");
let contents = replace!(contents, r"copy\((\w+)\)", "copy $1");
let contents = replace!(contents, r"resource\s+(\w)", "resource struct $1");
let contents = replace!(contents, r":\s*copyable", NoExpand(": copyable"));
let contents = replace!(contents, r"import", NoExpand("use"));
let contents = replace!(contents, r"Self\.", NoExpand(""));
let contents = replace!(contents, r"(([A-Z]\w*)|(\}\})|(0x\d+))\.", "$1::");
let contents = replace!(
contents,
r"(((public|native| )*))(\w+\(.*\).*\{)",
|cap: &Captures| format!("{}fun {}", &cap[1], &cap[4])
);
fs::write(out_path, contents.as_bytes()).unwrap();
}