Skip to content

Commit dfd2766

Browse files
calebcartwrighttopecongiro
authored andcommitted
add json emit mode (#3735)
1 parent 541d9a8 commit dfd2766

17 files changed

+458
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ needs to be specified in `rustfmt.toml`, e.g., with `edition = "2018"`.
220220
| stdout | writes output to stdout | No |
221221
| coverage | displays how much of the input file was processed | Yes |
222222
| checkstyle | emits in a checkstyle format | Yes |
223+
| json | emits diffs in a json format | Yes |
223224

224225
## License
225226

src/bin/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn make_opts() -> Options {
9898
);
9999
let is_nightly = is_nightly();
100100
let emit_opts = if is_nightly {
101-
"[files|stdout|coverage|checkstyle]"
101+
"[files|stdout|coverage|checkstyle|json]"
102102
} else {
103103
"[files|stdout]"
104104
};
@@ -631,6 +631,7 @@ fn emit_mode_from_emit_str(emit_str: &str) -> Result<EmitMode, FailureError> {
631631
"stdout" => Ok(EmitMode::Stdout),
632632
"coverage" => Ok(EmitMode::Coverage),
633633
"checkstyle" => Ok(EmitMode::Checkstyle),
634+
"json" => Ok(EmitMode::Json),
634635
_ => Err(format_err!("Invalid value for `--emit`")),
635636
}
636637
}

src/config/options.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ pub enum EmitMode {
119119
Coverage,
120120
/// Unfancy stdout
121121
Checkstyle,
122+
/// Writes the resulting diffs in a JSON format. Returns an empty array
123+
/// `[]` if there were no diffs.
124+
Json,
122125
/// Output the changed lines (for internal value only)
123126
ModifiedLines,
124127
/// Checks if a diff can be generated. If so, rustfmt outputs a diff and

src/emitter.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub(crate) use self::checkstyle::*;
22
pub(crate) use self::diff::*;
33
pub(crate) use self::files::*;
44
pub(crate) use self::files_with_backup::*;
5+
pub(crate) use self::json::*;
56
pub(crate) use self::modified_lines::*;
67
pub(crate) use self::stdout::*;
78
use crate::FileName;
@@ -12,6 +13,7 @@ mod checkstyle;
1213
mod diff;
1314
mod files;
1415
mod files_with_backup;
16+
mod json;
1517
mod modified_lines;
1618
mod stdout;
1719

@@ -28,7 +30,7 @@ pub(crate) struct EmitterResult {
2830

2931
pub(crate) trait Emitter {
3032
fn emit_formatted_file(
31-
&self,
33+
&mut self,
3234
output: &mut dyn Write,
3335
formatted_file: FormattedFile<'_>,
3436
) -> Result<EmitterResult, io::Error>;

src/emitter/checkstyle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Emitter for CheckstyleEmitter {
2121
}
2222

2323
fn emit_formatted_file(
24-
&self,
24+
&mut self,
2525
output: &mut dyn Write,
2626
FormattedFile {
2727
filename,

src/emitter/diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl DiffEmitter {
1414

1515
impl Emitter for DiffEmitter {
1616
fn emit_formatted_file(
17-
&self,
17+
&mut self,
1818
_output: &mut dyn Write,
1919
FormattedFile {
2020
filename,

src/emitter/files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub(crate) struct FilesEmitter;
66

77
impl Emitter for FilesEmitter {
88
fn emit_formatted_file(
9-
&self,
9+
&mut self,
1010
_output: &mut dyn Write,
1111
FormattedFile {
1212
filename,

src/emitter/files_with_backup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub(crate) struct FilesWithBackupEmitter;
66

77
impl Emitter for FilesWithBackupEmitter {
88
fn emit_formatted_file(
9-
&self,
9+
&mut self,
1010
_output: &mut dyn Write,
1111
FormattedFile {
1212
filename,

0 commit comments

Comments
 (0)