Skip to content

Commit b6c89fc

Browse files
feat: add --style-edition option to rustfmt binary
1 parent 7c41e2b commit b6c89fc

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");
@@ -525,6 +536,7 @@ struct GetOptsOptions {
525536
backup: bool,
526537
check: bool,
527538
edition: Option<Edition>,
539+
style_edition: Option<StyleEdition>,
528540
color: Option<Color>,
529541
file_lines: FileLines, // Default is all lines in all files.
530542
unstable_features: bool,
@@ -556,6 +568,10 @@ impl GetOptsOptions {
556568
if let Some(ref file_lines) = matches.opt_str("file-lines") {
557569
options.file_lines = file_lines.parse()?;
558570
}
571+
if let Some(ref edition_str) = matches.opt_str("style-edition") {
572+
options.style_edition =
573+
Some(style_edition_from_style_edition_str(edition_str)?);
574+
}
559575
} else {
560576
let mut unstable_options = vec![];
561577
if matches.opt_present("skip-children") {
@@ -567,6 +583,9 @@ impl GetOptsOptions {
567583
if matches.opt_present("file-lines") {
568584
unstable_options.push("`--file-lines`");
569585
}
586+
if matches.opt_present("style-edition") {
587+
unstable_options.push("`--style-edition`");
588+
}
570589
if !unstable_options.is_empty() {
571590
let s = if unstable_options.len() == 1 { "" } else { "s" };
572591
return Err(format_err!(
@@ -688,6 +707,9 @@ impl CliOptions for GetOptsOptions {
688707
if let Some(edition) = self.edition {
689708
config.set_cli().edition(edition);
690709
}
710+
if let Some(edition) = self.style_edition {
711+
config.set().style_edition(edition);
712+
}
691713
if self.check {
692714
config.set_cli().emit_mode(EmitMode::Diff);
693715
} else if let Some(emit_mode) = self.emit_mode {
@@ -723,6 +745,16 @@ fn edition_from_edition_str(edition_str: &str) -> Result<Edition> {
723745
}
724746
}
725747

748+
fn style_edition_from_style_edition_str(edition_str: &str) -> Result<StyleEdition> {
749+
match edition_str {
750+
"2015" => Ok(StyleEdition::Edition2015),
751+
"2018" => Ok(StyleEdition::Edition2018),
752+
"2021" => Ok(StyleEdition::Edition2021),
753+
"2024" => Ok(StyleEdition::Edition2024),
754+
_ => Err(format_err!("Invalid value for `--style-edition`")),
755+
}
756+
}
757+
726758
fn emit_mode_from_emit_str(emit_str: &str) -> Result<EmitMode> {
727759
match emit_str {
728760
"files" => Ok(EmitMode::Files),

0 commit comments

Comments
 (0)