Skip to content

Commit 189f785

Browse files
committed
minor: standard snippet
1 parent a8da2ca commit 189f785

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

crates/rust-analyzer/build.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,23 @@ fn rev() -> String {
5151
}
5252

5353
fn commit_hash() -> Option<String> {
54-
output_to_string("git rev-parse --short HEAD")
54+
exec("git rev-parse --short HEAD").ok()
5555
}
5656

5757
fn build_date() -> Option<String> {
58-
output_to_string("date -u +%Y-%m-%d")
58+
exec("date -u +%Y-%m-%d").ok()
5959
}
6060

61-
fn output_to_string(command: &str) -> Option<String> {
61+
fn exec(command: &str) -> std::io::Result<String> {
6262
let args = command.split_ascii_whitespace().collect::<Vec<_>>();
63-
let output = Command::new(args[0]).args(&args[1..]).output().ok()?;
64-
let stdout = String::from_utf8(output.stdout).ok()?;
65-
Some(stdout.trim().to_string())
63+
let output = Command::new(args[0]).args(&args[1..]).output()?;
64+
if !output.status.success() {
65+
return Err(std::io::Error::new(
66+
std::io::ErrorKind::InvalidData,
67+
format!("command {:?} returned non-zero code", command,),
68+
));
69+
}
70+
let stdout = String::from_utf8(output.stdout)
71+
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
72+
Ok(stdout.trim().to_string())
6673
}

0 commit comments

Comments
 (0)