Format {
files: Vec<PathBuf>,
config_path: Option<PathBuf>,
+ minimal_config_path: Option<String>,
},
/// Print the help message.
Help,
Version,
/// Print detailed configuration help.
ConfigHelp,
+ /// Output default config to a file
+ ConfigOutputDefault { path: String },
/// No file specified, read from stdin
Stdin {
input: String,
}
fn apply_to(self, config: &mut Config) {
- config.skip_children = self.skip_children;
- config.verbose = self.verbose;
- config.file_lines = self.file_lines;
+ config.set().skip_children(self.skip_children);
+ config.set().verbose(self.verbose);
+ config.set().file_lines(self.file_lines);
if let Some(write_mode) = self.write_mode {
- config.write_mode = write_mode;
+ config.set().write_mode(write_mode);
}
}
}
opts.optflag("",
"config-help",
"show details of rustfmt configuration options");
+ opts.optopt("",
+ "dump-default-config",
+ "Dumps the default configuration to a file and exits.",
+ "PATH");
+ opts.optopt("",
+ "dump-minimal-config",
+ "Dumps configuration options that were checked during formatting to a file.",
+ "PATH");
opts.optopt("",
"config-path",
"Recursively searches the given path for the rustfmt.toml config file. If not \
Config::print_docs();
Ok(Summary::new())
}
+ Operation::ConfigOutputDefault { path } => {
+ let mut file = File::create(path)?;
+ let toml = Config::default().all_options().to_toml()?;
+ file.write_all(toml.as_bytes())?;
+ Ok(Summary::new())
+ }
Operation::Stdin { input, config_path } => {
// try to read config from local directory
let (mut config, _) = match_cli_path_or_file(config_path,
&env::current_dir().unwrap())?;
// write_mode is always Plain for Stdin.
- config.write_mode = WriteMode::Plain;
+ config.set().write_mode(WriteMode::Plain);
// parse file_lines
if let Some(ref file_lines) = matches.opt_str("file-lines") {
- config.file_lines = file_lines.parse()?;
- for f in config.file_lines.files() {
+ config.set().file_lines(file_lines.parse()?);
+ for f in config.file_lines().files() {
if f != "stdin" {
println!("Warning: Extra file listed in file_lines option '{}'", f);
}
Ok(run(Input::Text(input), &config))
}
- Operation::Format { files, config_path } => {
+ Operation::Format {
+ files,
+ config_path,
+ minimal_config_path,
+ } => {
let options = CliOptions::from_matches(&matches)?;
for f in options.file_lines.files() {
error_summary.add(run(Input::File(file), &config));
}
}
+
+ // 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)
}
}
return Ok(Operation::ConfigHelp);
}
+ if let Some(path) = matches.opt_str("dump-default-config") {
+ return Ok(Operation::ConfigOutputDefault { path });
+ }
+
if matches.opt_present("version") {
return Ok(Operation::Version);
}
path @ _ => path,
};
+ // If no path is given, we won't output a minimal config.
+ let minimal_config_path = matches.opt_str("dump-minimal-config");
+
// if no file argument is supplied, read from stdin
if matches.free.is_empty() {
let mut buffer = String::new();
Ok(Operation::Format {
files: files,
config_path: config_path,
+ minimal_config_path: minimal_config_path,
})
}