Skip to content

add required-version option to rustfmt.toml #2048

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 3 commits into from
Oct 20, 2017
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
11 changes: 10 additions & 1 deletion src/bin/rustfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
}
}

Ok(run(Input::Text(input), &config))
let mut error_summary = Summary::default();
if config.version_meets_requirement(&mut error_summary) {
error_summary.add(run(Input::Text(input), &config));
}

Ok(error_summary)
}
Operation::Format {
files,
Expand Down Expand Up @@ -247,6 +252,10 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
config = config_tmp;
}

if !config.version_meets_requirement(&mut error_summary) {
break;
}

options.clone().apply_to(&mut config);
error_summary.add(run(Input::File(file), &config));
}
Expand Down
20 changes: 20 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::path::{Path, PathBuf};

use file_lines::FileLines;
use lists::{ListTactic, SeparatorPlace, SeparatorTactic};
use Summary;

macro_rules! configuration_option_enum{
($e:ident: $( $x:ident ),+ $(,)*) => {
Expand Down Expand Up @@ -272,6 +273,23 @@ macro_rules! create_config {
}

impl Config {
pub fn version_meets_requirement(&self, error_summary: &mut Summary) -> bool {
if self.was_set().required_version() {
let version = env!("CARGO_PKG_VERSION");
let required_version = self.required_version();
if version != required_version {
println!(
"Error: rustfmt version ({}) doesn't match the required version ({})",
version,
required_version,
);
error_summary.add_formatting_error();
return false;
}
}

true
}

$(
pub fn $i(&self) -> $ty {
Expand Down Expand Up @@ -622,6 +640,8 @@ create_config! {
merge_derives: bool, true, "Merge multiple `#[derive(...)]` into a single one";
binop_separator: SeparatorPlace, SeparatorPlace::Front,
"Where to put a binary operator when a binary expression goes multiline.";
required_version: String, env!("CARGO_PKG_VERSION").to_owned(),
"Require a specific version of rustfmt."
}

#[cfg(test)]
Expand Down