2024-03-28 19:34:48 +03:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
2024-04-23 20:18:25 +03:00
|
|
|
use serde::Deserialize;
|
2024-03-28 19:34:48 +03:00
|
|
|
|
2024-04-23 20:18:25 +03:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ExerciseInfo {
|
|
|
|
name: String,
|
|
|
|
dir: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct InfoFile {
|
|
|
|
exercises: Vec<ExerciseInfo>,
|
2024-03-28 19:34:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro]
|
|
|
|
pub fn include_files(_: TokenStream) -> TokenStream {
|
2024-05-13 02:25:38 +03:00
|
|
|
let info_file = include_str!("../info.toml");
|
|
|
|
let exercises = toml_edit::de::from_str::<InfoFile>(info_file)
|
2024-04-23 20:18:25 +03:00
|
|
|
.expect("Failed to parse `info.toml`")
|
|
|
|
.exercises;
|
|
|
|
|
|
|
|
let exercise_files = exercises
|
|
|
|
.iter()
|
|
|
|
.map(|exercise| format!("../exercises/{}/{}.rs", exercise.dir, exercise.name));
|
|
|
|
let solution_files = exercises
|
|
|
|
.iter()
|
|
|
|
.map(|exercise| format!("../solutions/{}/{}.rs", exercise.dir, exercise.name));
|
2024-05-12 18:40:53 +03:00
|
|
|
|
|
|
|
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
|
2024-04-23 20:18:25 +03:00
|
|
|
.iter()
|
2024-05-12 18:40:53 +03:00
|
|
|
.map(|dir| format!("../exercises/{dir}/README.md"));
|
2024-03-28 19:34:48 +03:00
|
|
|
|
|
|
|
quote! {
|
|
|
|
EmbeddedFiles {
|
2024-05-13 02:25:38 +03:00
|
|
|
info_file: #info_file,
|
2024-05-12 18:40:53 +03:00
|
|
|
exercise_files: &[#(ExerciseFiles { exercise: include_bytes!(#exercise_files), solution: include_bytes!(#solution_files), dir_ind: #dir_inds }),*],
|
2024-04-23 20:18:25 +03:00
|
|
|
exercise_dirs: &[#(ExerciseDir { name: #dirs, readme: include_bytes!(#readmes) }),*]
|
2024-03-28 19:34:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.into()
|
|
|
|
}
|