Skip to content

add json emit mode #3735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ needs to be specified in `rustfmt.toml`, e.g., with `edition = "2018"`.
| stdout | writes output to stdout | No |
| coverage | displays how much of the input file was processed | Yes |
| checkstyle | emits in a checkstyle format | Yes |
| json | emits diffs in a json format | Yes |

## License

Expand Down
3 changes: 2 additions & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn make_opts() -> Options {
);
let is_nightly = is_nightly();
let emit_opts = if is_nightly {
"[files|stdout|coverage|checkstyle]"
"[files|stdout|coverage|checkstyle|json]"
} else {
"[files|stdout]"
};
Expand Down Expand Up @@ -631,6 +631,7 @@ fn emit_mode_from_emit_str(emit_str: &str) -> Result<EmitMode, FailureError> {
"stdout" => Ok(EmitMode::Stdout),
"coverage" => Ok(EmitMode::Coverage),
"checkstyle" => Ok(EmitMode::Checkstyle),
"json" => Ok(EmitMode::Json),
_ => Err(format_err!("Invalid value for `--emit`")),
}
}
3 changes: 3 additions & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ pub enum EmitMode {
Coverage,
/// Unfancy stdout
Checkstyle,
/// Writes the resulting diffs in a JSON format. Returns an empty array
/// `[]` if there were no diffs.
Json,
/// Output the changed lines (for internal value only)
ModifiedLines,
/// Checks if a diff can be generated. If so, rustfmt outputs a diff and
Expand Down
4 changes: 3 additions & 1 deletion src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub(crate) use self::checkstyle::*;
pub(crate) use self::diff::*;
pub(crate) use self::files::*;
pub(crate) use self::files_with_backup::*;
pub(crate) use self::json::*;
pub(crate) use self::modified_lines::*;
pub(crate) use self::stdout::*;
use crate::FileName;
Expand All @@ -12,6 +13,7 @@ mod checkstyle;
mod diff;
mod files;
mod files_with_backup;
mod json;
mod modified_lines;
mod stdout;

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

pub(crate) trait Emitter {
fn emit_formatted_file(
&self,
&mut self,
output: &mut dyn Write,
formatted_file: FormattedFile<'_>,
) -> Result<EmitterResult, io::Error>;
Expand Down
2 changes: 1 addition & 1 deletion src/emitter/checkstyle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Emitter for CheckstyleEmitter {
}

fn emit_formatted_file(
&self,
&mut self,
output: &mut dyn Write,
FormattedFile {
filename,
Expand Down
2 changes: 1 addition & 1 deletion src/emitter/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl DiffEmitter {

impl Emitter for DiffEmitter {
fn emit_formatted_file(
&self,
&mut self,
_output: &mut dyn Write,
FormattedFile {
filename,
Expand Down
2 changes: 1 addition & 1 deletion src/emitter/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub(crate) struct FilesEmitter;

impl Emitter for FilesEmitter {
fn emit_formatted_file(
&self,
&mut self,
_output: &mut dyn Write,
FormattedFile {
filename,
Expand Down
2 changes: 1 addition & 1 deletion src/emitter/files_with_backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub(crate) struct FilesWithBackupEmitter;

impl Emitter for FilesWithBackupEmitter {
fn emit_formatted_file(
&self,
&mut self,
_output: &mut dyn Write,
FormattedFile {
filename,
Expand Down
Loading