Skip to content

Commit 90e1264

Browse files
committed
refactor configs and stdin
1 parent aba3ce4 commit 90e1264

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

check_diff/src/lib.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use diffy;
22
use std::env;
33
use std::fmt::Debug;
4-
use std::io;
4+
use std::io::{self, Write};
55
use std::path::{Path, PathBuf};
6-
use std::process::Command;
6+
use std::process::{Command, Stdio};
77
use std::str::Utf8Error;
88
use tracing::info;
99
use walkdir::WalkDir;
@@ -115,39 +115,46 @@ impl RustfmtRunner {
115115
code: &'a str,
116116
config: &Option<Vec<String>>,
117117
) -> Result<String, CheckDiffError> {
118-
let config_arg: String = match config {
119-
Some(configs) => {
120-
let mut result = String::new();
121-
result.push(',');
122-
for arg in configs.iter() {
123-
result.push_str(arg.as_str());
124-
result.push(',');
125-
}
126-
result.pop();
127-
result
128-
}
129-
None => String::new(),
130-
};
131-
let config = format!(
132-
"error_on_line_overflow=false,error_on_unformatted=false{}",
133-
config_arg.as_str()
134-
);
135-
136-
let output = Command::new(&self.binary_path)
118+
let config = create_config_arg(config);
119+
let mut command = Command::new(&self.binary_path)
137120
.env("LD_LIBRARY_PATH", &self.ld_library_path)
138121
.args([
139122
"--unstable-features",
140123
"--skip-children",
141124
"--emit=stdout",
142125
config.as_str(),
143-
code,
144126
])
145-
.output()?;
127+
.stdin(Stdio::piped())
128+
.stdout(Stdio::piped())
129+
.stderr(Stdio::piped())
130+
.spawn()?;
146131

132+
command.stdin.as_mut().unwrap().write_all(code.as_bytes())?;
133+
let output = command.wait_with_output()?;
147134
Ok(std::str::from_utf8(&output.stdout)?.to_string())
148135
}
149136
}
150137

138+
fn create_config_arg(config: &Option<Vec<String>>) -> String {
139+
let config_arg: String = match config {
140+
Some(configs) => {
141+
let mut result = String::new();
142+
result.push(',');
143+
for arg in configs.iter() {
144+
result.push_str(arg.as_str());
145+
result.push(',');
146+
}
147+
result.pop();
148+
result
149+
}
150+
None => String::new(),
151+
};
152+
let config = format!(
153+
"error_on_line_overflow=false,error_on_unformatted=false{}",
154+
config_arg.as_str()
155+
);
156+
config
157+
}
151158
/// Clone a git repository
152159
///
153160
/// Parameters:

0 commit comments

Comments
 (0)