fn apply_rustfmt()

in tools/rustfmt/srcs/main.rs [105:149]


fn apply_rustfmt(options: &Config, targets: &[String]) {
    // Ensure the targets are first built and a manifest containing `rustfmt`
    // arguments are generated before formatting source files.
    generate_rustfmt_target_manifests(options, targets);

    for target in targets.iter() {
        // Replace any `:` characters and strip leading slashes
        let target_path = target.replace(":", "/").trim_start_matches('/').to_owned();

        // Find a manifest for the current target. Not all targets will have one
        let manifest = options.workspace.join("bazel-bin").join(format!(
            "{}.{}",
            &target_path,
            rustfmt_lib::RUSTFMT_MANIFEST_EXTENSION,
        ));

        if !manifest.exists() {
            continue;
        }

        // Load the manifest containing rustfmt arguments
        let rustfmt_config = rustfmt_lib::parse_rustfmt_manifest(&manifest);

        // Ignore any targets which do not have source files. This can
        // occur in cases where all source files are generated.
        if rustfmt_config.sources.is_empty() {
            continue;
        }

        // Run rustfmt
        let status = Command::new(&options.rustfmt_config.rustfmt)
            .current_dir(&options.workspace)
            .arg("--edition")
            .arg(rustfmt_config.edition)
            .arg("--config-path")
            .arg(&options.rustfmt_config.config)
            .args(rustfmt_config.sources)
            .status()
            .expect("Failed to run rustfmt");

        if !status.success() {
            std::process::exit(status.code().unwrap_or(1));
        }
    }
}