-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
41 lines (31 loc) · 1.43 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
use std::{io, path};
static AUTOMATON_TEMPLATE: &str = include_str!("src/parse/_templates/automaton_template.rs");
/// Generate a automaton template.
fn generate_automaton_template(source: path::PathBuf, output: path::PathBuf) -> io::Result<()> {
let name = source.file_stem().unwrap().to_string_lossy().to_string();
let string = std::fs::read_to_string(&source)?;
let words = string.split(char::is_whitespace);
let template = AUTOMATON_TEMPLATE
.replace("\"{{WORD_LIST}}\"", {
&words.fold(String::new(), |text, word| {
text + "\"" + word + "\",\n "
})
})
.replace("{{LIST_NAME}}", &name);
std::fs::write(output, template)?;
Ok(())
}
// Example custom build script.
fn main() -> io::Result<()> {
// Tell Cargo that if the english words changes, to rerun this build script.
println!("cargo::rerun-if-changed=data/words/en_common_words.csv");
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=src/parse/_templates/automaton_template.rs");
let out_dir = std::env::var("OUT_DIR").expect(
"Failed to get the output directory. Please make sure that the environment variable `OUT_DIR` is set.",
);
let source = path::PathBuf::from("data/words/en_common_words.csv");
let output = path::PathBuf::from(out_dir).join("en_common_words.rs");
generate_automaton_template(source, output)?;
Ok(())
}