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 <otavio@ossystems.com.br>
This commit is contained in:
Otavio Salvador 2018-10-23 02:33:38 -03:00
parent 2eab9714e4
commit e41fcb137c

View file

@ -25,8 +25,8 @@ use failure::err_msg;
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<Color>,
file_lines: FileLines, // Default is all lines in all files.
unstable_features: bool,
@ -500,6 +502,10 @@ impl GetOptsOptions {
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 @@ impl CliOptions for GetOptsOptions {
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 @@ impl CliOptions for GetOptsOptions {
}
}
fn edition_from_edition_str(edition_str: &str) -> Result<Edition, failure::Error> {
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<EmitMode, failure::Error> {
match emit_str {
"files" => Ok(EmitMode::Files),