Skip to content

Commit d7f40ff

Browse files
feat: add --style-edition option to rustfmt binary
1 parent 5438b84 commit d7f40ff

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

Diff for: src/bin/main.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use getopts::{Matches, Options};
1919

2020
use crate::rustfmt::{
2121
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName,
22-
FormatReportFormatterBuilder, Input, Session, Verbosity,
22+
FormatReportFormatterBuilder, Input, Session, StyleEdition, Verbosity,
2323
};
2424

2525
const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rustfmt/issues/new?labels=bug";
@@ -129,7 +129,12 @@ fn make_opts() -> Options {
129129
found reverts to the input file path",
130130
"[Path for the configuration file]",
131131
);
132-
opts.optopt("", "edition", "Rust edition to use", "[2015|2018|2021]");
132+
opts.optopt(
133+
"",
134+
"edition",
135+
"Rust edition to use",
136+
"[2015|2018|2021|2024]",
137+
);
133138
opts.optopt(
134139
"",
135140
"color",
@@ -181,6 +186,12 @@ fn make_opts() -> Options {
181186
"skip-children",
182187
"Don't reformat child modules (unstable).",
183188
);
189+
opts.optopt(
190+
"",
191+
"style-edition",
192+
"The edition of the Style Guide (unstable).",
193+
"[2015|2018|2021|2024]",
194+
);
184195
}
185196

186197
opts.optflag("v", "verbose", "Print verbose output");
@@ -514,6 +525,7 @@ struct GetOptsOptions {
514525
backup: bool,
515526
check: bool,
516527
edition: Option<Edition>,
528+
style_edition: Option<StyleEdition>,
517529
color: Option<Color>,
518530
file_lines: FileLines, // Default is all lines in all files.
519531
unstable_features: bool,
@@ -545,6 +557,10 @@ impl GetOptsOptions {
545557
if let Some(ref file_lines) = matches.opt_str("file-lines") {
546558
options.file_lines = file_lines.parse()?;
547559
}
560+
if let Some(ref edition_str) = matches.opt_str("style-edition") {
561+
options.style_edition =
562+
Some(style_edition_from_style_edition_str(edition_str)?);
563+
}
548564
} else {
549565
let mut unstable_options = vec![];
550566
if matches.opt_present("skip-children") {
@@ -556,6 +572,9 @@ impl GetOptsOptions {
556572
if matches.opt_present("file-lines") {
557573
unstable_options.push("`--file-lines`");
558574
}
575+
if matches.opt_present("style-edition") {
576+
unstable_options.push("`--style-edition`");
577+
}
559578
if !unstable_options.is_empty() {
560579
let s = if unstable_options.len() == 1 { "" } else { "s" };
561580
return Err(format_err!(
@@ -667,6 +686,9 @@ impl CliOptions for GetOptsOptions {
667686
if let Some(edition) = self.edition {
668687
config.set().edition(edition);
669688
}
689+
if let Some(edition) = self.style_edition {
690+
config.set().style_edition(edition);
691+
}
670692
if self.check {
671693
config.set().emit_mode(EmitMode::Diff);
672694
} else if let Some(emit_mode) = self.emit_mode {
@@ -702,6 +724,16 @@ fn edition_from_edition_str(edition_str: &str) -> Result<Edition> {
702724
}
703725
}
704726

727+
fn style_edition_from_style_edition_str(edition_str: &str) -> Result<StyleEdition> {
728+
match edition_str {
729+
"2015" => Ok(StyleEdition::Edition2015),
730+
"2018" => Ok(StyleEdition::Edition2018),
731+
"2021" => Ok(StyleEdition::Edition2021),
732+
"2024" => Ok(StyleEdition::Edition2024),
733+
_ => Err(format_err!("Invalid value for `--style-edition`")),
734+
}
735+
}
736+
705737
fn emit_mode_from_emit_str(emit_str: &str) -> Result<EmitMode> {
706738
match emit_str {
707739
"files" => Ok(EmitMode::Files),

0 commit comments

Comments
 (0)