1 use std::path::PathBuf; 2 use std::process::Command; 3 main()4fn main() { 5 // Gather all and environment settings 6 let options = parse_args(); 7 8 // Perform rustfmt for each manifest available 9 run_rustfmt(&options); 10 } 11 12 /// Run rustfmt on a set of Bazel targets run_rustfmt(options: &Config)13fn run_rustfmt(options: &Config) { 14 // In order to ensure the test parses all sources, we separately 15 // track whether or not a failure has occured when checking formatting. 16 let mut is_failure: bool = false; 17 18 for manifest in options.manifests.iter() { 19 // Ignore any targets which do not have source files. This can 20 // occur in cases where all source files are generated. 21 if manifest.sources.is_empty() { 22 continue; 23 } 24 25 // Run rustfmt 26 let status = Command::new(&options.rustfmt_config.rustfmt) 27 .arg("--check") 28 .arg("--edition") 29 .arg(&manifest.edition) 30 .arg("--config-path") 31 .arg(&options.rustfmt_config.config) 32 .args(&manifest.sources) 33 .status() 34 .expect("Failed to run rustfmt"); 35 36 if !status.success() { 37 is_failure = true; 38 } 39 } 40 41 if is_failure { 42 std::process::exit(1); 43 } 44 } 45 46 /// A struct containing details used for executing rustfmt. 47 #[derive(Debug)] 48 struct Config { 49 /// Information about the current rustfmt binary to run. 50 pub rustfmt_config: rustfmt_lib::RustfmtConfig, 51 52 /// A list of manifests containing information about sources 53 /// to check using rustfmt. 54 pub manifests: Vec<rustfmt_lib::RustfmtManifest>, 55 } 56 57 /// Parse settings from the environment into a config struct parse_args() -> Config58fn parse_args() -> Config { 59 let manifests: Vec<PathBuf> = rustfmt_lib::find_manifests(); 60 61 if manifests.is_empty() { 62 panic!("No manifests were found"); 63 } 64 65 Config { 66 rustfmt_config: rustfmt_lib::parse_rustfmt_config(), 67 manifests: manifests 68 .iter() 69 .map(|manifest| rustfmt_lib::parse_rustfmt_manifest(manifest)) 70 .collect(), 71 } 72 } 73