summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs29
1 files changed, 25 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index 7a142fd..b03aa52 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,16 +5,18 @@ use crossterm::{
terminal::{Clear, ClearType},
ExecutableCommand,
};
+use serde::Deserialize;
use std::{
io::{self, BufRead, Write},
- path::Path,
- process::exit,
+ path::{Path, PathBuf},
+ process::{exit, Command, Stdio},
};
use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile, watch::WatchExit};
mod app_state;
mod cargo_toml;
+mod cmd;
mod dev;
mod embedded;
mod exercise;
@@ -75,6 +77,11 @@ enum Subcommands {
Dev(DevCommands),
}
+#[derive(Deserialize)]
+struct CargoMetadata {
+ target_directory: PathBuf,
+}
+
fn in_official_repo() -> bool {
Path::new("dev/rustlings-repo.txt").exists()
}
@@ -86,7 +93,20 @@ fn main() -> Result<()> {
bail!("{OLD_METHOD_ERR}");
}
- which::which("cargo").context(CARGO_NOT_FOUND_ERR)?;
+ let metadata_output = Command::new("cargo")
+ .arg("metadata")
+ .arg("-q")
+ .arg("--format-version")
+ .arg("1")
+ .arg("--no-deps")
+ .stdin(Stdio::null())
+ .stderr(Stdio::inherit())
+ .output()
+ .context(CARGO_METADATA_ERR)?
+ .stdout;
+ let target_dir = serde_json::de::from_slice::<CargoMetadata>(&metadata_output)
+ .context("Failed to read the field `target_directory` from the `cargo metadata` output")?
+ .target_directory;
match args.command {
Some(Subcommands::Init) => {
@@ -122,6 +142,7 @@ fn main() -> Result<()> {
let (mut app_state, state_file_status) = AppState::new(
info_file.exercises,
info_file.final_message.unwrap_or_default(),
+ target_dir,
);
if let Some(welcome_message) = info_file.welcome_message {
@@ -198,7 +219,7 @@ The new method doesn't include cloning the Rustlings' repository.
Please follow the instructions in the README:
https://github.com/rust-lang/rustlings#getting-started";
-const CARGO_NOT_FOUND_ERR: &str = "Failed to find `cargo`.
+const CARGO_METADATA_ERR: &str = "Failed to run the command `cargo metadata …`
Did you already install Rust?
Try running `cargo --version` to diagnose the problem.";