Skip to content

Commit 9754bcb

Browse files
authored
Merge pull request #2048 from japaric/required-version
add `required-version` option to rustfmt.toml
2 parents 560b054 + 77584e5 commit 9754bcb

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/bin/rustfmt.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,12 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
201201
}
202202
}
203203

204-
Ok(run(Input::Text(input), &config))
204+
let mut error_summary = Summary::default();
205+
if config.version_meets_requirement(&mut error_summary) {
206+
error_summary.add(run(Input::Text(input), &config));
207+
}
208+
209+
Ok(error_summary)
205210
}
206211
Operation::Format {
207212
files,
@@ -253,6 +258,10 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
253258
config = config_tmp;
254259
}
255260

261+
if !config.version_meets_requirement(&mut error_summary) {
262+
break;
263+
}
264+
256265
options.clone().apply_to(&mut config);
257266
error_summary.add(run(Input::File(file), &config));
258267
}

src/config.rs

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::path::{Path, PathBuf};
1818

1919
use file_lines::FileLines;
2020
use lists::{ListTactic, SeparatorPlace, SeparatorTactic};
21+
use Summary;
2122

2223
macro_rules! configuration_option_enum{
2324
($e:ident: $( $x:ident ),+ $(,)*) => {
@@ -272,6 +273,23 @@ macro_rules! create_config {
272273
}
273274

274275
impl Config {
276+
pub fn version_meets_requirement(&self, error_summary: &mut Summary) -> bool {
277+
if self.was_set().required_version() {
278+
let version = env!("CARGO_PKG_VERSION");
279+
let required_version = self.required_version();
280+
if version != required_version {
281+
println!(
282+
"Error: rustfmt version ({}) doesn't match the required version ({})",
283+
version,
284+
required_version,
285+
);
286+
error_summary.add_formatting_error();
287+
return false;
288+
}
289+
}
290+
291+
true
292+
}
275293

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

627647
#[cfg(test)]

0 commit comments

Comments
 (0)