From 17cbbd8cbc2d829e0ce07c619a2970a46a3b9b8c Mon Sep 17 00:00:00 2001 From: calebcartwright Date: Fri, 12 Jul 2019 20:59:29 -0500 Subject: [PATCH 1/2] feat: add --manifest-path support to cargo fmt --- src/cargo-fmt/main.rs | 67 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/src/cargo-fmt/main.rs b/src/cargo-fmt/main.rs index c063d8eb4194..f32f7b5f1be2 100644 --- a/src/cargo-fmt/main.rs +++ b/src/cargo-fmt/main.rs @@ -41,6 +41,10 @@ pub struct Opts { #[structopt(short = "p", long = "package", value_name = "package")] packages: Vec, + /// Specify path to Cargo.toml + #[structopt(long = "manifest-path", value_name = "manifest-path")] + manifest_path: Option, + /// Options passed to rustfmt // 'raw = true' to make `--` explicit. #[structopt(name = "rustfmt_options", raw(raw = "true"))] @@ -90,7 +94,27 @@ fn execute() -> i32 { let strategy = CargoFmtStrategy::from_opts(&opts); - handle_command_status(format_crate(verbosity, &strategy, opts.rustfmt_options)) + if opts.manifest_path.is_some() { + let specified_manifest_path = opts.manifest_path.unwrap(); + if !specified_manifest_path.ends_with("Cargo.toml") { + print_usage_to_stderr("the manifest-path must be a path to a Cargo.toml file"); + return FAILURE; + } + let manifest_path = PathBuf::from(specified_manifest_path); + handle_command_status(format_crate( + verbosity, + &strategy, + opts.rustfmt_options, + Some(&manifest_path), + )) + } else { + handle_command_status(format_crate( + verbosity, + &strategy, + opts.rustfmt_options, + None, + )) + } } fn print_usage_to_stderr(reason: &str) { @@ -142,6 +166,7 @@ fn format_crate( verbosity: Verbosity, strategy: &CargoFmtStrategy, rustfmt_args: Vec, + manifest_path: Option<&Path>, ) -> Result { let targets = if rustfmt_args .iter() @@ -149,7 +174,7 @@ fn format_crate( { BTreeSet::new() } else { - get_targets(strategy)? + get_targets(strategy, manifest_path)? }; // Currently only bin and lib files get formatted. @@ -227,13 +252,20 @@ pub fn from_opts(opts: &Opts) -> CargoFmtStrategy { } /// Based on the specified `CargoFmtStrategy`, returns a set of main source files. -fn get_targets(strategy: &CargoFmtStrategy) -> Result, io::Error> { +fn get_targets( + strategy: &CargoFmtStrategy, + manifest_path: Option<&Path>, +) -> Result, io::Error> { let mut targets = BTreeSet::new(); match *strategy { - CargoFmtStrategy::Root => get_targets_root_only(&mut targets)?, - CargoFmtStrategy::All => get_targets_recursive(None, &mut targets, &mut BTreeSet::new())?, - CargoFmtStrategy::Some(ref hitlist) => get_targets_with_hitlist(hitlist, &mut targets)?, + CargoFmtStrategy::Root => get_targets_root_only(manifest_path, &mut targets)?, + CargoFmtStrategy::All => { + get_targets_recursive(manifest_path, &mut targets, &mut BTreeSet::new())? + } + CargoFmtStrategy::Some(ref hitlist) => { + get_targets_with_hitlist(manifest_path, hitlist, &mut targets)? + } } if targets.is_empty() { @@ -246,12 +278,22 @@ fn get_targets(strategy: &CargoFmtStrategy) -> Result, io::Erro } } -fn get_targets_root_only(targets: &mut BTreeSet) -> Result<(), io::Error> { - let metadata = get_cargo_metadata(None, false)?; - let current_dir = env::current_dir()?.canonicalize()?; - let current_dir_manifest = current_dir.join("Cargo.toml"); +fn get_targets_root_only( + manifest_path: Option<&Path>, + targets: &mut BTreeSet, +) -> Result<(), io::Error> { + let metadata = get_cargo_metadata(manifest_path, false)?; let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?; - let in_workspace_root = workspace_root_path == current_dir; + let (in_workspace_root, current_dir_manifest) = if manifest_path.is_some() { + let target_manifest = manifest_path.unwrap().canonicalize()?; + (workspace_root_path == target_manifest, target_manifest) + } else { + let current_dir = env::current_dir()?.canonicalize()?; + ( + workspace_root_path == current_dir, + current_dir.join("Cargo.toml"), + ) + }; let package_targets = match metadata.packages.len() { 1 => metadata.packages.into_iter().next().unwrap().targets, @@ -319,10 +361,11 @@ fn get_targets_recursive( } fn get_targets_with_hitlist( + manifest_path: Option<&Path>, hitlist: &[String], targets: &mut BTreeSet, ) -> Result<(), io::Error> { - let metadata = get_cargo_metadata(None, false)?; + let metadata = get_cargo_metadata(manifest_path, false)?; let mut workspace_hitlist: BTreeSet<&String> = BTreeSet::from_iter(hitlist); From 6f67f077529525cb5f96803f17295079656f4058 Mon Sep 17 00:00:00 2001 From: calebcartwright Date: Sun, 14 Jul 2019 09:56:07 -0500 Subject: [PATCH 2/2] refactor: simplify manifest_path option checks --- src/cargo-fmt/main.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/cargo-fmt/main.rs b/src/cargo-fmt/main.rs index 3a0d1cfddbd3..7651a0ee4372 100644 --- a/src/cargo-fmt/main.rs +++ b/src/cargo-fmt/main.rs @@ -101,8 +101,7 @@ fn execute() -> i32 { let strategy = CargoFmtStrategy::from_opts(&opts); - if opts.manifest_path.is_some() { - let specified_manifest_path = opts.manifest_path.unwrap(); + if let Some(specified_manifest_path) = opts.manifest_path { if !specified_manifest_path.ends_with("Cargo.toml") { print_usage_to_stderr("the manifest-path must be a path to a Cargo.toml file"); return FAILURE; @@ -284,9 +283,11 @@ fn get_targets_root_only( ) -> Result<(), io::Error> { let metadata = get_cargo_metadata(manifest_path, false)?; let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?; - let (in_workspace_root, current_dir_manifest) = if manifest_path.is_some() { - let target_manifest = manifest_path.unwrap().canonicalize()?; - (workspace_root_path == target_manifest, target_manifest) + let (in_workspace_root, current_dir_manifest) = if let Some(target_manifest) = manifest_path { + ( + workspace_root_path == target_manifest, + target_manifest.canonicalize()?, + ) } else { let current_dir = env::current_dir()?.canonicalize()?; (