1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2024-11-08 09:09:17 +01:00
rustlings/src/info_file.rs

95 lines
3.0 KiB
Rust
Raw Normal View History

2024-04-14 01:15:43 +02:00
use anyhow::{bail, Context, Error, Result};
use serde::Deserialize;
use std::{fs, io::ErrorKind};
use crate::embedded::EMBEDDED_FILES;
2024-05-13 22:02:45 +02:00
/// Deserialized from the `info.toml` file.
2024-04-14 01:15:43 +02:00
#[derive(Deserialize)]
pub struct ExerciseInfo {
2024-05-13 22:02:45 +02:00
/// Exercise's unique name.
2024-04-14 01:15:43 +02:00
pub name: String,
2024-05-13 22:02:45 +02:00
/// Exercise's directory name inside the `exercises/` directory.
2024-04-14 01:15:43 +02:00
pub dir: Option<String>,
#[serde(default = "default_true")]
2024-05-13 22:02:45 +02:00
/// Run `cargo test` on the exercise.
pub test: bool,
2024-05-13 22:02:45 +02:00
/// Deny all Clippy warnings.
#[serde(default)]
pub strict_clippy: bool,
2024-05-13 22:02:45 +02:00
/// The exercise's hint to be shown to the user on request.
2024-04-14 01:15:43 +02:00
pub hint: String,
}
2024-05-13 22:02:45 +02:00
#[inline(always)]
const fn default_true() -> bool {
true
}
2024-04-14 01:15:43 +02:00
impl ExerciseInfo {
2024-05-13 22:02:45 +02:00
/// Path to the exercise file starting with the `exercises/` directory.
2024-04-14 02:41:19 +02:00
pub fn path(&self) -> String {
2024-05-13 22:02:45 +02: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 01:15:43 +02:00
} else {
2024-05-13 22:02:45 +02: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 01:15:43 +02:00
}
}
2024-05-13 22:02:45 +02:00
/// The deserialized `info.toml` file.
2024-04-14 01:15:43 +02:00
#[derive(Deserialize)]
pub struct InfoFile {
2024-05-13 22:02:45 +02:00
/// For possible breaking changes in the future for third-party exercises.
2024-04-16 01:22:54 +02:00
pub format_version: u8,
2024-05-13 22:02:45 +02:00
/// Shown to users when starting with the exercises.
2024-04-14 01:15:43 +02:00
pub welcome_message: Option<String>,
2024-05-13 22:02:45 +02:00
/// Shown to users after finishing all exercises.
2024-04-14 01:15:43 +02:00
pub final_message: Option<String>,
2024-05-13 22:02:45 +02:00
/// List of all exercises.
2024-04-14 01:15:43 +02:00
pub exercises: Vec<ExerciseInfo>,
}
impl InfoFile {
2024-05-13 22:02:45 +02:00
/// Official exercises: Parse the embedded `info.toml` file.
/// Third-party exercises: Parse the `info.toml` file in the current directory.
2024-04-14 01:15:43 +02: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 01:15:43 +02: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 16:26:48 +02:00
.context("Failed to parse the embedded `info.toml` file");
2024-04-14 01:15:43 +02:00
}
return Err(Error::from(e).context("Failed to read the `info.toml` file"));
}
2024-04-14 01:15:43 +02: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.";