summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-08-09 00:12:49 +0200
committermo8it <mo8it@proton.me>2024-08-09 00:12:49 +0200
commit337460d299e59552620d4a9cc3de3a8cf067a4f8 (patch)
tree0e6ce343956040d493f1b7945dffcff57a32cd2d
parente41c3a7c925387ca2c2441b4f41c963b95bc828d (diff)
Check the status of the `cargo metadata` command
-rw-r--r--src/cmd.rs16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/cmd.rs b/src/cmd.rs
index ba6ec89..a10a7ea 100644
--- a/src/cmd.rs
+++ b/src/cmd.rs
@@ -1,4 +1,4 @@
-use anyhow::{Context, Result};
+use anyhow::{bail, Context, Result};
use serde::Deserialize;
use std::{
io::Read,
@@ -68,12 +68,16 @@ impl CmdRunner {
.stdin(Stdio::null())
.stderr(Stdio::inherit())
.output()
- .context(CARGO_METADATA_ERR)?
- .stdout;
+ .context(CARGO_METADATA_ERR)?;
- let target_dir = serde_json::de::from_slice::<CargoMetadata>(&metadata_output)
- .context("Failed to read the field `target_directory` from the `cargo metadata` output")
- .map(|metadata| metadata.target_directory)?;
+ if !metadata_output.status.success() {
+ bail!("The command `cargo metadata …` failed. Are you in the `rustlings/` directory?");
+ }
+
+ let target_dir = serde_json::de::from_slice::<CargoMetadata>(&metadata_output.stdout)
+ .context(
+ "Failed to read the field `target_directory` from the output of the command `cargo metadata …`",
+ )?.target_directory;
Ok(Self { target_dir })
}