Skip to content

Commit 0c36c59

Browse files
committed
add required-version option to rustfmt.toml
This option specifies the rustfmt version that *must* be used to format the code. Trying to use a different version raises an error. closes #1505
1 parent 6c9ee31 commit 0c36c59

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/bin/rustfmt.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,13 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
195195
}
196196
}
197197

198-
Ok(run(Input::Text(input), &config))
198+
let mut error_summary = Summary::default();
199+
if config.version_meets_requirement(&mut error_summary) {
200+
error_summary.add(run(Input::Text(input), &config));
201+
}
202+
203+
Ok(error_summary)
204+
199205
}
200206
Operation::Format {
201207
files,
@@ -247,6 +253,10 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
247253
config = config_tmp;
248254
}
249255

256+
if !config.version_meets_requirement(&mut error_summary) {
257+
break
258+
}
259+
250260
options.clone().apply_to(&mut config);
251261
error_summary.add(run(Input::File(file), &config));
252262
}

src/config.rs

Lines changed: 20 additions & 0 deletions
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, "".to_owned(),
644+
"Require a specific version of rustfmt."
625645
}
626646

627647
#[cfg(test)]

0 commit comments

Comments
 (0)