mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-07-01 00:45:17 +02:00
Refactor embedded files to add solutions
This commit is contained in:
@ -11,11 +11,7 @@ use std::{
|
||||
process::{Command, Stdio},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
embedded::{WriteStrategy, EMBEDDED_FILES},
|
||||
exercise::Exercise,
|
||||
info_file::ExerciseInfo,
|
||||
};
|
||||
use crate::{embedded::EMBEDDED_FILES, exercise::Exercise, info_file::ExerciseInfo};
|
||||
|
||||
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
|
||||
const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";
|
||||
@ -100,10 +96,15 @@ impl AppState {
|
||||
|
||||
exercise_info.name.shrink_to_fit();
|
||||
let name = exercise_info.name.leak();
|
||||
let dir = exercise_info.dir.map(|mut dir| {
|
||||
dir.shrink_to_fit();
|
||||
&*dir.leak()
|
||||
});
|
||||
|
||||
let hint = exercise_info.hint.trim().to_owned();
|
||||
|
||||
Exercise {
|
||||
dir,
|
||||
name,
|
||||
path,
|
||||
mode: exercise_info.mode,
|
||||
@ -181,10 +182,16 @@ impl AppState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_path(&self, path: &str) -> Result<()> {
|
||||
fn reset(&self, ind: usize, dir_name: Option<&str>, path: &str) -> Result<()> {
|
||||
if self.official_exercises {
|
||||
return EMBEDDED_FILES
|
||||
.write_exercise_to_disk(path, WriteStrategy::Overwrite)
|
||||
.write_exercise_to_disk(
|
||||
ind,
|
||||
dir_name.context(
|
||||
"Official exercises must be nested in the `exercises` directory",
|
||||
)?,
|
||||
path,
|
||||
)
|
||||
.with_context(|| format!("Failed to reset the exercise {path}"));
|
||||
}
|
||||
|
||||
@ -209,11 +216,11 @@ impl AppState {
|
||||
}
|
||||
|
||||
pub fn reset_current_exercise(&mut self) -> Result<&'static str> {
|
||||
let path = self.current_exercise().path;
|
||||
self.set_pending(self.current_exercise_ind)?;
|
||||
self.reset_path(path)?;
|
||||
let exercise = self.current_exercise();
|
||||
self.reset(self.current_exercise_ind, exercise.dir, exercise.path)?;
|
||||
|
||||
Ok(path)
|
||||
Ok(exercise.path)
|
||||
}
|
||||
|
||||
pub fn reset_exercise_by_ind(&mut self, exercise_ind: usize) -> Result<&'static str> {
|
||||
@ -221,11 +228,11 @@ impl AppState {
|
||||
bail!(BAD_INDEX_ERR);
|
||||
}
|
||||
|
||||
let path = self.exercises[exercise_ind].path;
|
||||
self.set_pending(exercise_ind)?;
|
||||
self.reset_path(path)?;
|
||||
let exercise = &self.exercises[exercise_ind];
|
||||
self.reset(exercise_ind, exercise.dir, exercise.path)?;
|
||||
|
||||
Ok(path)
|
||||
Ok(exercise.path)
|
||||
}
|
||||
|
||||
fn next_pending_exercise_ind(&self) -> Option<usize> {
|
||||
|
110
src/embedded.rs
110
src/embedded.rs
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -31,6 +31,7 @@ impl<'a> Display for TerminalFileLink<'a> {
|
||||
}
|
||||
|
||||
pub struct Exercise {
|
||||
pub dir: Option<&'static str>,
|
||||
// Exercise's unique name
|
||||
pub name: &'static str,
|
||||
// Exercise's path
|
||||
|
@ -24,11 +24,11 @@ pub fn init() -> Result<()> {
|
||||
set_current_dir("rustlings")
|
||||
.context("Failed to change the current directory to `rustlings`")?;
|
||||
|
||||
let info_file = InfoFile::parse()?;
|
||||
EMBEDDED_FILES
|
||||
.init_exercises_dir()
|
||||
.init_exercises_dir(&info_file.exercises)
|
||||
.context("Failed to initialize the `rustlings/exercises` directory")?;
|
||||
|
||||
let info_file = InfoFile::parse()?;
|
||||
let current_cargo_toml = include_str!("../dev/Cargo.toml");
|
||||
// Skip the first line (comment).
|
||||
let newline_ind = current_cargo_toml
|
||||
|
Reference in New Issue
Block a user