Allow processing for hints and messages in the info file

This commit is contained in:
mo8it
2026-08-01 21:21:01 +02:00
committed by Mo Bitar
parent 8560bac8e6
commit a93a10ac68
8 changed files with 44 additions and 52 deletions
+15 -17
View File
@@ -6,11 +6,11 @@ use crate::{embedded::EMBEDDED_FILES, exercise::RunnableExercise};
/// Deserialized from the `info.toml` file.
#[derive(Deserialize)]
pub struct ExerciseInfo {
pub struct ExerciseInfo<'a> {
/// Exercise's unique name.
pub name: &'static str,
pub name: &'a str,
/// Exercise's directory name inside the `exercises/` directory.
pub dir: Option<&'static str>,
pub dir: Option<&'a str>,
/// Run `cargo test` on the exercise.
#[serde(default = "default_true")]
pub test: bool,
@@ -18,7 +18,7 @@ pub struct ExerciseInfo {
#[serde(default)]
pub strict_clippy: bool,
/// The exercise's hint to be shown to the user on request.
pub hint: &'static str,
pub hint: String,
/// The exercise is already solved. Ignore it when checking that all exercises are unsolved.
#[serde(default)]
pub skip_check_unsolved: bool,
@@ -27,7 +27,7 @@ const fn default_true() -> bool {
true
}
impl ExerciseInfo {
impl ExerciseInfo<'_> {
/// Path to the exercise file starting with the `exercises/` directory.
pub fn path(&self) -> String {
let mut path = if let Some(dir) = self.dir {
@@ -53,7 +53,7 @@ impl ExerciseInfo {
}
}
impl RunnableExercise for ExerciseInfo {
impl RunnableExercise for ExerciseInfo<'_> {
fn name(&self) -> &str {
self.name
}
@@ -77,11 +77,14 @@ pub struct InfoFile {
/// For possible breaking changes in the future for community exercises.
pub format_version: u8,
/// Shown to users when starting with the exercises.
pub welcome_message: Option<&'static str>,
#[serde(default)]
pub welcome_message: String,
/// Shown to users after finishing all exercises.
pub final_message: Option<&'static str>,
#[serde(default)]
pub final_message: String,
/// List of all exercises.
pub exercises: Vec<ExerciseInfo>,
#[serde(borrow)]
pub exercises: Vec<ExerciseInfo<'static>>,
}
impl InfoFile {
@@ -89,15 +92,10 @@ impl InfoFile {
/// Community exercises: Parse the `info.toml` file in the current directory.
pub fn parse() -> Result<Self> {
// Read a local `info.toml` if it exists.
let slf = match fs::read("info.toml") {
let slf = match fs::read_to_string("info.toml") {
Ok(file_content) => {
// Remove `\r` on Windows.
// Leaking is fine since the info file is used until the end of the program.
let file_content =
String::from_utf8(file_content.into_iter().filter(|c| *c != b'\r').collect())
.context("Failed to parse `info.toml` as UTF8")?
.leak();
toml::de::from_str::<Self>(file_content)
// LEAKING: The info file is used until the end of the program.
toml::de::from_str::<Self>(file_content.leak())
.context("Failed to parse the `info.toml` file")?
}
Err(e) => {