summaryrefslogtreecommitdiff
path: root/src/app_state.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-04-27 17:31:51 +0200
committermo8it <mo8it@proton.me>2024-04-27 17:31:51 +0200
commitcdeb8ce2292d5968e3866fd96cc422756f5a0ff4 (patch)
tree0e508bfd627baaf2eb7c48b3662344f2909b71e0 /src/app_state.rs
parent12504b01e910cd9066a9d8a6c41896470033b0c2 (diff)
Fix initialization
Diffstat (limited to 'src/app_state.rs')
-rw-r--r--src/app_state.rs32
1 files changed, 29 insertions, 3 deletions
diff --git a/src/app_state.rs b/src/app_state.rs
index b980bdb..7683c14 100644
--- a/src/app_state.rs
+++ b/src/app_state.rs
@@ -4,6 +4,7 @@ use crossterm::{
terminal::{Clear, ClearType},
ExecutableCommand,
};
+use serde::Deserialize;
use std::{
fs::{self, File},
io::{Read, StdoutLock, Write},
@@ -32,6 +33,11 @@ pub enum StateFileStatus {
NotRead,
}
+#[derive(Deserialize)]
+struct CargoMetadata {
+ target_directory: PathBuf,
+}
+
pub struct AppState {
current_exercise_ind: usize,
exercises: Vec<Exercise>,
@@ -91,8 +97,24 @@ impl AppState {
pub fn new(
exercise_infos: Vec<ExerciseInfo>,
final_message: String,
- target_dir: PathBuf,
- ) -> (Self, StateFileStatus) {
+ ) -> Result<(Self, StateFileStatus)> {
+ 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;
+
let exercises = exercise_infos
.into_iter()
.map(|mut exercise_info| {
@@ -134,7 +156,7 @@ impl AppState {
let state_file_status = slf.update_from_file();
- (slf, state_file_status)
+ Ok((slf, state_file_status))
}
#[inline]
@@ -388,6 +410,10 @@ impl AppState {
}
}
+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.";
+
const RERUNNING_ALL_EXERCISES_MSG: &[u8] = b"
All exercises seem to be done.
Recompiling and running all exercises to make sure that all of them are actually done.