]> git.lizzy.rs Git - rust.git/blobdiff - src/bin/rustfmt.rs
Add --dump-default-config and --dump-minimal-config.
[rust.git] / src / bin / rustfmt.rs
index 2c5cf6973622803b78d333b888bc8717c9cf4e64..73c4439ba2965aa2e713b28e0600a20ca04b90ce 100644 (file)
@@ -44,6 +44,7 @@ enum Operation {
     Format {
         files: Vec<PathBuf>,
         config_path: Option<PathBuf>,
+        minimal_config_path: Option<String>,
     },
     /// Print the help message.
     Help,
@@ -51,6 +52,8 @@ enum Operation {
     Version,
     /// Print detailed configuration help.
     ConfigHelp,
+    /// Output default config to a file
+    ConfigOutputDefault { path: String },
     /// No file specified, read from stdin
     Stdin {
         input: String,
@@ -89,11 +92,11 @@ fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
     }
 
     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);
         }
     }
 }
@@ -186,6 +189,14 @@ fn make_opts() -> Options {
     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 \
@@ -216,18 +227,24 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
             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);
                     }
@@ -236,7 +253,11 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
 
             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() {
@@ -286,6 +307,15 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
                     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)
         }
     }
@@ -353,6 +383,10 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
         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);
     }
@@ -383,6 +417,9 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
         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();
@@ -408,5 +445,6 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
     Ok(Operation::Format {
            files: files,
            config_path: config_path,
+           minimal_config_path: minimal_config_path,
        })
 }