feat: add --style-edition option to rustfmt binary

This commit is contained in:
Caleb Cartwright 2024-07-19 18:22:42 -05:00 committed by Caleb Cartwright
parent 7c41e2bfba
commit b6c89fc657

View File

@ -19,7 +19,7 @@
use crate::rustfmt::{
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName,
FormatReportFormatterBuilder, Input, Session, Verbosity,
FormatReportFormatterBuilder, Input, Session, StyleEdition, Verbosity,
};
const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rustfmt/issues/new?labels=bug";
@ -129,7 +129,12 @@ fn make_opts() -> Options {
found reverts to the input file path",
"[Path for the configuration file]",
);
opts.optopt("", "edition", "Rust edition to use", "[2015|2018|2021]");
opts.optopt(
"",
"edition",
"Rust edition to use",
"[2015|2018|2021|2024]",
);
opts.optopt(
"",
"color",
@ -181,6 +186,12 @@ fn make_opts() -> Options {
"skip-children",
"Don't reformat child modules (unstable).",
);
opts.optopt(
"",
"style-edition",
"The edition of the Style Guide (unstable).",
"[2015|2018|2021|2024]",
);
}
opts.optflag("v", "verbose", "Print verbose output");
@ -525,6 +536,7 @@ struct GetOptsOptions {
backup: bool,
check: bool,
edition: Option<Edition>,
style_edition: Option<StyleEdition>,
color: Option<Color>,
file_lines: FileLines, // Default is all lines in all files.
unstable_features: bool,
@ -556,6 +568,10 @@ pub fn from_matches(matches: &Matches) -> Result<GetOptsOptions> {
if let Some(ref file_lines) = matches.opt_str("file-lines") {
options.file_lines = file_lines.parse()?;
}
if let Some(ref edition_str) = matches.opt_str("style-edition") {
options.style_edition =
Some(style_edition_from_style_edition_str(edition_str)?);
}
} else {
let mut unstable_options = vec![];
if matches.opt_present("skip-children") {
@ -567,6 +583,9 @@ pub fn from_matches(matches: &Matches) -> Result<GetOptsOptions> {
if matches.opt_present("file-lines") {
unstable_options.push("`--file-lines`");
}
if matches.opt_present("style-edition") {
unstable_options.push("`--style-edition`");
}
if !unstable_options.is_empty() {
let s = if unstable_options.len() == 1 { "" } else { "s" };
return Err(format_err!(
@ -688,6 +707,9 @@ fn apply_to(self, config: &mut Config) {
if let Some(edition) = self.edition {
config.set_cli().edition(edition);
}
if let Some(edition) = self.style_edition {
config.set().style_edition(edition);
}
if self.check {
config.set_cli().emit_mode(EmitMode::Diff);
} else if let Some(emit_mode) = self.emit_mode {
@ -723,6 +745,16 @@ fn edition_from_edition_str(edition_str: &str) -> Result<Edition> {
}
}
fn style_edition_from_style_edition_str(edition_str: &str) -> Result<StyleEdition> {
match edition_str {
"2015" => Ok(StyleEdition::Edition2015),
"2018" => Ok(StyleEdition::Edition2018),
"2021" => Ok(StyleEdition::Edition2021),
"2024" => Ok(StyleEdition::Edition2024),
_ => Err(format_err!("Invalid value for `--style-edition`")),
}
}
fn emit_mode_from_emit_str(emit_str: &str) -> Result<EmitMode> {
match emit_str {
"files" => Ok(EmitMode::Files),