-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.rs
70 lines (54 loc) · 1.86 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
extern crate env_logger;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate log;
extern crate tl_codegen;
use std::fs::File;
use std::io::{self, BufRead, BufReader, Read, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
mod error {
error_chain! {
foreign_links {
Io(::std::io::Error);
SetLogger(::log::SetLoggerError);
}
}
}
const TL_SCHEMA_DIR: &'static str = "./tl";
const TL_SCHEMA_LIST_FILE: &'static str = "./tl/tl-schema-list.txt";
const RUST_SCHEMA_FILE: &'static str = "./src/schema.rs";
fn collect_input() -> error::Result<String> {
let mut tl_files = BufReader::new(File::open(TL_SCHEMA_LIST_FILE)?).lines().filter_map(|line| {
match line {
Ok(ref line) if line.starts_with("//") => None, // This line is a comment
Ok(filename) => Some(Ok(Path::new(TL_SCHEMA_DIR).join(filename))),
Err(e) => Some(Err(e)), // Do not ignore errors
}
}).collect::<io::Result<Vec<PathBuf>>>()?;
tl_files.sort();
debug!("Files detected: {:?}", &tl_files);
println!("cargo:rerun-if-changed={}", TL_SCHEMA_LIST_FILE);
let mut input = String::new();
for tl_file in tl_files {
File::open(&tl_file)?.read_to_string(&mut input)?;
println!("cargo:rerun-if-changed={}", tl_file.to_string_lossy());
}
Ok(input)
}
fn run() -> error::Result<()> {
env_logger::init()?;
let input = collect_input()?;
let code = tl_codegen::generate_code_for(&input);
debug!("Code size: {} bytes", code.as_str().len());
File::create(RUST_SCHEMA_FILE)?.write_all(code.as_str().as_bytes())?;
debug!("Successful write to {}", RUST_SCHEMA_FILE);
Command::new("rustfmt")
.arg("--write-mode")
.arg("overwrite")
.arg(RUST_SCHEMA_FILE)
.status()?;
Ok(())
}
quick_main!(run);