// option. This file may not be copied, modified, or distributed
// except according to those terms.
-#![cfg(not(test))]
-#![feature(extern_prelude)]
-
extern crate env_logger;
#[macro_use]
extern crate failure;
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() {
opts.optflag(
"",
"check",
- "Run in 'check' mode. Exits with 0 if input if formatted correctly. Exits \
+ "Run in 'check' mode. Exits with 0 if input is formatted correctly. Exits \
with 1 and prints a diff if formatting is required.",
);
let is_nightly = is_nightly();
found reverts to the input file path",
"[Path for the configuration file]",
);
+ opts.optopt("", "edition", "Rust edition to use", "[2015|2018]");
opts.optopt(
"",
"color",
}
fn is_nightly() -> bool {
- option_env!("CFG_RELEASE_CHANNEL")
- .map(|c| c == "nightly" || c == "dev")
- .unwrap_or(false)
+ option_env!("CFG_RELEASE_CHANNEL").map_or(false, |c| c == "nightly" || c == "dev")
}
// Returned i32 is an exit code
// we will do comparison later, so here tries to canonicalize first
// to get the expected behavior.
p.canonicalize().unwrap_or(p)
- }).collect();
+ })
+ .collect();
Ok(Operation::Format {
files,
emit_mode: EmitMode,
backup: bool,
check: bool,
+ edition: Option<Edition>,
color: Option<Color>,
file_lines: FileLines, // Default is all lines in all files.
unstable_features: bool,
if options.check {
return Err(format_err!("Invalid to use `--emit` and `--check`"));
}
- if let Ok(emit_mode) = emit_mode_from_emit_str(emit_str) {
- options.emit_mode = emit_mode;
- } else {
- return Err(format_err!("Invalid value for `--emit`"));
- }
+
+ options.emit_mode = emit_mode_from_emit_str(emit_str)?;
+ }
+
+ if let Some(ref edition_str) = matches.opt_str("edition") {
+ options.edition = Some(edition_from_edition_str(edition_str)?);
}
if matches.opt_present("backup") {
if let Some(error_on_unformatted) = self.error_on_unformatted {
config.set().error_on_unformatted(error_on_unformatted);
}
+ if let Some(edition) = self.edition {
+ config.set().edition(edition);
+ }
if self.check {
config.set().emit_mode(EmitMode::Diff);
} else {
}
}
+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),