|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::fs; |
| 3 | +use std::io::{BufRead, BufReader, Write}; |
| 4 | +use std::path::{Path, PathBuf}; |
| 5 | + |
| 6 | +use rustfmt_nightly::{load_config, CliOptions, Config, Input, Session}; |
| 7 | + |
| 8 | +fn main() { |
| 9 | + let mut args = std::env::args(); |
| 10 | + let Some(_arg0) = args.next() else { |
| 11 | + std::process::exit(1); |
| 12 | + }; |
| 13 | + let Some(filename) = args.next() else { |
| 14 | + std::process::exit(1); |
| 15 | + }; |
| 16 | + let filename: PathBuf = filename.into(); |
| 17 | + let opt_config = args.next().map(PathBuf::from); |
| 18 | + |
| 19 | + let config = if let Some(ref config_file_path) = opt_config { |
| 20 | + load_config(Some(config_file_path), None::<NullOptions>) |
| 21 | + .expect("`rustfmt.toml` not found") |
| 22 | + .0 |
| 23 | + } else { |
| 24 | + read_config(&filename) |
| 25 | + }; |
| 26 | + |
| 27 | + let input = Input::File(filename); |
| 28 | + let mut session = Session::<Blackhole>::new(config, None); |
| 29 | + let _ = session.format(input).unwrap(); |
| 30 | +} |
| 31 | + |
| 32 | +struct Blackhole; |
| 33 | +impl Write for Blackhole { |
| 34 | + fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 35 | + Ok(buf.len()) |
| 36 | + } |
| 37 | + |
| 38 | + fn flush(&mut self) -> std::io::Result<()> { |
| 39 | + Ok(()) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +struct NullOptions; |
| 44 | + |
| 45 | +impl CliOptions for NullOptions { |
| 46 | + fn apply_to(self, _: &mut Config) { |
| 47 | + unreachable!(); |
| 48 | + } |
| 49 | + fn config_path(&self) -> Option<&Path> { |
| 50 | + unreachable!(); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +fn read_config(filename: &Path) -> Config { |
| 55 | + let sig_comments = read_significant_comments(filename); |
| 56 | + // Look for a config file. If there is a 'config' property in the significant comments, use |
| 57 | + // that. Otherwise, if there are no significant comments at all, look for a config file with |
| 58 | + // the same name as the test file. |
| 59 | + let mut config = if !sig_comments.is_empty() { |
| 60 | + load_config( |
| 61 | + sig_comments.get("config").map(Path::new), |
| 62 | + None::<NullOptions>, |
| 63 | + ) |
| 64 | + .map(|(config, _)| config) |
| 65 | + .unwrap_or_default() |
| 66 | + } else { |
| 67 | + load_config( |
| 68 | + filename.with_extension("toml").file_name().map(Path::new), |
| 69 | + None::<NullOptions>, |
| 70 | + ) |
| 71 | + .map(|(config, _)| config) |
| 72 | + .unwrap_or_default() |
| 73 | + }; |
| 74 | + |
| 75 | + for (key, val) in &sig_comments { |
| 76 | + if key != "target" && key != "config" && key != "unstable" { |
| 77 | + config.override_value(key, val); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + config |
| 82 | +} |
| 83 | + |
| 84 | +// Reads significant comments of the form: `// rustfmt-key: value` into a hash map. |
| 85 | +fn read_significant_comments(file_name: &Path) -> HashMap<String, String> { |
| 86 | + let file = fs::File::open(file_name) |
| 87 | + .unwrap_or_else(|_| panic!("couldn't read file {}", file_name.display())); |
| 88 | + let reader = BufReader::new(file); |
| 89 | + let pattern = r"^\s*//\s*rustfmt-([^:]+):\s*(\S+)"; |
| 90 | + let regex = regex::Regex::new(pattern).expect("failed creating pattern 1"); |
| 91 | + |
| 92 | + // Matches lines containing significant comments or whitespace. |
| 93 | + let line_regex = regex::Regex::new(r"(^\s*$)|(^\s*//\s*rustfmt-[^:]+:\s*\S+)") |
| 94 | + .expect("failed creating pattern 2"); |
| 95 | + |
| 96 | + reader |
| 97 | + .lines() |
| 98 | + .map(|line| line.expect("failed getting line")) |
| 99 | + .filter(|line| line_regex.is_match(line)) |
| 100 | + .filter_map(|line| { |
| 101 | + regex.captures_iter(&line).next().map(|capture| { |
| 102 | + ( |
| 103 | + capture |
| 104 | + .get(1) |
| 105 | + .expect("couldn't unwrap capture") |
| 106 | + .as_str() |
| 107 | + .to_owned(), |
| 108 | + capture |
| 109 | + .get(2) |
| 110 | + .expect("couldn't unwrap capture") |
| 111 | + .as_str() |
| 112 | + .to_owned(), |
| 113 | + ) |
| 114 | + }) |
| 115 | + }) |
| 116 | + .collect() |
| 117 | +} |
0 commit comments