From 8a04ec6ec7d3db1ad101ea0b43fc570f38cea6c4 Mon Sep 17 00:00:00 2001 From: rchaser53 Date: Sun, 14 Apr 2019 00:23:10 +0900 Subject: [PATCH 1/3] fix not to emit version --- src/cargo-fmt/main.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/cargo-fmt/main.rs b/src/cargo-fmt/main.rs index a686e91b6947..59b76a542792 100644 --- a/src/cargo-fmt/main.rs +++ b/src/cargo-fmt/main.rs @@ -79,7 +79,7 @@ fn execute() -> i32 { } if matches.opt_present("version") { - return handle_command_status(get_version(verbosity), &opts); + return handle_command_status(get_version(), &opts); } let strategy = CargoFmtStrategy::from_matches(&matches); @@ -122,8 +122,19 @@ fn handle_command_status(status: Result, opts: &getopts::Options } } -fn get_version(verbosity: Verbosity) -> Result { - run_rustfmt(&BTreeSet::new(), &[String::from("--version")], verbosity) +fn get_version() -> Result { + let mut status = vec![]; + let mut command = Command::new("rustfmt") + .stdout(std::process::Stdio::inherit()) + .args(&[String::from("--version")]) + .spawn()?; + status.push(command.wait()?); + + Ok(status + .iter() + .filter_map(|s| if s.success() { None } else { s.code() }) + .next() + .unwrap_or(SUCCESS)) } fn format_crate(verbosity: Verbosity, strategy: &CargoFmtStrategy) -> Result { From 3759a6695d12abcb7901c82a7b93d184deb55248 Mon Sep 17 00:00:00 2001 From: rchaser53 Date: Sun, 14 Apr 2019 17:34:53 +0900 Subject: [PATCH 2/3] add the error mapping --- src/cargo-fmt/main.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cargo-fmt/main.rs b/src/cargo-fmt/main.rs index 59b76a542792..55f098348b99 100644 --- a/src/cargo-fmt/main.rs +++ b/src/cargo-fmt/main.rs @@ -127,7 +127,14 @@ fn get_version() -> Result { let mut command = Command::new("rustfmt") .stdout(std::process::Stdio::inherit()) .args(&[String::from("--version")]) - .spawn()?; + .spawn() + .map_err(|e| match e.kind() { + io::ErrorKind::NotFound => io::Error::new( + io::ErrorKind::Other, + "Could not run rustfmt, please make sure it is in your PATH.", + ), + _ => e, + })?; status.push(command.wait()?); Ok(status From 15ab3635084a4f6a7f56d20a2678e4c86019ac0e Mon Sep 17 00:00:00 2001 From: rchaser53 Date: Sun, 14 Apr 2019 20:37:29 +0900 Subject: [PATCH 3/3] not to use Vec --- src/cargo-fmt/main.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/cargo-fmt/main.rs b/src/cargo-fmt/main.rs index 55f098348b99..14d9c7f24237 100644 --- a/src/cargo-fmt/main.rs +++ b/src/cargo-fmt/main.rs @@ -123,7 +123,6 @@ fn handle_command_status(status: Result, opts: &getopts::Options } fn get_version() -> Result { - let mut status = vec![]; let mut command = Command::new("rustfmt") .stdout(std::process::Stdio::inherit()) .args(&[String::from("--version")]) @@ -135,13 +134,12 @@ fn get_version() -> Result { ), _ => e, })?; - status.push(command.wait()?); - - Ok(status - .iter() - .filter_map(|s| if s.success() { None } else { s.code() }) - .next() - .unwrap_or(SUCCESS)) + let result = command.wait()?; + if result.success() { + Ok(SUCCESS) + } else { + Ok(result.code().unwrap_or(SUCCESS)) + } } fn format_crate(verbosity: Verbosity, strategy: &CargoFmtStrategy) -> Result {