1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
use anyhow::{Context, Error, Result};
use std::{
fs::{self, create_dir},
io,
};
use crate::info_file::ExerciseInfo;
/// Contains all embedded files.
pub static EMBEDDED_FILES: EmbeddedFiles = rustlings_macros::include_files!();
// Files related to one exercise.
struct ExerciseFiles {
// The content of the exercise file.
exercise: &'static [u8],
// The content of the solution file.
solution: &'static [u8],
// Index of the related `ExerciseDir` in `EmbeddedFiles::exercise_dirs`.
dir_ind: usize,
}
fn create_dir_if_not_exists(path: &str) -> Result<()> {
if let Err(e) = create_dir(path) {
if e.kind() != io::ErrorKind::AlreadyExists {
return Err(Error::from(e).context(format!("Failed to create the directory {path}")));
}
}
Ok(())
}
// A directory in the `exercises/` directory.
pub struct ExerciseDir {
pub name: &'static str,
readme: &'static [u8],
}
impl ExerciseDir {
fn init_on_disk(&self) -> Result<()> {
// 20 = 10 + 10
// exercises/ + /README.md
let mut dir_path = String::with_capacity(20 + self.name.len());
dir_path.push_str("exercises/");
dir_path.push_str(self.name);
create_dir_if_not_exists(&dir_path)?;
let mut readme_path = dir_path;
readme_path.push_str("/README.md");
fs::write(&readme_path, self.readme)
.with_context(|| format!("Failed to write the file {readme_path}"))
}
}
/// All embedded files.
pub struct EmbeddedFiles {
/// The content of the `info.toml` file.
pub info_file: &'static str,
exercise_files: &'static [ExerciseFiles],
pub exercise_dirs: &'static [ExerciseDir],
}
impl EmbeddedFiles {
/// Dump all the embedded files of the `exercises/` directory.
pub fn init_exercises_dir(&self, exercise_infos: &[ExerciseInfo]) -> Result<()> {
create_dir("exercises").context("Failed to create the directory `exercises`")?;
fs::write(
"exercises/README.md",
include_bytes!("../exercises/README.md"),
)
.context("Failed to write the file exercises/README.md")?;
for dir in self.exercise_dirs {
dir.init_on_disk()?;
}
let mut exercise_path = String::with_capacity(64);
let prefix = "exercises/";
exercise_path.push_str(prefix);
for (exercise_info, exercise_files) in exercise_infos.iter().zip(self.exercise_files) {
let dir = &self.exercise_dirs[exercise_files.dir_ind];
exercise_path.truncate(prefix.len());
exercise_path.push_str(dir.name);
exercise_path.push('/');
exercise_path.push_str(&exercise_info.name);
exercise_path.push_str(".rs");
fs::write(&exercise_path, exercise_files.exercise)
.with_context(|| format!("Failed to write the exercise file {exercise_path}"))?;
}
Ok(())
}
pub fn write_exercise_to_disk(&self, exercise_ind: usize, path: &str) -> Result<()> {
let exercise_files = &self.exercise_files[exercise_ind];
let dir = &self.exercise_dirs[exercise_files.dir_ind];
dir.init_on_disk()?;
fs::write(path, exercise_files.exercise)
.with_context(|| format!("Failed to write the exercise file {path}"))
}
/// Write the solution file to disk and return its path.
pub fn write_solution_to_disk(
&self,
exercise_ind: usize,
exercise_name: &str,
) -> Result<String> {
create_dir_if_not_exists("solutions")?;
let exercise_files = &self.exercise_files[exercise_ind];
let dir = &self.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_if_not_exists(&dir_path)?;
let mut solution_path = dir_path;
solution_path.push('/');
solution_path.push_str(exercise_name);
solution_path.push_str(".rs");
fs::write(&solution_path, exercise_files.solution)
.with_context(|| format!("Failed to write the solution file {solution_path}"))?;
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::de::from_str::<InfoFile>(EMBEDDED_FILES.info_file)
.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,
);
}
}
}
|