Skip to content

Commit 2b258f7

Browse files
committed
Add rough draft of check mode. Not unit tested.
1 parent 87180d9 commit 2b258f7

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/bin/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ fn make_opts() -> Options {
198198
"",
199199
"write-mode",
200200
"How to write output (not usable when piping from stdin)",
201-
"[replace|overwrite|display|plain|diff|coverage|checkstyle]",
201+
"[replace|overwrite|display|plain|diff|check|coverage|checkstyle]",
202202
);
203203

204204
opts
@@ -287,6 +287,10 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
287287
}
288288

289289
let mut error_summary = Summary::default();
290+
if let Some(mode) = options.write_mode {
291+
error_summary.add_write_mode(mode)
292+
}
293+
290294
for file in files {
291295
if !file.exists() {
292296
eprintln!("Error: file `{}` does not exist", file.to_str().unwrap());
@@ -342,6 +346,8 @@ fn main() {
342346
Ok(summary) => {
343347
if summary.has_operational_errors() {
344348
1
349+
} else if summary.has_diff && summary.write_mode == Some(WriteMode::Check) {
350+
1
345351
} else if summary.has_parsing_errors() {
346352
2
347353
} else if summary.has_formatting_errors() {

src/config/options.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ configuration_option_enum! { WriteMode:
183183
Checkstyle,
184184
// Output the changed lines (for internal value only)
185185
Modified,
186+
// Displays how much of the input was processed, but if anything was modified, rustfmt quits
187+
// with exit code 0. This option is intended to be run as part of a CI pipeline.
188+
Check,
186189
}
187190

188191
configuration_option_enum! { Color:

src/config/summary.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use std::time::{Duration, Instant};
1212
use std::default::Default;
1313

14+
use config::options::WriteMode;
15+
1416
#[must_use]
1517
#[derive(Debug, Default, Clone, Copy)]
1618
pub struct Summary {
@@ -26,6 +28,9 @@ pub struct Summary {
2628
// Formatted code differs from existing code (write-mode diff only).
2729
pub has_diff: bool,
2830

31+
// What write mode rustfmt was invoked with, if any.
32+
pub write_mode: Option<WriteMode>,
33+
2934
// Keeps track of time spent in parsing and formatting steps.
3035
timer: Timer,
3136
}
@@ -89,6 +94,10 @@ impl Summary {
8994
self.has_diff = true;
9095
}
9196

97+
pub fn add_write_mode(&mut self, mode: WriteMode) {
98+
self.write_mode = Some(mode)
99+
}
100+
92101
pub fn has_no_errors(&self) -> bool {
93102
!(self.has_operational_errors || self.has_parsing_errors || self.has_formatting_errors
94103
|| self.has_diff)

src/filemap.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ where
178178
let diff = create_diff(filename, text, config)?;
179179
output_checkstyle_file(out, filename, diff)?;
180180
}
181+
WriteMode::Check => {
182+
let filename = filename_to_path();
183+
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
184+
let mismatch = make_diff(&ori, &fmt, 3);
185+
let has_diff = !mismatch.is_empty();
186+
print_diff(
187+
mismatch,
188+
|line_num| format!("Diff in {} at line {}:", filename.display(), line_num),
189+
config.color(),
190+
);
191+
return Ok(has_diff);
192+
}
193+
}
181194
}
182195

183196
// when we are not in diff mode, don't indicate differing files

0 commit comments

Comments
 (0)