Skip to content

Commit 1e048de

Browse files
committed
impl check_diff fn
1 parent 613dacb commit 1e048de

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

check_diff/Cargo.lock

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

check_diff/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ clap = { version = "4.4.2", features = ["derive"] }
1010
tracing = "0.1.37"
1111
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
1212
tempfile = "3"
13+
walkdir = "2.5.0"

check_diff/src/lib.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::env;
2+
use std::fs::OpenOptions;
23
use std::io;
34
use std::path::{Path, PathBuf};
45
use std::process::Command;
56
use std::str::Utf8Error;
67
use tracing::info;
8+
use walkdir::WalkDir;
79

810
pub enum CheckDiffError {
911
/// Git related errors
@@ -18,6 +20,9 @@ pub enum CheckDiffError {
1820
FailedBinaryVersioning(PathBuf),
1921
/// Error when obtaining cargo version
2022
FailedCargoVersion(&'static str),
23+
/// Error when trying to find rust files
24+
FailedFindingRustFiles(&'static str),
25+
FailedWritingToFile(&'static str),
2126
IO(std::io::Error),
2227
}
2328

@@ -88,7 +93,12 @@ impl RustfmtRunner {
8893
// output_path: Output file path for the diff
8994
// config: Any additional configuration options to pass to rustfmt
9095
//
91-
fn create_diff(&self, output_path: &Path, config: Option<Vec<String>>) {
96+
#[allow(dead_code)]
97+
fn create_diff(
98+
&self,
99+
output_path: &Path,
100+
config: Option<Vec<String>>,
101+
) -> Result<(), CheckDiffError> {
92102
let config_arg: String = match config {
93103
Some(configs) => {
94104
let mut result = String::new();
@@ -106,6 +116,34 @@ impl RustfmtRunner {
106116
"error_on_line_overflow=false,error_on_unformatted=false{}",
107117
config_arg.as_str()
108118
);
119+
120+
// walks the "." directory and finds all files with .rs extensions
121+
for entry in WalkDir::new(".").into_iter().filter_map(|e| e.ok()) {
122+
let path = entry.path();
123+
if path.is_file() && path.extension().map_or(false, |ext| ext == "rs") {
124+
let file = OpenOptions::new()
125+
.write(true)
126+
.append(true)
127+
.open(output_path)?;
128+
let Ok(_) = Command::new(&self.binary_path)
129+
.env("LD_LIBRARY_PATH", &self.ld_library_path)
130+
.args([
131+
"--unstable-features",
132+
"--skip-children",
133+
"--check",
134+
"--color=always",
135+
config.as_str(),
136+
])
137+
.stdout(file)
138+
.output()
139+
else {
140+
return Err(CheckDiffError::FailedWritingToFile(
141+
"Failed to write to file",
142+
));
143+
};
144+
}
145+
}
146+
Ok(())
109147
}
110148
}
111149

0 commit comments

Comments
 (0)