From e41fcb137cc28c2d0ec6d81eec802ab8cb0a9f61 Mon Sep 17 00:00:00 2001 From: Otavio Salvador Date: Tue, 23 Oct 2018 02:33:38 -0300 Subject: [PATCH] rustfmt: add support to specify the Rust edition as argument The new `--edition` command line argument allow the setting of the desired Rust edition to be used. Refs: #3104. Signed-off-by: Otavio Salvador --- src/bin/main.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 7bbb237ccb11..204efa19541c 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -25,8 +25,8 @@ use getopts::{Matches, Options}; use rustfmt::{ - load_config, CliOptions, Color, Config, EmitMode, ErrorKind, FileLines, FileName, Input, - Session, Verbosity, + load_config, CliOptions, Color, Config, Edition, EmitMode, ErrorKind, FileLines, FileName, + Input, Session, Verbosity, }; fn main() { @@ -102,6 +102,7 @@ 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]"); opts.optopt( "", "color", @@ -437,6 +438,7 @@ struct GetOptsOptions { emit_mode: EmitMode, backup: bool, check: bool, + edition: Edition, color: Option, file_lines: FileLines, // Default is all lines in all files. unstable_features: bool, @@ -500,6 +502,10 @@ pub fn from_matches(matches: &Matches) -> Result options.emit_mode = emit_mode_from_emit_str(emit_str)?; } + if let Some(ref edition_str) = matches.opt_str("edition") { + options.edition = edition_from_edition_str(edition_str)?; + } + if matches.opt_present("backup") { options.backup = true; } @@ -553,6 +559,7 @@ fn apply_to(self, config: &mut Config) { if let Some(error_on_unformatted) = self.error_on_unformatted { config.set().error_on_unformatted(error_on_unformatted); } + config.set().edition(self.edition); if self.check { config.set().emit_mode(EmitMode::Diff); } else { @@ -571,6 +578,14 @@ fn config_path(&self) -> Option<&Path> { } } +fn edition_from_edition_str(edition_str: &str) -> Result { + match edition_str { + "2015" => Ok(Edition::Edition2015), + "2018" => Ok(Edition::Edition2018), + _ => Err(format_err!("Invalid value for `--edition`")), + } +} + fn emit_mode_from_emit_str(emit_str: &str) -> Result { match emit_str { "files" => Ok(EmitMode::Files),