Skip to content

Commit 5d9f5aa

Browse files
committed
Replace --write-mode with --emit
cc #1976
1 parent 8396da1 commit 5d9f5aa

File tree

7 files changed

+32
-76
lines changed

7 files changed

+32
-76
lines changed

README.md

+6-45
Original file line numberDiff line numberDiff line change
@@ -105,53 +105,14 @@ just need to run on the root file (usually mod.rs or lib.rs). Rustfmt can also
105105
read data from stdin. Alternatively, you can use `cargo fmt` to format all
106106
binary and library targets of your crate.
107107

108-
You'll probably want to specify the write mode. Currently, there are modes for
109-
`check`, `diff`, `replace`, `overwrite`, `display`, `coverage`, `checkstyle`, and `plain`.
110-
111-
* `overwrite` Is the default and overwrites the original files _without_ creating backups.
112-
* `replace` Overwrites the original files after creating backups of the files.
113-
* `display` Will print the formatted files to stdout.
114-
* `plain` Also writes to stdout, but with no metadata.
115-
* `diff` Will print a diff between the original files and formatted files to stdout.
116-
* `check` Checks if the program's formatting matches what rustfmt would do. Silently exits
117-
with code 0 if so, emits a diff and exits with code 1 if not. This option is
118-
designed to be run in CI-like where a non-zero exit signifies incorrect formatting.
119-
* `checkstyle` Will output the lines that need to be corrected as a checkstyle XML file,
120-
that can be used by tools like Jenkins.
121-
122-
The write mode can be set by passing the `--write-mode` flag on
123-
the command line. For example `rustfmt --write-mode=display src/filename.rs`
124-
125-
`cargo fmt` uses `--write-mode=overwrite` by default.
126-
127-
If you want to restrict reformatting to specific sets of lines, you can
128-
use the `--file-lines` option. Its argument is a JSON array of objects
129-
with `file` and `range` properties, where `file` is a file name, and
130-
`range` is an array representing a range of lines like `[7,13]`. Ranges
131-
are 1-based and inclusive of both end points. Specifying an empty array
132-
will result in no files being formatted. For example,
108+
You can run `rustfmt --help` for information about argument.
133109

134-
```
135-
rustfmt --file-lines '[
136-
{"file":"src/lib.rs","range":[7,13]},
137-
{"file":"src/lib.rs","range":[21,29]},
138-
{"file":"src/foo.rs","range":[10,11]},
139-
{"file":"src/foo.rs","range":[15,15]}]'
140-
```
141-
142-
would format lines `7-13` and `21-29` of `src/lib.rs`, and lines `10-11`,
143-
and `15` of `src/foo.rs`. No other files would be formatted, even if they
144-
are included as out of line modules from `src/lib.rs`.
145-
146-
If `rustfmt` successfully reformatted the code it will exit with `0` exit
147-
status. Exit status `1` signals some unexpected error, like an unknown option or
148-
a failure to read a file. Exit status `2` is returned if there are syntax errors
149-
in the input files. `rustfmt` can't format syntactically invalid code. Finally,
150-
exit status `3` is returned if there are some issues which can't be resolved
151-
automatically. For example, if you have a very long comment line `rustfmt`
152-
doesn't split it. Instead it prints a warning and exits with `3`.
110+
When running with `--check`, Rustfmt will exit with `0` if Rustfmt would not
111+
make any formatting changes to the input, and `1` if Rustfmt would make changes.
112+
In other modes, Rustfmt will exit with `1` if there was some error during
113+
formatting (for example a parsing or internal error) and `0` if formatting
114+
completed without error (whether or not changes were made).
153115

154-
You can run `rustfmt --help` for more information.
155116

156117

157118
## Running Rustfmt from your editor

bootstrap.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
cargo build --release
88

9-
target/release/rustfmt --write-mode=overwrite src/lib.rs
10-
target/release/rustfmt --write-mode=overwrite src/bin/main.rs
11-
target/release/rustfmt --write-mode=overwrite src/cargo-fmt/main.rs
9+
target/release/rustfmt src/lib.rs
10+
target/release/rustfmt src/bin/main.rs
11+
target/release/rustfmt src/cargo-fmt/main.rs
1212

1313
for filename in tests/target/*.rs; do
1414
if ! grep -q "rustfmt-" "$filename"; then
15-
target/release/rustfmt --write-mode=overwrite $filename
15+
target/release/rustfmt $filename
1616
fi
1717
done

src/bin/main.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ fn make_opts() -> Options {
111111
found reverts to the input file path",
112112
"[Path for the configuration file]",
113113
);
114+
opts.optopt("", "emit", "What data to emit and how", WRITE_MODE_LIST);
114115
opts.optflag(
115116
"",
116117
"error-on-unformatted",
@@ -120,13 +121,13 @@ fn make_opts() -> Options {
120121
opts.optopt(
121122
"",
122123
"file-lines",
123-
"Format specified line ranges. See README for more detail on the JSON format.",
124+
"Format specified line ranges. Run with `--help file-lines` for more detail.",
124125
"JSON",
125126
);
126127
opts.optflagopt(
127128
"h",
128129
"help",
129-
"Show this message or help about a specific topic: config or file-lines",
130+
"Show this message or help about a specific topic: `config` or `file-lines`",
130131
"=TOPIC",
131132
);
132133
opts.optopt(
@@ -145,12 +146,6 @@ fn make_opts() -> Options {
145146
opts.optflag("v", "verbose", "Print verbose output");
146147
opts.optflag("q", "quiet", "Print less output");
147148
opts.optflag("V", "version", "Show version information");
148-
opts.optopt(
149-
"",
150-
"write-mode",
151-
"How to write output (not usable when piping from stdin)",
152-
WRITE_MODE_LIST,
153-
);
154149

155150
opts
156151
}

src/config/options.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -363,20 +363,20 @@ impl CliOptions {
363363
options.config_path = matches.opt_str("config-path").map(PathBuf::from);
364364

365365
options.check = matches.opt_present("check");
366-
if let Some(ref write_mode) = matches.opt_str("write-mode") {
366+
if let Some(ref emit_str) = matches.opt_str("emit") {
367367
if options.check {
368-
return Err(format_err!("Invalid to set write-mode and `--check`"));
368+
return Err(format_err!("Invalid to use `--emit` and `--check`"));
369369
}
370-
if let Ok(write_mode) = WriteMode::from_str(write_mode) {
370+
if let Ok(write_mode) = write_mode_from_emit_str(emit_str) {
371371
if write_mode == WriteMode::Overwrite && matches.opt_present("backup") {
372372
options.write_mode = Some(WriteMode::Replace);
373373
} else {
374374
options.write_mode = Some(write_mode);
375375
}
376376
} else {
377377
return Err(format_err!(
378-
"Invalid write-mode: {}, expected one of {}",
379-
write_mode,
378+
"Invalid value for `--emit`: {}, expected one of {}",
379+
emit_str,
380380
WRITE_MODE_LIST
381381
));
382382
}
@@ -441,3 +441,13 @@ impl CliOptions {
441441
}
442442
}
443443
}
444+
445+
fn write_mode_from_emit_str(emit_str: &str) -> FmtResult<WriteMode> {
446+
match emit_str {
447+
"files" => Ok(WriteMode::Overwrite),
448+
"stdout" => Ok(WriteMode::Display),
449+
"coverage" => Ok(WriteMode::Coverage),
450+
"checkstyle" => Ok(WriteMode::Checkstyle),
451+
_ => Err(format_err!("Invalid value for `--emit`")),
452+
}
453+
}

src/config/summary.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct Summary {
2323
// Code is valid, but it is impossible to format it properly.
2424
has_formatting_errors: bool,
2525

26-
// Formatted code differs from existing code (write-mode diff only).
26+
// Formatted code differs from existing code (--check only).
2727
pub has_diff: bool,
2828

2929
// Keeps track of time spent in parsing and formatting steps.

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ pub use config::{file_lines, load_config, Config, Verbosity, WriteMode};
6868

6969
pub type FmtResult<T> = std::result::Result<T, failure::Error>;
7070

71-
pub const WRITE_MODE_LIST: &str = "[overwrite|display|plain|diff|coverage|checkstyle]";
71+
// FIXME: this is badly named since the user-facing name is `emit` not `write-mode`.
72+
pub const WRITE_MODE_LIST: &str = "[files|stdout|coverage|checkstyle]";
7273

7374
#[macro_use]
7475
mod utils;

src/test/mod.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -912,18 +912,7 @@ fn verify_check_works() {
912912
let temp_file = make_temp_file("temp_check.rs");
913913
assert_cli::Assert::command(&[
914914
rustfmt().to_str().unwrap(),
915-
"--write-mode=check",
916-
temp_file.path.to_str().unwrap(),
917-
]).succeeds()
918-
.unwrap();
919-
}
920-
921-
#[test]
922-
fn verify_diff_works() {
923-
let temp_file = make_temp_file("temp_diff.rs");
924-
assert_cli::Assert::command(&[
925-
rustfmt().to_str().unwrap(),
926-
"--write-mode=diff",
915+
"--check",
927916
temp_file.path.to_str().unwrap(),
928917
]).succeeds()
929918
.unwrap();

0 commit comments

Comments
 (0)