mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 15:26:29 +00:00
Optimize embedded dirs
This commit is contained in:
parent
da9f97b0e0
commit
d9df809838
3 changed files with 98 additions and 56 deletions
|
@ -25,14 +25,28 @@ pub fn include_files(_: TokenStream) -> TokenStream {
|
||||||
let solution_files = exercises
|
let solution_files = exercises
|
||||||
.iter()
|
.iter()
|
||||||
.map(|exercise| format!("../solutions/{}/{}.rs", exercise.dir, exercise.name));
|
.map(|exercise| format!("../solutions/{}/{}.rs", exercise.dir, exercise.name));
|
||||||
let dirs = exercises.iter().map(|exercise| &exercise.dir);
|
|
||||||
let readmes = exercises
|
let mut dirs = Vec::with_capacity(32);
|
||||||
|
let mut dir_inds = vec![0; exercises.len()];
|
||||||
|
|
||||||
|
for (exercise, dir_ind) in exercises.iter().zip(&mut dir_inds) {
|
||||||
|
// The directory is often the last one inserted.
|
||||||
|
if let Some(ind) = dirs.iter().rev().position(|dir| *dir == exercise.dir) {
|
||||||
|
*dir_ind = dirs.len() - 1 - ind;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
dirs.push(exercise.dir.as_str());
|
||||||
|
*dir_ind = dirs.len() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let readmes = dirs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|exercise| format!("../exercises/{}/README.md", exercise.dir));
|
.map(|dir| format!("../exercises/{dir}/README.md"));
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
EmbeddedFiles {
|
EmbeddedFiles {
|
||||||
exercise_files: &[#(ExerciseFiles { exercise: include_bytes!(#exercise_files), solution: include_bytes!(#solution_files) }),*],
|
exercise_files: &[#(ExerciseFiles { exercise: include_bytes!(#exercise_files), solution: include_bytes!(#solution_files), dir_ind: #dir_inds }),*],
|
||||||
exercise_dirs: &[#(ExerciseDir { name: #dirs, readme: include_bytes!(#readmes) }),*]
|
exercise_dirs: &[#(ExerciseDir { name: #dirs, readme: include_bytes!(#readmes) }),*]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -193,12 +193,12 @@ impl AppState {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_current_exercise_ind(&mut self, ind: usize) -> Result<()> {
|
pub fn set_current_exercise_ind(&mut self, exercise_ind: usize) -> Result<()> {
|
||||||
if ind >= self.exercises.len() {
|
if exercise_ind >= self.exercises.len() {
|
||||||
bail!(BAD_INDEX_ERR);
|
bail!(BAD_INDEX_ERR);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.current_exercise_ind = ind;
|
self.current_exercise_ind = exercise_ind;
|
||||||
|
|
||||||
self.write()
|
self.write()
|
||||||
}
|
}
|
||||||
|
@ -215,8 +215,11 @@ impl AppState {
|
||||||
self.write()
|
self.write()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_pending(&mut self, ind: usize) -> Result<()> {
|
pub fn set_pending(&mut self, exercise_ind: usize) -> Result<()> {
|
||||||
let exercise = self.exercises.get_mut(ind).context(BAD_INDEX_ERR)?;
|
let exercise = self
|
||||||
|
.exercises
|
||||||
|
.get_mut(exercise_ind)
|
||||||
|
.context(BAD_INDEX_ERR)?;
|
||||||
|
|
||||||
if exercise.done {
|
if exercise.done {
|
||||||
exercise.done = false;
|
exercise.done = false;
|
||||||
|
@ -229,16 +232,10 @@ impl AppState {
|
||||||
|
|
||||||
// Official exercises: Dump the original file from the binary.
|
// Official exercises: Dump the original file from the binary.
|
||||||
// Third-party exercises: Reset the exercise file with `git stash`.
|
// Third-party exercises: Reset the exercise file with `git stash`.
|
||||||
fn reset(&self, ind: usize, dir_name: Option<&str>, path: &str) -> Result<()> {
|
fn reset(&self, exercise_ind: usize, path: &str) -> Result<()> {
|
||||||
if self.official_exercises {
|
if self.official_exercises {
|
||||||
return EMBEDDED_FILES
|
return EMBEDDED_FILES
|
||||||
.write_exercise_to_disk(
|
.write_exercise_to_disk(exercise_ind, path)
|
||||||
ind,
|
|
||||||
dir_name.context(
|
|
||||||
"Official exercises must be nested in the `exercises` directory",
|
|
||||||
)?,
|
|
||||||
path,
|
|
||||||
)
|
|
||||||
.with_context(|| format!("Failed to reset the exercise {path}"));
|
.with_context(|| format!("Failed to reset the exercise {path}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,7 +262,7 @@ impl AppState {
|
||||||
pub fn reset_current_exercise(&mut self) -> Result<&'static str> {
|
pub fn reset_current_exercise(&mut self) -> Result<&'static str> {
|
||||||
self.set_pending(self.current_exercise_ind)?;
|
self.set_pending(self.current_exercise_ind)?;
|
||||||
let exercise = self.current_exercise();
|
let exercise = self.current_exercise();
|
||||||
self.reset(self.current_exercise_ind, exercise.dir, exercise.path)?;
|
self.reset(self.current_exercise_ind, exercise.path)?;
|
||||||
|
|
||||||
Ok(exercise.path)
|
Ok(exercise.path)
|
||||||
}
|
}
|
||||||
|
@ -277,7 +274,7 @@ impl AppState {
|
||||||
|
|
||||||
self.set_pending(exercise_ind)?;
|
self.set_pending(exercise_ind)?;
|
||||||
let exercise = &self.exercises[exercise_ind];
|
let exercise = &self.exercises[exercise_ind];
|
||||||
self.reset(exercise_ind, exercise.dir, exercise.path)?;
|
self.reset(exercise_ind, exercise.path)?;
|
||||||
|
|
||||||
Ok(exercise.path)
|
Ok(exercise.path)
|
||||||
}
|
}
|
||||||
|
@ -315,18 +312,9 @@ impl AppState {
|
||||||
let current_exercise = self.current_exercise();
|
let current_exercise = self.current_exercise();
|
||||||
|
|
||||||
if self.official_exercises {
|
if self.official_exercises {
|
||||||
let dir_name = current_exercise
|
EMBEDDED_FILES
|
||||||
.dir
|
.write_solution_to_disk(self.current_exercise_ind, current_exercise.name)
|
||||||
.context("Official exercises must be nested in the `exercises` directory")?;
|
.map(Some)
|
||||||
let solution_path = format!("solutions/{dir_name}/{}.rs", current_exercise.name);
|
|
||||||
|
|
||||||
EMBEDDED_FILES.write_solution_to_disk(
|
|
||||||
self.current_exercise_ind,
|
|
||||||
dir_name,
|
|
||||||
&solution_path,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(Some(solution_path))
|
|
||||||
} else {
|
} else {
|
||||||
let solution_path = if let Some(dir) = current_exercise.dir {
|
let solution_path = if let Some(dir) = current_exercise.dir {
|
||||||
format!("solutions/{dir}/{}.rs", current_exercise.name)
|
format!("solutions/{dir}/{}.rs", current_exercise.name)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use anyhow::{bail, Context, Error, Result};
|
use anyhow::{Context, Error, Result};
|
||||||
use std::{
|
use std::{
|
||||||
fs::{create_dir, create_dir_all, OpenOptions},
|
fs::{create_dir, create_dir_all, OpenOptions},
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
|
@ -34,6 +34,7 @@ impl WriteStrategy {
|
||||||
struct ExerciseFiles {
|
struct ExerciseFiles {
|
||||||
exercise: &'static [u8],
|
exercise: &'static [u8],
|
||||||
solution: &'static [u8],
|
solution: &'static [u8],
|
||||||
|
dir_ind: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ExerciseDir {
|
struct ExerciseDir {
|
||||||
|
@ -43,11 +44,10 @@ struct ExerciseDir {
|
||||||
|
|
||||||
impl ExerciseDir {
|
impl ExerciseDir {
|
||||||
fn init_on_disk(&self) -> Result<()> {
|
fn init_on_disk(&self) -> Result<()> {
|
||||||
let path_prefix = "exercises/";
|
// 20 = 10 + 10
|
||||||
let readme_path_postfix = "/README.md";
|
// exercises/ + /README.md
|
||||||
let mut dir_path =
|
let mut dir_path = String::with_capacity(20 + self.name.len());
|
||||||
String::with_capacity(path_prefix.len() + self.name.len() + readme_path_postfix.len());
|
dir_path.push_str("exercises/");
|
||||||
dir_path.push_str(path_prefix);
|
|
||||||
dir_path.push_str(self.name);
|
dir_path.push_str(self.name);
|
||||||
|
|
||||||
if let Err(e) = create_dir(&dir_path) {
|
if let Err(e) = create_dir(&dir_path) {
|
||||||
|
@ -60,10 +60,9 @@ impl ExerciseDir {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let readme_path = {
|
let mut readme_path = dir_path;
|
||||||
dir_path.push_str(readme_path_postfix);
|
readme_path.push_str("/README.md");
|
||||||
dir_path
|
|
||||||
};
|
|
||||||
WriteStrategy::Overwrite.write(&readme_path, self.readme)?;
|
WriteStrategy::Overwrite.write(&readme_path, self.readme)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -95,30 +94,71 @@ impl EmbeddedFiles {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_exercise_to_disk(
|
pub fn write_exercise_to_disk(&self, exercise_ind: usize, path: &str) -> Result<()> {
|
||||||
&self,
|
let exercise_files = &EMBEDDED_FILES.exercise_files[exercise_ind];
|
||||||
exercise_ind: usize,
|
let dir = &EMBEDDED_FILES.exercise_dirs[exercise_files.dir_ind];
|
||||||
dir_name: &str,
|
|
||||||
path: &str,
|
|
||||||
) -> Result<()> {
|
|
||||||
let Some(dir) = self.exercise_dirs.iter().find(|dir| dir.name == dir_name) else {
|
|
||||||
bail!("`{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, exercise_files.exercise)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write the solution file to disk and return its path.
|
||||||
pub fn write_solution_to_disk(
|
pub fn write_solution_to_disk(
|
||||||
&self,
|
&self,
|
||||||
exercise_ind: usize,
|
exercise_ind: usize,
|
||||||
dir_name: &str,
|
exercise_name: &str,
|
||||||
path: &str,
|
) -> Result<String> {
|
||||||
) -> Result<()> {
|
let exercise_files = &EMBEDDED_FILES.exercise_files[exercise_ind];
|
||||||
let dir_path = format!("solutions/{dir_name}");
|
let dir = &EMBEDDED_FILES.exercise_dirs[exercise_files.dir_ind];
|
||||||
|
|
||||||
|
// 14 = 10 + 1 + 3
|
||||||
|
// solutions/ + / + .rs
|
||||||
|
let mut dir_path = String::with_capacity(14 + dir.name.len() + exercise_name.len());
|
||||||
|
dir_path.push_str("solutions/");
|
||||||
|
dir_path.push_str(dir.name);
|
||||||
create_dir_all(&dir_path)
|
create_dir_all(&dir_path)
|
||||||
.with_context(|| format!("Failed to create the directory {dir_path}"))?;
|
.with_context(|| format!("Failed to create the directory {dir_path}"))?;
|
||||||
|
|
||||||
WriteStrategy::Overwrite.write(path, self.exercise_files[exercise_ind].solution)
|
let mut solution_path = dir_path;
|
||||||
|
solution_path.push('/');
|
||||||
|
solution_path.push_str(exercise_name);
|
||||||
|
solution_path.push_str(".rs");
|
||||||
|
|
||||||
|
WriteStrategy::Overwrite.write(&solution_path, exercise_files.solution)?;
|
||||||
|
|
||||||
|
Ok(solution_path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct ExerciseInfo {
|
||||||
|
dir: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct InfoFile {
|
||||||
|
exercises: Vec<ExerciseInfo>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dirs() {
|
||||||
|
let exercises = toml_edit::de::from_str::<InfoFile>(include_str!("../info.toml"))
|
||||||
|
.expect("Failed to parse `info.toml`")
|
||||||
|
.exercises;
|
||||||
|
|
||||||
|
assert_eq!(exercises.len(), EMBEDDED_FILES.exercise_files.len());
|
||||||
|
|
||||||
|
for (exercise, exercise_files) in exercises.iter().zip(EMBEDDED_FILES.exercise_files) {
|
||||||
|
assert_eq!(
|
||||||
|
exercise.dir,
|
||||||
|
EMBEDDED_FILES.exercise_dirs[exercise_files.dir_ind].name,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue