mirror of
https://git.aramjonghu.dev/AramJonghu/rustlings.git
synced 2026-09-03 00:43:29 +02:00
Refactor embedded files to add solutions
This commit is contained in:
+41
-69
@@ -1,9 +1,10 @@
|
||||
use std::{
|
||||
fs::{create_dir, File, OpenOptions},
|
||||
fs::{create_dir, OpenOptions},
|
||||
io::{self, Write},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use crate::info_file::ExerciseInfo;
|
||||
|
||||
pub static EMBEDDED_FILES: EmbeddedFiles = rustlings_macros::include_files!();
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -13,107 +14,78 @@ pub enum WriteStrategy {
|
||||
}
|
||||
|
||||
impl WriteStrategy {
|
||||
fn open<P: AsRef<Path>>(self, path: P) -> io::Result<File> {
|
||||
match self {
|
||||
fn write(self, path: &str, content: &[u8]) -> io::Result<()> {
|
||||
let file = match self {
|
||||
Self::IfNotExists => OpenOptions::new().create_new(true).write(true).open(path),
|
||||
Self::Overwrite => OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.open(path),
|
||||
}
|
||||
};
|
||||
|
||||
file?.write_all(content)
|
||||
}
|
||||
}
|
||||
|
||||
struct EmbeddedFile {
|
||||
path: &'static str,
|
||||
content: &'static [u8],
|
||||
struct ExerciseFiles {
|
||||
exercise: &'static [u8],
|
||||
solution: &'static [u8],
|
||||
}
|
||||
|
||||
impl EmbeddedFile {
|
||||
fn write_to_disk(&self, strategy: WriteStrategy) -> io::Result<()> {
|
||||
strategy.open(self.path)?.write_all(self.content)
|
||||
}
|
||||
struct ExerciseDir {
|
||||
name: &'static str,
|
||||
readme: &'static [u8],
|
||||
}
|
||||
|
||||
struct EmbeddedFlatDir {
|
||||
path: &'static str,
|
||||
readme: EmbeddedFile,
|
||||
content: &'static [EmbeddedFile],
|
||||
}
|
||||
|
||||
impl EmbeddedFlatDir {
|
||||
impl ExerciseDir {
|
||||
fn init_on_disk(&self) -> io::Result<()> {
|
||||
let path = Path::new(self.path);
|
||||
|
||||
if let Err(e) = create_dir(path) {
|
||||
if e.kind() != io::ErrorKind::AlreadyExists {
|
||||
return Err(e);
|
||||
if let Err(e) = create_dir(format!("exercises/{}", self.name)) {
|
||||
if e.kind() == io::ErrorKind::AlreadyExists {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
self.readme.write_to_disk(WriteStrategy::Overwrite)
|
||||
WriteStrategy::Overwrite.write(&format!("exercises/{}/README.md", self.name), self.readme)
|
||||
}
|
||||
}
|
||||
|
||||
struct ExercisesDir {
|
||||
readme: EmbeddedFile,
|
||||
files: &'static [EmbeddedFile],
|
||||
dirs: &'static [EmbeddedFlatDir],
|
||||
}
|
||||
|
||||
pub struct EmbeddedFiles {
|
||||
exercises_dir: ExercisesDir,
|
||||
exercise_files: &'static [ExerciseFiles],
|
||||
exercise_dirs: &'static [ExerciseDir],
|
||||
}
|
||||
|
||||
impl EmbeddedFiles {
|
||||
pub fn init_exercises_dir(&self) -> io::Result<()> {
|
||||
pub fn init_exercises_dir(&self, exercise_infos: &[ExerciseInfo]) -> io::Result<()> {
|
||||
create_dir("exercises")?;
|
||||
|
||||
self.exercises_dir
|
||||
.readme
|
||||
.write_to_disk(WriteStrategy::IfNotExists)?;
|
||||
WriteStrategy::IfNotExists.write(
|
||||
"exercises/README.md",
|
||||
include_bytes!("../exercises/README.md"),
|
||||
)?;
|
||||
|
||||
for file in self.exercises_dir.files {
|
||||
file.write_to_disk(WriteStrategy::IfNotExists)?;
|
||||
for dir in self.exercise_dirs {
|
||||
dir.init_on_disk()?;
|
||||
}
|
||||
|
||||
for dir in self.exercises_dir.dirs {
|
||||
dir.init_on_disk()?;
|
||||
|
||||
for file in dir.content {
|
||||
file.write_to_disk(WriteStrategy::IfNotExists)?;
|
||||
}
|
||||
for (exercise_info, exercise_files) in exercise_infos.iter().zip(self.exercise_files) {
|
||||
WriteStrategy::IfNotExists.write(&exercise_info.path(), exercise_files.exercise)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write_exercise_to_disk<P>(&self, path: P, strategy: WriteStrategy) -> io::Result<()>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let path = path.as_ref();
|
||||
pub fn write_exercise_to_disk(&self, exercise_ind: usize, dir_name: &str, path: &str) -> io::Result<()> {
|
||||
let Some(dir) = self.exercise_dirs.iter().find(|dir| dir.name == dir_name) else {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::NotFound,
|
||||
format!("`{dir_name}` not found in the embedded directories"),
|
||||
));
|
||||
};
|
||||
|
||||
if let Some(file) = self
|
||||
.exercises_dir
|
||||
.files
|
||||
.iter()
|
||||
.find(|file| Path::new(file.path) == path)
|
||||
{
|
||||
return file.write_to_disk(strategy);
|
||||
}
|
||||
|
||||
for dir in self.exercises_dir.dirs {
|
||||
if let Some(file) = dir.content.iter().find(|file| Path::new(file.path) == path) {
|
||||
dir.init_on_disk()?;
|
||||
return file.write_to_disk(strategy);
|
||||
}
|
||||
}
|
||||
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::NotFound,
|
||||
format!("{} not found in the embedded files", path.display()),
|
||||
))
|
||||
dir.init_on_disk()?;
|
||||
WriteStrategy::Overwrite.write(path, self.exercise_files[exercise_ind].exercise)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user