rustlings/src/info_file.rs

136 lines
4 KiB
Rust
Raw Normal View History

2024-04-14 00:15:43 +01:00
use anyhow::{bail, Context, Error, Result};
use serde::Deserialize;
use std::{fs, io::ErrorKind};
use crate::{embedded::EMBEDDED_FILES, exercise::RunnableExercise};
2024-05-13 21:02:45 +01:00
/// Deserialized from the `info.toml` file.
2024-04-14 00:15:43 +01:00
#[derive(Deserialize)]
pub struct ExerciseInfo {
2024-05-13 21:02:45 +01:00
/// Exercise's unique name.
2024-04-14 00:15:43 +01:00
pub name: String,
2024-05-13 21:02:45 +01:00
/// Exercise's directory name inside the `exercises/` directory.
2024-04-14 00:15:43 +01:00
pub dir: Option<String>,
#[serde(default = "default_true")]
2024-05-13 21:02:45 +01:00
/// Run `cargo test` on the exercise.
pub test: bool,
2024-05-13 21:02:45 +01:00
/// Deny all Clippy warnings.
#[serde(default)]
pub strict_clippy: bool,
2024-05-13 21:02:45 +01:00
/// The exercise's hint to be shown to the user on request.
2024-04-14 00:15:43 +01:00
pub hint: String,
}
2024-05-13 21:02:45 +01:00
#[inline(always)]
const fn default_true() -> bool {
true
}
2024-04-14 00:15:43 +01:00
impl ExerciseInfo {
2024-05-13 21:02:45 +01:00
/// Path to the exercise file starting with the `exercises/` directory.
2024-04-14 01:41:19 +01:00
pub fn path(&self) -> String {
2024-05-13 21:02:45 +01:00
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());
path.push_str("exercises/");
path.push_str(dir);
path.push('/');
path
2024-04-14 00:15:43 +01:00
} else {
2024-05-13 21:02:45 +01:00
// 13 = 10 + 3
// exercises/ + .rs
let mut path = String::with_capacity(13 + self.name.len());
path.push_str("exercises/");
path
};
path.push_str(&self.name);
path.push_str(".rs");
path
2024-04-14 00:15:43 +01:00
}
2024-05-25 17:19:30 +01:00
/// Path to the solution file starting with the `solutions/` directory.
pub fn sol_path(&self) -> String {
let mut path = if let Some(dir) = &self.dir {
// 14 = 10 + 1 + 3
// solutions/ + / + .rs
let mut path = String::with_capacity(14 + dir.len() + self.name.len());
path.push_str("solutions/");
path.push_str(dir);
path.push('/');
path
} else {
// 13 = 10 + 3
// solutions/ + .rs
let mut path = String::with_capacity(13 + self.name.len());
path.push_str("solutions/");
path
};
path.push_str(&self.name);
path.push_str(".rs");
path
}
2024-04-14 00:15:43 +01:00
}
impl RunnableExercise for ExerciseInfo {
#[inline]
fn name(&self) -> &str {
&self.name
}
#[inline]
fn strict_clippy(&self) -> bool {
self.strict_clippy
}
#[inline]
fn test(&self) -> bool {
self.test
}
}
2024-05-13 21:02:45 +01:00
/// The deserialized `info.toml` file.
2024-04-14 00:15:43 +01:00
#[derive(Deserialize)]
pub struct InfoFile {
2024-05-13 21:02:45 +01:00
/// For possible breaking changes in the future for third-party exercises.
2024-04-16 00:22:54 +01:00
pub format_version: u8,
2024-05-13 21:02:45 +01:00
/// Shown to users when starting with the exercises.
2024-04-14 00:15:43 +01:00
pub welcome_message: Option<String>,
2024-05-13 21:02:45 +01:00
/// Shown to users after finishing all exercises.
2024-04-14 00:15:43 +01:00
pub final_message: Option<String>,
2024-05-13 21:02:45 +01:00
/// List of all exercises.
2024-04-14 00:15:43 +01:00
pub exercises: Vec<ExerciseInfo>,
}
impl InfoFile {
2024-05-13 21:02:45 +01:00
/// Official exercises: Parse the embedded `info.toml` file.
/// Third-party exercises: Parse the `info.toml` file in the current directory.
2024-04-14 00:15:43 +01:00
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_edit::de::from_str::<Self>(&file_content)
2024-04-14 00:15:43 +01:00
.context("Failed to parse the `info.toml` file")?,
Err(e) => {
if e.kind() == ErrorKind::NotFound {
return toml_edit::de::from_str(EMBEDDED_FILES.info_file)
2024-04-24 15:26:48 +01:00
.context("Failed to parse the embedded `info.toml` file");
2024-04-14 00:15:43 +01:00
}
return Err(Error::from(e).context("Failed to read the `info.toml` file"));
}
2024-04-14 00:15:43 +01:00
};
if slf.exercises.is_empty() {
bail!("{NO_EXERCISES_ERR}");
}
Ok(slf)
}
}
const NO_EXERCISES_ERR: &str = "There are no exercises yet!
If you are developing third-party exercises, add at least one exercise before testing.";