2019-08-20 00:04:25 +02:00
|
|
|
use walkdir::WalkDir;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::{env, fs};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// The src directory (we are in src/tools/error_index_generator)
|
|
|
|
// Note that we could skip one of the .. but this ensures we at least loosely find the right
|
|
|
|
// directory.
|
|
|
|
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
|
|
let dest = out_dir.join("error_codes.rs");
|
|
|
|
let mut idx = 0;
|
|
|
|
for entry in WalkDir::new("../../../src") {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
if entry.file_name() == "error_codes.rs" {
|
|
|
|
println!("cargo:rerun-if-changed={}", entry.path().to_str().unwrap());
|
|
|
|
let file = fs::read_to_string(entry.path()).unwrap()
|
2019-09-04 04:04:01 +02:00
|
|
|
.replace("syntax::register_diagnostics!", "register_diagnostics!");
|
2019-09-08 16:00:26 +02:00
|
|
|
let contents = format!("(|| {{\n{}\n}})()", file);
|
2019-08-20 00:04:25 +02:00
|
|
|
|
|
|
|
fs::write(&out_dir.join(&format!("error_{}.rs", idx)), &contents).unwrap();
|
|
|
|
|
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut all = String::new();
|
2019-09-04 04:04:01 +02:00
|
|
|
all.push_str(r###"
|
|
|
|
fn register_all() -> Vec<(&'static str, Option<&'static str>)> {
|
|
|
|
let mut long_codes: Vec<(&'static str, Option<&'static str>)> = Vec::new();
|
|
|
|
macro_rules! register_diagnostics {
|
|
|
|
($($ecode:ident: $message:expr,)*) => (
|
|
|
|
register_diagnostics!{$($ecode:$message,)* ;}
|
|
|
|
);
|
2019-08-20 00:04:25 +02:00
|
|
|
|
2019-09-04 04:04:01 +02:00
|
|
|
($($ecode:ident: $message:expr,)* ; $($code:ident,)*) => (
|
|
|
|
$(
|
|
|
|
{long_codes.extend([
|
2019-09-13 12:37:29 +02:00
|
|
|
(stringify!($ecode), Some($message)),
|
2019-09-04 04:04:01 +02:00
|
|
|
].iter());}
|
|
|
|
)*
|
|
|
|
$(
|
|
|
|
{long_codes.extend([
|
|
|
|
stringify!($code),
|
|
|
|
].iter().cloned().map(|s| (s, None)).collect::<Vec<_>>());}
|
|
|
|
)*
|
|
|
|
)
|
2019-08-20 00:04:25 +02:00
|
|
|
}
|
2019-09-04 04:04:01 +02:00
|
|
|
"###);
|
2019-08-20 00:04:25 +02:00
|
|
|
for idx in 0..idx {
|
|
|
|
all.push_str(&format!(r#"include!(concat!(env!("OUT_DIR"), "/error_{}.rs"));"#, idx));
|
2019-09-04 04:04:01 +02:00
|
|
|
all.push_str("\n");
|
2019-08-20 00:04:25 +02:00
|
|
|
}
|
|
|
|
all.push_str("\nlong_codes\n");
|
|
|
|
all.push_str("}\n");
|
|
|
|
|
|
|
|
fs::write(&dest, all).unwrap();
|
|
|
|
}
|