From 189f785749e9e6921ae1db4338c3d65c9da102cf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 7 May 2021 21:46:25 +0300 Subject: [PATCH] minor: standard snippet --- crates/rust-analyzer/build.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/rust-analyzer/build.rs b/crates/rust-analyzer/build.rs index bca6611d604..aceab82d9b0 100644 --- a/crates/rust-analyzer/build.rs +++ b/crates/rust-analyzer/build.rs @@ -51,16 +51,23 @@ fn rev() -> String { } fn commit_hash() -> Option { - output_to_string("git rev-parse --short HEAD") + exec("git rev-parse --short HEAD").ok() } fn build_date() -> Option { - output_to_string("date -u +%Y-%m-%d") + exec("date -u +%Y-%m-%d").ok() } -fn output_to_string(command: &str) -> Option { +fn exec(command: &str) -> std::io::Result { let args = command.split_ascii_whitespace().collect::>(); - let output = Command::new(args[0]).args(&args[1..]).output().ok()?; - let stdout = String::from_utf8(output.stdout).ok()?; - Some(stdout.trim().to_string()) + let output = Command::new(args[0]).args(&args[1..]).output()?; + if !output.status.success() { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + format!("command {:?} returned non-zero code", command,), + )); + } + let stdout = String::from_utf8(output.stdout) + .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?; + Ok(stdout.trim().to_string()) }