diff options
| author | Nick Cameron <ncameron@mozilla.com> | 2018-04-20 13:24:45 +1200 |
|---|---|---|
| committer | Nick Cameron <ncameron@mozilla.com> | 2018-04-20 13:24:45 +1200 |
| commit | a73f14aa9563738b9de12c612e6add412a36dd40 (patch) | |
| tree | 9235993a26b8a1757377c4cd4f08c7c52e070511 /src | |
| parent | f9532ba8d7ffec4bbd2698c048c383910fe714c4 (diff) | |
Trivial refactoring in bin
Diffstat (limited to 'src')
| -rw-r--r-- | src/bin/main.rs | 192 |
1 files changed, 100 insertions, 92 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs index ff0961fd2e2..9411ccde41f 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -32,6 +32,39 @@ type FmtResult<T> = std::result::Result<T, FmtError>; const WRITE_MODE_LIST: &str = "[replace|overwrite|display|plain|diff|coverage|checkstyle|check]"; +fn main() { + env_logger::init(); + let opts = make_opts(); + // Only handles arguments passed in through the CLI. + let write_mode = determine_write_mode(&opts); + + let exit_code = match execute(&opts) { + Ok(summary) => { + if summary.has_operational_errors() + || summary.has_diff && write_mode == WriteMode::Check + || summary.has_parsing_errors() || summary.has_formatting_errors() + { + 1 + } else { + assert!(summary.has_no_errors()); + 0 + } + } + Err(e) => { + eprintln!("{}", e.to_string()); + 1 + } + }; + // Make sure standard output is flushed before we exit. + std::io::stdout().flush().unwrap(); + + // Exit with given exit code. + // + // NOTE: This immediately terminates the process without doing any cleanup, + // so make sure to finish all necessary cleanup before this is called. + std::process::exit(exit_code); +} + /// Rustfmt operations. enum Operation { /// Format files and their child modules. @@ -277,78 +310,86 @@ fn execute(opts: &Options) -> FmtResult<Summary> { minimal_config_path, } => { let options = CliOptions::from_matches(&matches)?; + format(files, config_path, minimal_config_path, options) + } + } +} - for f in options.file_lines.files() { - match *f { - FileName::Real(ref f) if files.contains(f) => {} - FileName::Real(_) => { - eprintln!("Warning: Extra file listed in file_lines option '{}'", f) - } - _ => eprintln!("Warning: Not a file '{}'", f), - } - } - - let mut config = Config::default(); - // Load the config path file if provided - if let Some(config_file) = config_path.as_ref() { - config = Config::from_toml_path(config_file.as_ref())?; - }; - - if options.verbose { - if let Some(path) = config_path.as_ref() { - println!("Using rustfmt config file {}", path.display()); - } +fn format( + files: Vec<PathBuf>, + config_path: Option<PathBuf>, + minimal_config_path: Option<String>, + options: CliOptions, +) -> FmtResult<Summary> { + for f in options.file_lines.files() { + match *f { + FileName::Real(ref f) if files.contains(f) => {} + FileName::Real(_) => { + eprintln!("Warning: Extra file listed in file_lines option '{}'", f) } + _ => eprintln!("Warning: Not a file '{}'", f), + } + } - let mut out = &mut stdout(); - checkstyle::output_header(&mut out, config.write_mode())?; - let mut error_summary = Summary::default(); + let mut config = Config::default(); + // Load the config path file if provided + if let Some(config_file) = config_path.as_ref() { + config = Config::from_toml_path(config_file.as_ref())?; + }; - for file in files { - if !file.exists() { - eprintln!("Error: file `{}` does not exist", file.to_str().unwrap()); - error_summary.add_operational_error(); - } else if file.is_dir() { - eprintln!("Error: `{}` is a directory", file.to_str().unwrap()); - error_summary.add_operational_error(); - } else { - // Check the file directory if the config-path could not be read or not provided - if config_path.is_none() { - let (config_tmp, path_tmp) = - Config::from_resolved_toml_path(file.parent().unwrap())?; - if options.verbose { - if let Some(path) = path_tmp.as_ref() { - println!( - "Using rustfmt config file {} for {}", - path.display(), - file.display() - ); - } - } - config = config_tmp; - } + if options.verbose { + if let Some(path) = config_path.as_ref() { + println!("Using rustfmt config file {}", path.display()); + } + } - if !config.version_meets_requirement(&mut error_summary) { - break; + let mut out = &mut stdout(); + checkstyle::output_header(&mut out, config.write_mode())?; + let mut error_summary = Summary::default(); + + for file in files { + if !file.exists() { + eprintln!("Error: file `{}` does not exist", file.to_str().unwrap()); + error_summary.add_operational_error(); + } else if file.is_dir() { + eprintln!("Error: `{}` is a directory", file.to_str().unwrap()); + error_summary.add_operational_error(); + } else { + // Check the file directory if the config-path could not be read or not provided + if config_path.is_none() { + let (config_tmp, path_tmp) = + Config::from_resolved_toml_path(file.parent().unwrap())?; + if options.verbose { + if let Some(path) = path_tmp.as_ref() { + println!( + "Using rustfmt config file {} for {}", + path.display(), + file.display() + ); } - - options.clone().apply_to(&mut config); - error_summary.add(run(Input::File(file), &config)); } + config = config_tmp; } - checkstyle::output_footer(&mut out, config.write_mode())?; - // If we were given a path via dump-minimal-config, output any options - // that were used during formatting as TOML. - if let Some(path) = minimal_config_path { - let mut file = File::create(path)?; - let toml = config.used_options().to_toml()?; - file.write_all(toml.as_bytes())?; + if !config.version_meets_requirement(&mut error_summary) { + break; } - Ok(error_summary) + options.clone().apply_to(&mut config); + error_summary.add(run(Input::File(file), &config)); } } + checkstyle::output_footer(&mut out, config.write_mode())?; + + // If we were given a path via dump-minimal-config, output any options + // that were used during formatting as TOML. + if let Some(path) = minimal_config_path { + let mut file = File::create(path)?; + let toml = config.used_options().to_toml()?; + file.write_all(toml.as_bytes())?; + } + + Ok(error_summary) } fn determine_write_mode(opts: &Options) -> WriteMode { @@ -360,39 +401,6 @@ fn determine_write_mode(opts: &Options) -> WriteMode { } } -fn main() { - env_logger::init(); - let opts = make_opts(); - // Only handles arguments passed in through the CLI. - let write_mode = determine_write_mode(&opts); - - let exit_code = match execute(&opts) { - Ok(summary) => { - if summary.has_operational_errors() - || summary.has_diff && write_mode == WriteMode::Check - || summary.has_parsing_errors() || summary.has_formatting_errors() - { - 1 - } else { - assert!(summary.has_no_errors()); - 0 - } - } - Err(e) => { - eprintln!("{}", e.to_string()); - 1 - } - }; - // Make sure standard output is flushed before we exit. - std::io::stdout().flush().unwrap(); - - // Exit with given exit code. - // - // NOTE: This immediately terminates the process without doing any cleanup, - // so make sure to finish all necessary cleanup before this is called. - std::process::exit(exit_code); -} - fn print_usage_to_stdout(opts: &Options, reason: &str) { let sep = if reason.is_empty() { String::new() |
