Fix initialization

This commit is contained in:
mo8it 2024-04-27 17:31:51 +02:00
parent 12504b01e9
commit cdeb8ce229
2 changed files with 32 additions and 32 deletions

View file

@ -4,6 +4,7 @@ use crossterm::{
terminal::{Clear, ClearType}, terminal::{Clear, ClearType},
ExecutableCommand, ExecutableCommand,
}; };
use serde::Deserialize;
use std::{ use std::{
fs::{self, File}, fs::{self, File},
io::{Read, StdoutLock, Write}, io::{Read, StdoutLock, Write},
@ -32,6 +33,11 @@ pub enum StateFileStatus {
NotRead, NotRead,
} }
#[derive(Deserialize)]
struct CargoMetadata {
target_directory: PathBuf,
}
pub struct AppState { pub struct AppState {
current_exercise_ind: usize, current_exercise_ind: usize,
exercises: Vec<Exercise>, exercises: Vec<Exercise>,
@ -91,8 +97,24 @@ impl AppState {
pub fn new( pub fn new(
exercise_infos: Vec<ExerciseInfo>, exercise_infos: Vec<ExerciseInfo>,
final_message: String, final_message: String,
target_dir: PathBuf, ) -> Result<(Self, StateFileStatus)> {
) -> (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 let exercises = exercise_infos
.into_iter() .into_iter()
.map(|mut exercise_info| { .map(|mut exercise_info| {
@ -134,7 +156,7 @@ impl AppState {
let state_file_status = slf.update_from_file(); let state_file_status = slf.update_from_file();
(slf, state_file_status) Ok((slf, state_file_status))
} }
#[inline] #[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" const RERUNNING_ALL_EXERCISES_MSG: &[u8] = b"
All exercises seem to be done. All exercises seem to be done.
Recompiling and running all exercises to make sure that all of them are actually done. Recompiling and running all exercises to make sure that all of them are actually done.

View file

@ -5,11 +5,10 @@ use crossterm::{
terminal::{Clear, ClearType}, terminal::{Clear, ClearType},
ExecutableCommand, ExecutableCommand,
}; };
use serde::Deserialize;
use std::{ use std::{
io::{self, BufRead, Write}, io::{self, BufRead, Write},
path::{Path, PathBuf}, path::Path,
process::{exit, Command, Stdio}, process::exit,
}; };
use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile, watch::WatchExit}; use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile, watch::WatchExit};
@ -77,11 +76,6 @@ enum Subcommands {
Dev(DevCommands), Dev(DevCommands),
} }
#[derive(Deserialize)]
struct CargoMetadata {
target_directory: PathBuf,
}
fn in_official_repo() -> bool { fn in_official_repo() -> bool {
Path::new("dev/rustlings-repo.txt").exists() Path::new("dev/rustlings-repo.txt").exists()
} }
@ -93,21 +87,6 @@ fn main() -> Result<()> {
bail!("{OLD_METHOD_ERR}"); bail!("{OLD_METHOD_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 { match args.command {
Some(Subcommands::Init) => { Some(Subcommands::Init) => {
if DEBUG_PROFILE { if DEBUG_PROFILE {
@ -142,8 +121,7 @@ fn main() -> Result<()> {
let (mut app_state, state_file_status) = AppState::new( let (mut app_state, state_file_status) = AppState::new(
info_file.exercises, info_file.exercises,
info_file.final_message.unwrap_or_default(), info_file.final_message.unwrap_or_default(),
target_dir, )?;
);
if let Some(welcome_message) = info_file.welcome_message { if let Some(welcome_message) = info_file.welcome_message {
match state_file_status { match state_file_status {
@ -219,10 +197,6 @@ The new method doesn't include cloning the Rustlings' repository.
Please follow the instructions in the README: Please follow the instructions in the README:
https://github.com/rust-lang/rustlings#getting-started"; https://github.com/rust-lang/rustlings#getting-started";
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 FORMAT_VERSION_HIGHER_ERR: &str = const FORMAT_VERSION_HIGHER_ERR: &str =
"The format version specified in the `info.toml` file is higher than the last one supported. "The format version specified in the `info.toml` file is higher than the last one supported.
It is possible that you have an outdated version of Rustlings. It is possible that you have an outdated version of Rustlings.