1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2025-06-29 00:41:42 +02:00

Require a main function in all exercises

This commit is contained in:
mo8it
2024-04-17 22:46:21 +02:00
parent d83cc69afe
commit cb9f1ac9ce
39 changed files with 199 additions and 44 deletions

View File

@ -1,8 +1,9 @@
use anyhow::{bail, Context, Result};
use anyhow::{anyhow, bail, Context, Error, Result};
use std::{
cmp::Ordering,
fs::{self, read_dir},
path::PathBuf,
fs::{self, read_dir, OpenOptions},
io::Read,
path::{Path, PathBuf},
};
use crate::{
@ -18,6 +19,7 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<hashbrown::HashSet<
let mut names = hashbrown::HashSet::with_capacity(info_file.exercises.len());
let mut paths = hashbrown::HashSet::with_capacity(info_file.exercises.len());
let mut file_buf = String::with_capacity(1 << 14);
for exercise_info in &info_file.exercises {
if exercise_info.name.is_empty() {
bail!("Found an empty exercise name in `info.toml`");
@ -31,7 +33,10 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<hashbrown::HashSet<
if let Some(dir) = &exercise_info.dir {
if dir.is_empty() {
bail!("Found an empty dir name in `info.toml`");
bail!(
"The exercise `{}` has an empty dir name in `info.toml`",
exercise_info.name,
);
}
if let Some(c) = forbidden_char(dir) {
bail!("Char `{c}` in the exercise dir `{dir}` is not allowed");
@ -44,23 +49,37 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<hashbrown::HashSet<
if !names.insert(exercise_info.name.as_str()) {
bail!(
"The exercise name {} is duplicated. Exercise names must all be unique",
"The exercise name `{}` is duplicated. Exercise names must all be unique",
exercise_info.name,
);
}
paths.insert(PathBuf::from(exercise_info.path()));
let path = exercise_info.path();
OpenOptions::new()
.read(true)
.open(&path)
.with_context(|| format!("Failed to open the file {path}"))?
.read_to_string(&mut file_buf)
.with_context(|| format!("Failed to read the file {path}"))?;
if !file_buf.contains("fn main()") {
bail!("The `main` function is missing in the file `{path}`.\nCreate at least an empty `main` function to avoid language server errors");
}
file_buf.clear();
paths.insert(PathBuf::from(path));
}
Ok(paths)
}
fn check_exercise_dir_files(
info_file: &InfoFile,
info_file_paths: hashbrown::HashSet<PathBuf>,
) -> Result<hashbrown::HashSet<String>> {
let mut names = hashbrown::HashSet::with_capacity(info_file.exercises.len());
fn unexpected_file(path: &Path) -> Error {
anyhow!("Found the file `{}`. Only `README.md` and Rust files related to an exercise in `info.toml` are allowed in the `exercises` directory", path.display())
}
fn check_exercise_dir_files(info_file_paths: hashbrown::HashSet<PathBuf>) -> Result<()> {
for entry in read_dir("exercises").context("Failed to open the `exercises` directory")? {
let entry = entry.context("Failed to read the `exercises` directory")?;
@ -72,11 +91,9 @@ fn check_exercise_dir_files(
}
if !info_file_paths.contains(&path) {
bail!("`{}` is expected to be an exercise file corresponding to some exercise in `info.toml`", path.display());
return Err(unexpected_file(&path));
}
let file_name = file_name.to_string_lossy();
names.insert(file_name[..file_name.len() - 3].to_string());
continue;
}
@ -89,7 +106,7 @@ fn check_exercise_dir_files(
let path = entry.path();
if !entry.file_type().unwrap().is_file() {
bail!("Found {} but expected only files", path.display());
bail!("Found `{}` but expected only files. Only one level of exercise nesting is allowed", path.display());
}
let file_name = path.file_name().unwrap();
@ -98,21 +115,15 @@ fn check_exercise_dir_files(
}
if !info_file_paths.contains(&path) {
bail!("`{}` is expected to be an exercise file corresponding to some exercise in `info.toml`", path.display());
return Err(unexpected_file(&path));
}
// The file name must be valid Unicode with the `.rs` extension
// because it is part of the info file paths.
let file_name = file_name.to_string_lossy();
let file_name_without_rs_extension = file_name[..file_name.len() - 3].to_string();
names.insert(file_name_without_rs_extension);
}
}
Ok(names)
Ok(())
}
fn check_info_file(info_file: &InfoFile) -> Result<()> {
fn check_exercises(info_file: &InfoFile) -> Result<()> {
match info_file.format_version.cmp(&CURRENT_FORMAT_VERSION) {
Ordering::Less => bail!("`format_version` < {CURRENT_FORMAT_VERSION} (supported version)\nPlease migrate to the latest format version"),
Ordering::Greater => bail!("`format_version` > {CURRENT_FORMAT_VERSION} (supported version)\nTry updating the Rustlings program"),
@ -120,17 +131,7 @@ fn check_info_file(info_file: &InfoFile) -> Result<()> {
}
let info_file_paths = check_info_file_exercises(info_file)?;
let names_in_exercises_dir = check_exercise_dir_files(info_file, info_file_paths)?;
// Now, we know that every file has an exercise in `info.toml`.
// But we need to check that every exercise in `info.toml` has a file.
if names_in_exercises_dir.len() != info_file.exercises.len() {
for exercise_info in &info_file.exercises {
if !names_in_exercises_dir.contains(&exercise_info.name) {
bail!("The file `{}` is missing", exercise_info.path());
}
}
}
check_exercise_dir_files(info_file_paths)?;
Ok(())
}
@ -190,7 +191,7 @@ fn check_cargo_toml(
pub fn check() -> Result<()> {
let info_file = InfoFile::parse()?;
check_info_file(&info_file)?;
check_exercises(&info_file)?;
if DEVELOPING_OFFICIAL_RUSTLINGS {
check_cargo_toml(