Make some write modes unstable

This commit is contained in:
Nick Cameron 2018-05-14 10:41:15 +12:00
parent 3b23a98d00
commit 6d0695303a
4 changed files with 36 additions and 36 deletions

View File

@ -26,7 +26,7 @@
use rustfmt::{
emit_post_matter, emit_pre_matter, format_and_emit_report, load_config, CliOptions, Config,
FileName, FmtResult, Input, Summary, Verbosity, WriteMode, WRITE_MODE_LIST,
FileName, FmtResult, Input, Summary, Verbosity, WriteMode,
};
fn main() {
@ -111,7 +111,13 @@ fn make_opts() -> Options {
found reverts to the input file path",
"[Path for the configuration file]",
);
opts.optopt("", "emit", "What data to emit and how", WRITE_MODE_LIST);
let is_nightly = is_nightly();
let emit_opts = if is_nightly {
"[files|stdout|coverage|checkstyle]"
} else {
"[files|stdout]"
};
opts.optopt("", "emit", "What data to emit and how", emit_opts);
opts.optflagopt(
"h",
"help",
@ -129,7 +135,7 @@ fn make_opts() -> Options {
opts.optflag("q", "quiet", "Print less output");
opts.optflag("V", "version", "Show version information");
if is_nightly() {
if is_nightly {
opts.optflag(
"",
"unstable-features",

View File

@ -14,7 +14,7 @@
use config::file_lines::FileLines;
use config::lists::*;
use config::Config;
use {FmtResult, WRITE_MODE_LIST};
use FmtResult;
use failure::err_msg;
@ -174,14 +174,12 @@ pub fn to_list_tactic(self) -> ListTactic {
}
configuration_option_enum! { WriteMode:
// Backs the original file up and overwrites the original.
Replace,
// Overwrites original file without backup.
Overwrite,
// Backs the original file up and overwrites the original.
Replace,
// Writes the output to stdout.
Display,
// Writes the diff to stdout.
Diff,
// Displays how much of the input file was processed
Coverage,
// Unfancy stdout
@ -196,6 +194,13 @@ pub fn to_list_tactic(self) -> ListTactic {
None,
}
const STABLE_WRITE_MODES: [WriteMode; 4] = [
WriteMode::Replace,
WriteMode::Overwrite,
WriteMode::Display,
WriteMode::Check,
];
configuration_option_enum! { Color:
// Always use color, whether it is a piped or terminal output
Always,
@ -331,7 +336,7 @@ pub struct CliOptions {
pub quiet: bool,
pub verbose: bool,
pub config_path: Option<PathBuf>,
pub write_mode: Option<WriteMode>,
pub write_mode: WriteMode,
pub check: bool,
pub color: Option<Color>,
pub file_lines: FileLines, // Default is all lines in all files.
@ -355,7 +360,7 @@ pub fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
options.unstable_features = matches.opt_present("unstable-features");
}
if options.unstable_features {
if !options.unstable_features {
if matches.opt_present("skip-children") {
options.skip_children = Some(true);
}
@ -375,16 +380,21 @@ pub fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
return Err(format_err!("Invalid to use `--emit` and `--check`"));
}
if let Ok(write_mode) = write_mode_from_emit_str(emit_str) {
if write_mode == WriteMode::Overwrite && matches.opt_present("backup") {
options.write_mode = Some(WriteMode::Replace);
} else {
options.write_mode = Some(write_mode);
}
options.write_mode = write_mode;
} else {
return Err(format_err!("Invalid value for `--emit`"));
}
}
if options.write_mode == WriteMode::Overwrite && matches.opt_present("backup") {
options.write_mode = WriteMode::Replace;
}
if !rust_nightly {
if !STABLE_WRITE_MODES.contains(&options.write_mode) {
return Err(format_err!(
"Invalid value for `--emit`: {}, expected one of {}",
emit_str,
WRITE_MODE_LIST
"Invalid value for `--emit` - using an unstable \
value without `--unstable-features`",
));
}
}
@ -417,8 +427,8 @@ pub fn apply_to(self, config: &mut Config) {
}
if self.check {
config.set().write_mode(WriteMode::Check);
} else if let Some(write_mode) = self.write_mode {
config.set().write_mode(write_mode);
} else {
config.set().write_mode(self.write_mode);
}
if let Some(color) = self.color {
config.set().color(color);

View File

@ -156,19 +156,6 @@ fn create_diff(
}
write_system_newlines(out, text, config)?;
}
WriteMode::Diff => {
let filename = filename_to_path();
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
let mismatch = make_diff(&ori, &fmt, 3);
let has_diff = !mismatch.is_empty();
print_diff(
mismatch,
|line_num| format!("Diff in {} at line {}:", filename.display(), line_num),
config,
);
return Ok(has_diff);
}
}
WriteMode::Modified => {
let filename = filename_to_path();
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {

View File

@ -68,9 +68,6 @@
pub type FmtResult<T> = std::result::Result<T, failure::Error>;
// FIXME: this is badly named since the user-facing name is `emit` not `write-mode`.
pub const WRITE_MODE_LIST: &str = "[files|stdout|coverage|checkstyle]";
#[macro_use]
mod utils;