-/// Parsed command line options.
-#[derive(Clone, Debug, Default)]
-struct CliOptions {
- skip_children: bool,
- verbose: bool,
- write_mode: Option<WriteMode>,
- color: Option<Color>,
- file_lines: FileLines, // Default is all lines in all files.
- unstable_features: bool,
- error_on_unformatted: bool,
-}
-
-impl CliOptions {
- fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
- let mut options = CliOptions::default();
- options.skip_children = matches.opt_present("skip-children");
- options.verbose = matches.opt_present("verbose");
- let unstable_features = matches.opt_present("unstable-features");
- let rust_nightly = option_env!("CFG_RELEASE_CHANNEL")
- .map(|c| c == "nightly")
- .unwrap_or(false);
- if unstable_features && !rust_nightly {
- return Err(FmtError::from(
- "Unstable features are only available on Nightly channel",
- ));
- } else {
- options.unstable_features = unstable_features;
- }
-
- 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
- )));
- }
- }
-
- if let Some(ref color) = matches.opt_str("color") {
- match Color::from_str(color) {
- Ok(color) => options.color = Some(color),
- _ => return Err(FmtError::from(format!("Invalid color: {}", color))),
- }
- }
-
- if let Some(ref file_lines) = matches.opt_str("file-lines") {
- options.file_lines = file_lines.parse()?;
- }
-
- if matches.opt_present("error-on-unformatted") {
- options.error_on_unformatted = true;
- }
-
- Ok(options)
- }
-
- fn apply_to(self, config: &mut Config) {
- config.set().skip_children(self.skip_children);
- config.set().verbose(self.verbose);
- config.set().file_lines(self.file_lines);
- config.set().unstable_features(self.unstable_features);
- config.set().error_on_unformatted(self.error_on_unformatted);
- if let Some(write_mode) = self.write_mode {
- config.set().write_mode(write_mode);
- }
- if let Some(color) = self.color {
- config.set().color(color);
- }
- }
-}
-
-/// read the given config file path recursively if present else read the project file path
-fn match_cli_path_or_file(
- config_path: Option<PathBuf>,
- input_file: &Path,
-) -> FmtResult<(Config, Option<PathBuf>)> {
- if let Some(config_file) = config_path {
- let toml = Config::from_toml_path(config_file.as_ref())?;
- return Ok((toml, Some(config_file)));
- }
- Config::from_resolved_toml_path(input_file).map_err(FmtError::from)