From: Kamal Marhubi Date: Sat, 16 Apr 2016 20:34:15 +0000 (-0400) Subject: rustfmt: Parse options once instead of once per file argument (#944) X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=27c91ee35b6dd42bb20f1aef4216299ab19bdaff;p=rust.git rustfmt: Parse options once instead of once per file argument (#944) `update_config()` was parsing the `write-mode` option once for each file argument. This commit parses them once up front into a `CliOptions` struct, which is then applied to the config before calling `run()`. --- diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 778a96a3c1b..472098f59eb 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -51,6 +51,40 @@ enum Operation { }, } +/// Parsed command line options. +#[derive(Clone, Debug, Default)] +struct CliOptions { + skip_children: bool, + verbose: bool, + write_mode: Option, +} + +impl CliOptions { + fn from_matches(matches: &Matches) -> FmtResult { + let mut options = CliOptions::default(); + options.skip_children = matches.opt_present("skip-children"); + options.verbose = matches.opt_present("verbose"); + + if let Some(ref write_mode) = matches.opt_str("write-mode") { + if let Ok(write_mode) = WriteMode::from_str(write_mode) { + options.write_mode = Some(write_mode); + } else { + return Err(FmtError::from(format!("Invalid write-mode: {}", write_mode))); + } + } + + Ok(options) + } + + fn apply_to(&self, config: &mut Config) { + config.skip_children = self.skip_children; + config.verbose = self.verbose; + if let Some(write_mode) = self.write_mode { + config.write_mode = write_mode; + } + } +} + /// Try to find a project file in the given directory and its parents. Returns the path of a the /// nearest project file if one exists, or `None` if no project file was found. fn lookup_project_file(dir: &Path) -> FmtResult> { @@ -115,24 +149,6 @@ fn match_cli_path_or_file(config_path: Option, resolve_config(input_file) } -fn update_config(config: &mut Config, matches: &Matches) -> FmtResult<()> { - config.verbose = matches.opt_present("verbose"); - config.skip_children = matches.opt_present("skip-children"); - - let write_mode = matches.opt_str("write-mode"); - match matches.opt_str("write-mode").map(|wm| WriteMode::from_str(&wm)) { - None => Ok(()), - Some(Ok(write_mode)) => { - config.write_mode = write_mode; - Ok(()) - } - Some(Err(_)) => { - Err(FmtError::from(format!("Invalid write-mode: {}", - write_mode.expect("cannot happen")))) - } - } -} - fn make_opts() -> Options { let mut opts = Options::new(); opts.optflag("h", "help", "show this message"); @@ -183,6 +199,7 @@ fn execute(opts: &Options) -> FmtResult { Ok(run(Input::Text(input), &config)) } Operation::Format { files, config_path } => { + let options = try!(CliOptions::from_matches(&matches)); let mut config = Config::default(); let mut path = None; // Load the config path file if provided @@ -213,7 +230,7 @@ fn execute(opts: &Options) -> FmtResult { config = config_tmp; } - try!(update_config(&mut config, &matches)); + options.apply_to(&mut config); error_summary.add(run(Input::File(file), &config)); } Ok(error_summary)