1use std::{
2 io::Write,
3 process::{Command, Stdio},
4};
5
6use crate::error::Error;
7
8pub fn format_source(src: &str) -> Result<String, Error> {
10 let rustfmt = {
11 let mut proc = Command::new(&"rustfmt")
12 .stdin(Stdio::piped())
13 .stdout(Stdio::piped())
14 .spawn()?;
15 let mut stdin = proc.stdin.take().unwrap();
16 stdin.write_all(src.as_bytes())?;
17 proc
18 };
19
20 let stdout = rustfmt.wait_with_output()?;
21
22 let src = String::from_utf8(stdout.stdout)?;
23
24 Ok(src)
25}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30
31 #[test]
32 fn test_format_source_works() {
33 let src = format_source("pub fn add1(x : i32) { x + 1}").unwrap();
34
35 assert!(!src.is_empty());
36 }
37}