2024-04-24 01:47:46 +03:00
|
|
|
use anyhow::{bail, Context, Error, Result};
|
2024-03-28 23:06:36 +03:00
|
|
|
use std::{
|
2024-04-24 01:47:46 +03:00
|
|
|
fs::{create_dir, create_dir_all, OpenOptions},
|
2024-03-28 23:06:36 +03:00
|
|
|
io::{self, Write},
|
|
|
|
};
|
|
|
|
|
2024-04-23 20:18:25 +03:00
|
|
|
use crate::info_file::ExerciseInfo;
|
|
|
|
|
2024-03-28 23:06:36 +03:00
|
|
|
pub static EMBEDDED_FILES: EmbeddedFiles = rustlings_macros::include_files!();
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum WriteStrategy {
|
|
|
|
IfNotExists,
|
|
|
|
Overwrite,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WriteStrategy {
|
2024-04-24 01:47:46 +03:00
|
|
|
fn write(self, path: &str, content: &[u8]) -> Result<()> {
|
2024-04-23 20:18:25 +03:00
|
|
|
let file = match self {
|
2024-03-28 23:06:36 +03:00
|
|
|
Self::IfNotExists => OpenOptions::new().create_new(true).write(true).open(path),
|
|
|
|
Self::Overwrite => OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
|
|
|
.truncate(true)
|
|
|
|
.open(path),
|
2024-04-23 20:18:25 +03:00
|
|
|
};
|
|
|
|
|
2024-04-24 01:47:46 +03:00
|
|
|
file.context("Failed to open the file `{path}` in write mode")?
|
|
|
|
.write_all(content)
|
|
|
|
.context("Failed to write the file {path}")
|
2024-03-28 23:06:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 20:18:25 +03:00
|
|
|
struct ExerciseFiles {
|
|
|
|
exercise: &'static [u8],
|
|
|
|
solution: &'static [u8],
|
2024-03-28 23:06:36 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 20:18:25 +03:00
|
|
|
struct ExerciseDir {
|
|
|
|
name: &'static str,
|
|
|
|
readme: &'static [u8],
|
2024-03-28 23:06:36 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 20:18:25 +03:00
|
|
|
impl ExerciseDir {
|
2024-04-24 01:47:46 +03:00
|
|
|
fn init_on_disk(&self) -> Result<()> {
|
2024-04-24 02:17:39 +03:00
|
|
|
let path_prefix = "exercises/";
|
|
|
|
let readme_path_postfix = "/README.md";
|
|
|
|
let mut dir_path =
|
|
|
|
String::with_capacity(path_prefix.len() + self.name.len() + readme_path_postfix.len());
|
|
|
|
dir_path.push_str(path_prefix);
|
|
|
|
dir_path.push_str(self.name);
|
|
|
|
|
2024-04-24 01:47:46 +03:00
|
|
|
if let Err(e) = create_dir(&dir_path) {
|
2024-04-23 20:18:25 +03:00
|
|
|
if e.kind() == io::ErrorKind::AlreadyExists {
|
|
|
|
return Ok(());
|
2024-03-28 23:06:36 +03:00
|
|
|
}
|
2024-04-23 20:18:25 +03:00
|
|
|
|
2024-04-24 01:47:46 +03:00
|
|
|
return Err(
|
|
|
|
Error::from(e).context(format!("Failed to create the directory {dir_path}"))
|
|
|
|
);
|
2024-03-28 23:06:36 +03:00
|
|
|
}
|
|
|
|
|
2024-04-24 02:17:39 +03:00
|
|
|
let readme_path = {
|
|
|
|
dir_path.push_str(readme_path_postfix);
|
|
|
|
dir_path
|
|
|
|
};
|
|
|
|
WriteStrategy::Overwrite.write(&readme_path, self.readme)?;
|
2024-04-24 01:47:46 +03:00
|
|
|
|
|
|
|
Ok(())
|
2024-03-28 23:06:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct EmbeddedFiles {
|
2024-04-23 20:18:25 +03:00
|
|
|
exercise_files: &'static [ExerciseFiles],
|
|
|
|
exercise_dirs: &'static [ExerciseDir],
|
2024-03-28 23:06:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EmbeddedFiles {
|
2024-04-24 01:47:46 +03:00
|
|
|
pub fn init_exercises_dir(&self, exercise_infos: &[ExerciseInfo]) -> Result<()> {
|
|
|
|
create_dir("exercises").context("Failed to create the directory `exercises`")?;
|
2024-03-29 00:11:16 +03:00
|
|
|
|
2024-04-23 20:18:25 +03:00
|
|
|
WriteStrategy::IfNotExists.write(
|
|
|
|
"exercises/README.md",
|
|
|
|
include_bytes!("../exercises/README.md"),
|
|
|
|
)?;
|
2024-03-29 00:11:16 +03:00
|
|
|
|
2024-04-23 20:18:25 +03:00
|
|
|
for dir in self.exercise_dirs {
|
2024-03-29 00:11:16 +03:00
|
|
|
dir.init_on_disk()?;
|
2024-04-23 20:18:25 +03:00
|
|
|
}
|
2024-03-29 00:11:16 +03:00
|
|
|
|
2024-04-23 20:18:25 +03:00
|
|
|
for (exercise_info, exercise_files) in exercise_infos.iter().zip(self.exercise_files) {
|
|
|
|
WriteStrategy::IfNotExists.write(&exercise_info.path(), exercise_files.exercise)?;
|
2024-03-29 00:11:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2024-03-28 23:06:36 +03:00
|
|
|
}
|
|
|
|
|
2024-04-24 01:47:46 +03:00
|
|
|
pub fn write_exercise_to_disk(
|
|
|
|
&self,
|
|
|
|
exercise_ind: usize,
|
|
|
|
dir_name: &str,
|
|
|
|
path: &str,
|
|
|
|
) -> Result<()> {
|
2024-04-23 20:18:25 +03:00
|
|
|
let Some(dir) = self.exercise_dirs.iter().find(|dir| dir.name == dir_name) else {
|
2024-04-24 01:47:46 +03:00
|
|
|
bail!("`{dir_name}` not found in the embedded directories");
|
2024-04-23 20:18:25 +03:00
|
|
|
};
|
2024-03-28 23:06:36 +03:00
|
|
|
|
2024-04-23 20:18:25 +03:00
|
|
|
dir.init_on_disk()?;
|
|
|
|
WriteStrategy::Overwrite.write(path, self.exercise_files[exercise_ind].exercise)
|
2024-03-28 23:06:36 +03:00
|
|
|
}
|
2024-04-24 01:47:46 +03:00
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|
2024-03-28 23:06:36 +03:00
|
|
|
}
|