X-Git-Url: https://git.libre-soc.org/?p=vector-math.git;a=blobdiff_plain;f=build.rs;fp=build.rs;h=d0bd64d4187ad5040c494905468e6ad563897719;hp=33d01bc2f4e737d07efebf2524507334cc86c7fc;hb=60f7e09b17d800a3e405a91cac7be7db05cacdd6;hpb=b1207f3114028406f12d35f7511a52dbfebdb3ad diff --git a/build.rs b/build.rs index 33d01bc..d0bd64d 100644 --- a/build.rs +++ b/build.rs @@ -8,23 +8,39 @@ use std::{ use vector_math_build_helpers::make_context_types; fn format_source(source: String) -> String { - let rustfmt_path = which::which("rustfmt").unwrap(); - let mut command = Command::new(rustfmt_path) + match try_format_source(&source) { + Ok(v) => v, + Err(e) => { + eprintln!("rustfmt failed: {}", e); + source + } + } +} + +fn try_format_source(source: &str) -> io::Result { + let mut command = Command::new("rustfmt") .stdin(Stdio::piped()) .stdout(Stdio::piped()) - .spawn() - .unwrap(); + .spawn()?; let stdin = command.stdin.take().unwrap(); let reader_thread = thread::spawn(move || -> io::Result<(String, Child)> { let mut output = String::new(); command.stdout.take().unwrap().read_to_string(&mut output)?; Ok((output, command)) }); - { stdin }.write_all(source.as_bytes()).unwrap(); - let (output, mut command) = reader_thread.join().unwrap().unwrap(); - let exit_status = command.wait().unwrap(); - assert!(exit_status.success()); - output + let write_result = { stdin }.write_all(source.as_bytes()); + let join_result = reader_thread.join().unwrap(); + write_result?; + let (output, mut command) = join_result?; + let exit_status = command.wait()?; + if exit_status.success() { + Ok(output) + } else { + Err(io::Error::new( + io::ErrorKind::Other, + format!("exited with status {}", exit_status), + )) + } } fn main() {