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<String> {
+ 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() {