diff options
| author | Otavio Salvador <otavio@ossystems.com.br> | 2018-10-23 02:33:38 -0300 |
|---|---|---|
| committer | Otavio Salvador <otavio@ossystems.com.br> | 2018-10-23 02:33:38 -0300 |
| commit | e41fcb137cc28c2d0ec6d81eec802ab8cb0a9f61 (patch) | |
| tree | 9599f4ca184770ac36f53cc64b1aa6dfeb68d779 /src | |
| parent | 2eab9714e457cd5d7715a880cb4535e453e861e7 (diff) | |
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>
Diffstat (limited to 'src')
| -rw-r--r-- | src/bin/main.rs | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs index 7bbb237ccb1..204efa19541 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -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), |
