convert proc-macro for generating Context trait to a build script in hopes of getting...
[vector-math.git] / build.rs
1 use std::{
2 env, fs,
3 io::{self, Read, Write},
4 path::Path,
5 process::{Child, Command, Stdio},
6 thread,
7 };
8 use vector_math_build_helpers::make_context_types;
9
10 fn format_source(source: String) -> String {
11 let rustfmt_path = which::which("rustfmt").unwrap();
12 let mut command = Command::new(rustfmt_path)
13 .stdin(Stdio::piped())
14 .stdout(Stdio::piped())
15 .spawn()
16 .unwrap();
17 let stdin = command.stdin.take().unwrap();
18 let reader_thread = thread::spawn(move || -> io::Result<(String, Child)> {
19 let mut output = String::new();
20 command.stdout.take().unwrap().read_to_string(&mut output)?;
21 Ok((output, command))
22 });
23 { stdin }.write_all(source.as_bytes()).unwrap();
24 let (output, mut command) = reader_thread.join().unwrap().unwrap();
25 let exit_status = command.wait().unwrap();
26 assert!(exit_status.success());
27 output
28 }
29
30 fn main() {
31 fs::write(
32 Path::new(&env::var_os("OUT_DIR").unwrap()).join("context_trait.rs"),
33 format_source(make_context_types().to_string()),
34 )
35 .unwrap();
36 }