Borrow deserialized values

This commit is contained in:
mo8it
2026-02-26 16:10:09 +01:00
parent 13564207cb
commit 0cbcb8964c
10 changed files with 57 additions and 54 deletions
+11 -10
View File
@@ -8,9 +8,9 @@ use crate::{embedded::EMBEDDED_FILES, exercise::RunnableExercise};
#[derive(Deserialize)]
pub struct ExerciseInfo {
/// Exercise's unique name.
pub name: String,
pub name: &'static str,
/// Exercise's directory name inside the `exercises/` directory.
pub dir: Option<String>,
pub dir: Option<&'static 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: String,
pub hint: &'static str,
/// The exercise is already solved. Ignore it when checking that all exercises are unsolved.
#[serde(default)]
pub skip_check_unsolved: bool,
@@ -31,7 +31,7 @@ const fn default_true() -> bool {
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 {
let mut path = if let Some(dir) = self.dir {
// 14 = 10 + 1 + 3
// exercises/ + / + .rs
let mut path = String::with_capacity(14 + dir.len() + self.name.len());
@@ -47,7 +47,7 @@ impl ExerciseInfo {
path
};
path.push_str(&self.name);
path.push_str(self.name);
path.push_str(".rs");
path
@@ -57,12 +57,12 @@ impl ExerciseInfo {
impl RunnableExercise for ExerciseInfo {
#[inline]
fn name(&self) -> &str {
&self.name
self.name
}
#[inline]
fn dir(&self) -> Option<&str> {
self.dir.as_deref()
self.dir
}
#[inline]
@@ -82,9 +82,9 @@ 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<String>,
pub welcome_message: Option<&'static str>,
/// Shown to users after finishing all exercises.
pub final_message: Option<String>,
pub final_message: Option<&'static str>,
/// List of all exercises.
pub exercises: Vec<ExerciseInfo>,
}
@@ -95,7 +95,8 @@ impl InfoFile {
pub fn parse() -> Result<Self> {
// Read a local `info.toml` if it exists.
let slf = match fs::read_to_string("info.toml") {
Ok(file_content) => toml::de::from_str::<Self>(&file_content)
// Leaking is fine since `InfoFile` is used until the end of the program.
Ok(file_content) => toml::de::from_str::<Self>(file_content.leak())
.context("Failed to parse the `info.toml` file")?,
Err(e) => {
if e.kind() == ErrorKind::NotFound {