mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-11-08 09:09:17 +01:00
Write the solution file on done
This commit is contained in:
parent
2dac8e509b
commit
b77007887c
@ -264,6 +264,16 @@ impl AppState {
|
|||||||
self.n_done += 1;
|
self.n_done += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if self.official_exercises {
|
||||||
|
EMBEDDED_FILES.write_solution_to_disk(
|
||||||
|
self.current_exercise_ind,
|
||||||
|
exercise
|
||||||
|
.dir
|
||||||
|
.context("Official exercises must be nested in the `exercises` directory")?,
|
||||||
|
exercise.name,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
let Some(ind) = self.next_pending_exercise_ind() else {
|
let Some(ind) = self.next_pending_exercise_ind() else {
|
||||||
writer.write_all(RERUNNING_ALL_EXERCISES_MSG)?;
|
writer.write_all(RERUNNING_ALL_EXERCISES_MSG)?;
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
|
use anyhow::{bail, Context, Error, Result};
|
||||||
use std::{
|
use std::{
|
||||||
fs::{create_dir, OpenOptions},
|
fs::{create_dir, create_dir_all, OpenOptions},
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ pub enum WriteStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl WriteStrategy {
|
impl WriteStrategy {
|
||||||
fn write(self, path: &str, content: &[u8]) -> io::Result<()> {
|
fn write(self, path: &str, content: &[u8]) -> Result<()> {
|
||||||
let file = match self {
|
let file = match self {
|
||||||
Self::IfNotExists => OpenOptions::new().create_new(true).write(true).open(path),
|
Self::IfNotExists => OpenOptions::new().create_new(true).write(true).open(path),
|
||||||
Self::Overwrite => OpenOptions::new()
|
Self::Overwrite => OpenOptions::new()
|
||||||
@ -24,7 +25,9 @@ impl WriteStrategy {
|
|||||||
.open(path),
|
.open(path),
|
||||||
};
|
};
|
||||||
|
|
||||||
file?.write_all(content)
|
file.context("Failed to open the file `{path}` in write mode")?
|
||||||
|
.write_all(content)
|
||||||
|
.context("Failed to write the file {path}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,16 +42,22 @@ struct ExerciseDir {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ExerciseDir {
|
impl ExerciseDir {
|
||||||
fn init_on_disk(&self) -> io::Result<()> {
|
fn init_on_disk(&self) -> Result<()> {
|
||||||
if let Err(e) = create_dir(format!("exercises/{}", self.name)) {
|
let dir_path = format!("exercises/{}", self.name);
|
||||||
|
if let Err(e) = create_dir(&dir_path) {
|
||||||
if e.kind() == io::ErrorKind::AlreadyExists {
|
if e.kind() == io::ErrorKind::AlreadyExists {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
return Err(e);
|
return Err(
|
||||||
|
Error::from(e).context(format!("Failed to create the directory {dir_path}"))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
WriteStrategy::Overwrite.write(&format!("exercises/{}/README.md", self.name), self.readme)
|
WriteStrategy::Overwrite
|
||||||
|
.write(&format!("exercises/{}/README.md", self.name), self.readme)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,8 +67,8 @@ pub struct EmbeddedFiles {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl EmbeddedFiles {
|
impl EmbeddedFiles {
|
||||||
pub fn init_exercises_dir(&self, exercise_infos: &[ExerciseInfo]) -> io::Result<()> {
|
pub fn init_exercises_dir(&self, exercise_infos: &[ExerciseInfo]) -> Result<()> {
|
||||||
create_dir("exercises")?;
|
create_dir("exercises").context("Failed to create the directory `exercises`")?;
|
||||||
|
|
||||||
WriteStrategy::IfNotExists.write(
|
WriteStrategy::IfNotExists.write(
|
||||||
"exercises/README.md",
|
"exercises/README.md",
|
||||||
@ -77,15 +86,32 @@ impl EmbeddedFiles {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_exercise_to_disk(&self, exercise_ind: usize, dir_name: &str, path: &str) -> io::Result<()> {
|
pub fn write_exercise_to_disk(
|
||||||
|
&self,
|
||||||
|
exercise_ind: usize,
|
||||||
|
dir_name: &str,
|
||||||
|
path: &str,
|
||||||
|
) -> Result<()> {
|
||||||
let Some(dir) = self.exercise_dirs.iter().find(|dir| dir.name == dir_name) else {
|
let Some(dir) = self.exercise_dirs.iter().find(|dir| dir.name == dir_name) else {
|
||||||
return Err(io::Error::new(
|
bail!("`{dir_name}` not found in the embedded directories");
|
||||||
io::ErrorKind::NotFound,
|
|
||||||
format!("`{dir_name}` not found in the embedded directories"),
|
|
||||||
));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
dir.init_on_disk()?;
|
dir.init_on_disk()?;
|
||||||
WriteStrategy::Overwrite.write(path, self.exercise_files[exercise_ind].exercise)
|
WriteStrategy::Overwrite.write(path, self.exercise_files[exercise_ind].exercise)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn write_solution_to_disk(
|
||||||
|
&self,
|
||||||
|
exercise_ind: usize,
|
||||||
|
dir_name: &str,
|
||||||
|
exercise_name: &str,
|
||||||
|
) -> Result<()> {
|
||||||
|
let dir_path = format!("solutions/{dir_name}");
|
||||||
|
create_dir_all(&dir_path).context("Failed to create the directory {dir_path}")?;
|
||||||
|
|
||||||
|
WriteStrategy::Overwrite.write(
|
||||||
|
&format!("{dir_path}/{exercise_name}.rs"),
|
||||||
|
self.exercise_files[exercise_ind].solution,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user