-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.rs
82 lines (67 loc) · 2.5 KB
/
build.rs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use serde::Deserialize;
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::Path;
#[derive(Deserialize)]
struct Language {
#[serde(default)]
r#_type: Option<String>,
#[serde(default)]
extensions: Vec<String>,
#[serde(default)]
filenames: Vec<String>,
#[serde(default)]
interpreters: Vec<String>,
#[serde(default)]
_language_id: Option<i32>,
}
fn main() {
println!("cargo:rerun-if-changed=languages.yml");
// Read and parse languages.yml
let yaml_content =
std::fs::read_to_string("languages.yml").expect("Failed to read languages.yml");
let languages: HashMap<String, Language> =
serde_yaml::from_str(&yaml_content).expect("Failed to parse languages.yml");
// Generate the rust code
let mut code = String::new();
// Add the use statements
code.push_str("use once_cell::sync::Lazy;\n");
code.push_str("use std::collections::HashSet;\n\n");
// Generate source extensions set
code.push_str("pub static SOURCE_EXTENSIONS: Lazy<HashSet<&'static str>> = Lazy::new(|| {\n");
code.push_str(" let mut set = HashSet::new();\n\n");
for (_, lang) in &languages {
for ext in &lang.extensions {
let ext = ext.trim_start_matches('.');
code.push_str(&format!(" set.insert(\"{}\");\n", ext));
}
}
code.push_str(" set\n");
code.push_str("});\n\n");
// Generate filename mappings
code.push_str("pub static KNOWN_FILENAMES: Lazy<HashSet<&'static str>> = Lazy::new(|| {\n");
code.push_str(" let mut set = HashSet::new();\n\n");
for (_, lang) in &languages {
for filename in &lang.filenames {
code.push_str(&format!(" set.insert(\"{}\");\n", filename));
}
}
code.push_str(" set\n");
code.push_str("});\n\n");
// Generate interpreter mappings
code.push_str("pub static INTERPRETER_NAMES: Lazy<HashSet<&'static str>> = Lazy::new(|| {\n");
code.push_str(" let mut set = HashSet::new();\n\n");
for (_, lang) in &languages {
for interpreter in &lang.interpreters {
code.push_str(&format!(" set.insert(\"{}\");\n", interpreter));
}
}
code.push_str(" set\n");
code.push_str("});\n");
// Write the generated code to file
let out_dir = std::env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("languages.rs");
let mut f = File::create(dest_path).unwrap();
f.write_all(code.as_bytes()).unwrap();
}