rustlings/src/embedded.rs

169 lines
4.8 KiB
Rust
Raw Normal View History

2024-05-12 11:40:53 -04:00
use anyhow::{Context, Error, Result};
2024-03-28 16:06:36 -04:00
use std::{
2024-05-25 12:19:30 -04:00
fs::{create_dir, OpenOptions},
2024-03-28 16:06:36 -04:00
io::{self, Write},
};
use crate::info_file::ExerciseInfo;
2024-05-13 15:40:40 -04:00
/// Contains all embedded files.
2024-03-28 16:06:36 -04:00
pub static EMBEDDED_FILES: EmbeddedFiles = rustlings_macros::include_files!();
#[derive(Clone, Copy)]
pub enum WriteStrategy {
IfNotExists,
Overwrite,
}
impl WriteStrategy {
2024-04-23 18:47:46 -04:00
fn write(self, path: &str, content: &[u8]) -> Result<()> {
let file = match self {
2024-03-28 16:06:36 -04: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-05-02 11:08:39 -04:00
file.with_context(|| format!("Failed to open the file `{path}` in write mode"))?
2024-04-23 18:47:46 -04:00
.write_all(content)
2024-05-02 11:08:39 -04:00
.with_context(|| format!("Failed to write the file {path}"))
2024-03-28 16:06:36 -04:00
}
}
2024-05-13 15:07:04 -04:00
// Files related to one exercise.
struct ExerciseFiles {
2024-05-13 15:07:04 -04:00
// The content of the exercise file.
exercise: &'static [u8],
2024-05-13 15:07:04 -04:00
// The content of the solution file.
solution: &'static [u8],
2024-05-13 15:07:04 -04:00
// Index of the related `ExerciseDir` in `EmbeddedFiles::exercise_dirs`.
2024-05-12 11:40:53 -04:00
dir_ind: usize,
2024-03-28 16:06:36 -04:00
}
2024-05-13 15:07:04 -04:00
// A directory in the `exercises/` directory.
2024-05-25 12:19:30 -04:00
pub struct ExerciseDir {
pub name: &'static str,
readme: &'static [u8],
2024-03-28 16:06:36 -04:00
}
impl ExerciseDir {
2024-04-23 18:47:46 -04:00
fn init_on_disk(&self) -> Result<()> {
2024-05-12 11:40:53 -04:00
// 20 = 10 + 10
// exercises/ + /README.md
let mut dir_path = String::with_capacity(20 + self.name.len());
dir_path.push_str("exercises/");
2024-04-23 19:17:39 -04:00
dir_path.push_str(self.name);
2024-04-23 18:47:46 -04:00
if let Err(e) = create_dir(&dir_path) {
if e.kind() == io::ErrorKind::AlreadyExists {
return Ok(());
2024-03-28 16:06:36 -04:00
}
2024-04-23 18:47:46 -04:00
return Err(
Error::from(e).context(format!("Failed to create the directory {dir_path}"))
);
2024-03-28 16:06:36 -04:00
}
2024-05-12 11:40:53 -04:00
let mut readme_path = dir_path;
readme_path.push_str("/README.md");
2024-05-13 15:07:04 -04:00
WriteStrategy::Overwrite.write(&readme_path, self.readme)
2024-03-28 16:06:36 -04:00
}
}
2024-05-13 15:40:40 -04:00
/// All embedded files.
2024-03-28 16:06:36 -04:00
pub struct EmbeddedFiles {
2024-05-13 15:40:40 -04:00
/// The content of the `info.toml` file.
pub info_file: &'static str,
exercise_files: &'static [ExerciseFiles],
2024-05-25 12:19:30 -04:00
pub exercise_dirs: &'static [ExerciseDir],
2024-03-28 16:06:36 -04:00
}
impl EmbeddedFiles {
2024-05-13 19:50:03 -04:00
/// Dump all the embedded files of the `exercises/` directory.
2024-04-23 18:47:46 -04:00
pub fn init_exercises_dir(&self, exercise_infos: &[ExerciseInfo]) -> Result<()> {
create_dir("exercises").context("Failed to create the directory `exercises`")?;
2024-03-28 17:11:16 -04:00
WriteStrategy::IfNotExists.write(
"exercises/README.md",
include_bytes!("../exercises/README.md"),
)?;
2024-03-28 17:11:16 -04:00
for dir in self.exercise_dirs {
2024-03-28 17:11:16 -04:00
dir.init_on_disk()?;
}
2024-03-28 17:11:16 -04: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-28 17:11:16 -04:00
}
Ok(())
2024-03-28 16:06:36 -04:00
}
2024-05-12 11:40:53 -04:00
pub fn write_exercise_to_disk(&self, exercise_ind: usize, path: &str) -> Result<()> {
2024-05-13 11:12:58 -04:00
let exercise_files = &self.exercise_files[exercise_ind];
let dir = &self.exercise_dirs[exercise_files.dir_ind];
2024-03-28 16:06:36 -04:00
dir.init_on_disk()?;
2024-05-12 11:40:53 -04:00
WriteStrategy::Overwrite.write(path, exercise_files.exercise)
2024-03-28 16:06:36 -04:00
}
2024-04-23 18:47:46 -04:00
2024-05-13 15:40:40 -04:00
/// Write the solution file to disk and return its path.
2024-04-23 18:47:46 -04:00
pub fn write_solution_to_disk(
&self,
exercise_ind: usize,
2024-05-12 11:40:53 -04:00
exercise_name: &str,
) -> Result<String> {
2024-05-13 11:12:58 -04:00
let exercise_files = &self.exercise_files[exercise_ind];
let dir = &self.exercise_dirs[exercise_files.dir_ind];
2024-05-12 11:40:53 -04:00
// 14 = 10 + 1 + 3
// solutions/ + / + .rs
2024-05-25 12:19:30 -04:00
let mut solution_path = String::with_capacity(14 + dir.name.len() + exercise_name.len());
solution_path.push_str("solutions/");
solution_path.push_str(dir.name);
2024-05-12 11:40:53 -04:00
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>(EMBEDDED_FILES.info_file)
2024-05-12 11:40:53 -04:00
.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,
);
}
2024-04-23 18:47:46 -04:00
}
2024-03-28 16:06:36 -04:00
}