extern crate rustfmt_nightly as rustfmt;
use std::env;
+use std::io::stdout;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use getopts::{Matches, Options};
-use rustfmt::{config, run, Input};
+use rustfmt::{load_config, CliOptions, Input, Session};
fn prune_files(files: Vec<&str>) -> Vec<&str> {
let prefixes: Vec<_> = files
let mut pruned_prefixes = vec![];
for p1 in prefixes {
- let mut include = true;
- if !p1.starts_with("src/bin/") {
- for p2 in &pruned_prefixes {
- if p1.starts_with(p2) {
- include = false;
- break;
- }
- }
- }
- if include {
+ if p1.starts_with("src/bin/") || pruned_prefixes.iter().all(|p2| !p1.starts_with(p2)) {
pruned_prefixes.push(p1);
}
}
files
.into_iter()
.filter(|f| {
- let mut include = true;
if f.ends_with("mod.rs") || f.ends_with("lib.rs") || f.starts_with("src/bin/") {
return true;
}
- for pp in &pruned_prefixes {
- if f.starts_with(pp) {
- include = false;
- break;
- }
- }
- include
+ pruned_prefixes.iter().all(|pp| !f.starts_with(pp))
})
.collect()
}
}
fn fmt_files(files: &[&str]) -> i32 {
- let (config, _) = config::Config::from_resolved_toml_path(Path::new("."))
- .unwrap_or_else(|_| (config::Config::default(), None));
+ let (config, _) =
+ load_config::<NullOptions>(Some(Path::new(".")), None).expect("couldn't load config");
let mut exit_code = 0;
+ let mut out = stdout();
+ let mut session = Session::new(config, Some(&mut out));
for file in files {
- let summary = run(Input::File(PathBuf::from(file)), &config);
- if !summary.has_no_errors() {
+ let report = session.format(Input::File(PathBuf::from(file))).unwrap();
+ if report.has_warnings() {
+ eprintln!("{}", report);
+ }
+ if !session.has_no_errors() {
exit_code = 1;
}
}
exit_code
}
+struct NullOptions;
+
+impl CliOptions for NullOptions {
+ fn apply_to(self, _: &mut rustfmt::Config) {
+ unreachable!();
+ }
+ fn config_path(&self) -> Option<&Path> {
+ unreachable!();
+ }
+}
+
fn uncommitted_files() -> Vec<String> {
let mut cmd = Command::new("git");
cmd.arg("ls-files");
env_logger::init();
let opts = make_opts();
- let matches = opts.parse(env::args().skip(1))
+ let matches = opts
+ .parse(env::args().skip(1))
.expect("Couldn't parse command line");
let config = Config::from_args(&matches, &opts);