1use std::{io::Write, process::Command};
2
3pub fn format_cpp(code: &str) -> Result<String, std::io::Error> {
5 let mut child = Command::new("clang-format")
6 .stdin(std::process::Stdio::piped())
7 .stdout(std::process::Stdio::piped())
8 .spawn()?;
9
10 {
11 let stdin = child.stdin.as_mut().expect("Failed to open stdin");
12 stdin.write_all(code.as_bytes())?;
13 }
14
15 let output = child.wait_with_output()?;
16
17 if output.status.success() {
18 Ok(String::from_utf8_lossy(&output.stdout).into_owned())
19 } else {
20 Err(std::io::Error::other("clang-format failed"))
21 }
22}