From cb9f1ac9ce3c834b0cca964b6809b74508f80b54 Mon Sep 17 00:00:00 2001 From: mo8it Date: Wed, 17 Apr 2024 22:46:21 +0200 Subject: [PATCH] Require a main function in all exercises --- exercises/03_if/if1.rs | 4 + exercises/03_if/if2.rs | 4 + exercises/03_if/if3.rs | 4 + .../04_primitive_types/primitive_types4.rs | 4 + .../04_primitive_types/primitive_types6.rs | 4 + exercises/05_vecs/vecs1.rs | 4 + exercises/05_vecs/vecs2.rs | 4 + exercises/07_structs/structs1.rs | 4 + exercises/07_structs/structs2.rs | 4 + exercises/07_structs/structs3.rs | 4 + exercises/08_enums/enums3.rs | 4 + exercises/09_strings/strings3.rs | 4 + exercises/11_hashmaps/hashmaps1.rs | 4 + exercises/11_hashmaps/hashmaps2.rs | 6 +- exercises/11_hashmaps/hashmaps3.rs | 10 ++- exercises/12_options/options1.rs | 4 + exercises/12_options/options2.rs | 4 + exercises/13_error_handling/errors1.rs | 4 + exercises/13_error_handling/errors2.rs | 4 + exercises/13_error_handling/errors4.rs | 4 + exercises/13_error_handling/errors6.rs | 4 + exercises/14_generics/generics2.rs | 4 + exercises/15_traits/traits2.rs | 4 + exercises/15_traits/traits3.rs | 4 + exercises/15_traits/traits4.rs | 4 + exercises/17_tests/tests1.rs | 4 + exercises/17_tests/tests2.rs | 4 + exercises/17_tests/tests3.rs | 4 + exercises/17_tests/tests4.rs | 8 +- exercises/18_iterators/iterators2.rs | 4 + exercises/18_iterators/iterators3.rs | 4 + exercises/18_iterators/iterators4.rs | 4 + exercises/18_iterators/iterators5.rs | 4 + exercises/19_smart_pointers/cow1.rs | 4 + exercises/23_conversions/as_ref_mut.rs | 4 + exercises/quiz1.rs | 4 + exercises/quiz2.rs | 4 + exercises/quiz3.rs | 10 ++- src/dev/check.rs | 73 ++++++++++--------- 39 files changed, 199 insertions(+), 44 deletions(-) diff --git a/exercises/03_if/if1.rs b/exercises/03_if/if1.rs index a1df66bb..dbd0d285 100644 --- a/exercises/03_if/if1.rs +++ b/exercises/03_if/if1.rs @@ -10,6 +10,10 @@ pub fn bigger(a: i32, b: i32) -> i32 { // - additional variables } +fn main() { + // You can optionally experiment here. +} + // Don't mind this for now :) #[cfg(test)] mod tests { diff --git a/exercises/03_if/if2.rs b/exercises/03_if/if2.rs index 7b9c05f6..a1ed5c8b 100644 --- a/exercises/03_if/if2.rs +++ b/exercises/03_if/if2.rs @@ -13,6 +13,10 @@ pub fn foo_if_fizz(fizzish: &str) -> &str { } } +fn main() { + // You can optionally experiment here. +} + // No test changes needed! #[cfg(test)] mod tests { diff --git a/exercises/03_if/if3.rs b/exercises/03_if/if3.rs index caba172b..0b44c5ab 100644 --- a/exercises/03_if/if3.rs +++ b/exercises/03_if/if3.rs @@ -27,6 +27,10 @@ pub fn animal_habitat(animal: &str) -> &'static str { habitat } +fn main() { + // You can optionally experiment here. +} + // No test changes needed. #[cfg(test)] mod tests { diff --git a/exercises/04_primitive_types/primitive_types4.rs b/exercises/04_primitive_types/primitive_types4.rs index 8ed0a82a..f99d8895 100644 --- a/exercises/04_primitive_types/primitive_types4.rs +++ b/exercises/04_primitive_types/primitive_types4.rs @@ -5,6 +5,10 @@ // Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand // for a hint. +fn main() { + // You can optionally experiment here. +} + #[test] fn slice_out_of_array() { let a = [1, 2, 3, 4, 5]; diff --git a/exercises/04_primitive_types/primitive_types6.rs b/exercises/04_primitive_types/primitive_types6.rs index 5f82f10f..48e84d34 100644 --- a/exercises/04_primitive_types/primitive_types6.rs +++ b/exercises/04_primitive_types/primitive_types6.rs @@ -6,6 +6,10 @@ // Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand // for a hint. +fn main() { + // You can optionally experiment here. +} + #[test] fn indexing_tuple() { let numbers = (1, 2, 3); diff --git a/exercises/05_vecs/vecs1.rs b/exercises/05_vecs/vecs1.rs index c64acbbd..5f44cb24 100644 --- a/exercises/05_vecs/vecs1.rs +++ b/exercises/05_vecs/vecs1.rs @@ -14,6 +14,10 @@ fn array_and_vec() -> ([i32; 4], Vec) { (a, v) } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/05_vecs/vecs2.rs b/exercises/05_vecs/vecs2.rs index d64d3d16..1b16f0b4 100644 --- a/exercises/05_vecs/vecs2.rs +++ b/exercises/05_vecs/vecs2.rs @@ -26,6 +26,10 @@ fn vec_map(v: &Vec) -> Vec { }).collect() } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/07_structs/structs1.rs b/exercises/07_structs/structs1.rs index 29781214..cd8b81c9 100644 --- a/exercises/07_structs/structs1.rs +++ b/exercises/07_structs/structs1.rs @@ -14,6 +14,10 @@ struct ColorTupleStruct(/* TODO: Something goes here */); #[derive(Debug)] struct UnitLikeStruct; +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/07_structs/structs2.rs b/exercises/07_structs/structs2.rs index a7a2deca..7e61e752 100644 --- a/exercises/07_structs/structs2.rs +++ b/exercises/07_structs/structs2.rs @@ -28,6 +28,10 @@ fn create_order_template() -> Order { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/07_structs/structs3.rs b/exercises/07_structs/structs3.rs index 9835b811..bd562a12 100644 --- a/exercises/07_structs/structs3.rs +++ b/exercises/07_structs/structs3.rs @@ -38,6 +38,10 @@ impl Package { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/08_enums/enums3.rs b/exercises/08_enums/enums3.rs index 580a553e..56c04fe6 100644 --- a/exercises/08_enums/enums3.rs +++ b/exercises/08_enums/enums3.rs @@ -45,6 +45,10 @@ impl State { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/09_strings/strings3.rs b/exercises/09_strings/strings3.rs index dedc081f..d53f654e 100644 --- a/exercises/09_strings/strings3.rs +++ b/exercises/09_strings/strings3.rs @@ -18,6 +18,10 @@ fn replace_me(input: &str) -> String { ??? } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/11_hashmaps/hashmaps1.rs b/exercises/11_hashmaps/hashmaps1.rs index 5a52f611..51146dfc 100644 --- a/exercises/11_hashmaps/hashmaps1.rs +++ b/exercises/11_hashmaps/hashmaps1.rs @@ -24,6 +24,10 @@ fn fruit_basket() -> HashMap { basket } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/11_hashmaps/hashmaps2.rs b/exercises/11_hashmaps/hashmaps2.rs index 27306439..47983f6b 100644 --- a/exercises/11_hashmaps/hashmaps2.rs +++ b/exercises/11_hashmaps/hashmaps2.rs @@ -41,6 +41,10 @@ fn fruit_basket(basket: &mut HashMap) { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; @@ -79,7 +83,7 @@ mod tests { let count = basket.values().sum::(); assert!(count > 11); } - + #[test] fn all_fruit_types_in_basket() { let mut basket = get_fruit_basket(); diff --git a/exercises/11_hashmaps/hashmaps3.rs b/exercises/11_hashmaps/hashmaps3.rs index 775a4014..33229096 100644 --- a/exercises/11_hashmaps/hashmaps3.rs +++ b/exercises/11_hashmaps/hashmaps3.rs @@ -5,9 +5,9 @@ // Example: England,France,4,2 (England scored 4 goals, France 2). // // You have to build a scores table containing the name of the team, the total -// number of goals the team scored, and the total number of goals the team -// conceded. One approach to build the scores table is to use a Hashmap. -// The solution is partially written to use a Hashmap, +// number of goals the team scored, and the total number of goals the team +// conceded. One approach to build the scores table is to use a Hashmap. +// The solution is partially written to use a Hashmap, // complete it to pass the test. // // Make me pass the tests! @@ -42,6 +42,10 @@ fn build_scores_table(results: String) -> HashMap { scores } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/12_options/options1.rs b/exercises/12_options/options1.rs index ba4b1cda..aecb123a 100644 --- a/exercises/12_options/options1.rs +++ b/exercises/12_options/options1.rs @@ -14,6 +14,10 @@ fn maybe_icecream(time_of_day: u16) -> Option { ??? } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/12_options/options2.rs b/exercises/12_options/options2.rs index 73f707e3..d183d1d5 100644 --- a/exercises/12_options/options2.rs +++ b/exercises/12_options/options2.rs @@ -3,6 +3,10 @@ // Execute `rustlings hint options2` or use the `hint` watch subcommand for a // hint. +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { #[test] diff --git a/exercises/13_error_handling/errors1.rs b/exercises/13_error_handling/errors1.rs index 9767f2c8..7991c420 100644 --- a/exercises/13_error_handling/errors1.rs +++ b/exercises/13_error_handling/errors1.rs @@ -9,6 +9,10 @@ // Execute `rustlings hint errors1` or use the `hint` watch subcommand for a // hint. +fn main() { + // You can optionally experiment here. +} + pub fn generate_nametag_text(name: String) -> Option { if name.is_empty() { // Empty names aren't allowed. diff --git a/exercises/13_error_handling/errors2.rs b/exercises/13_error_handling/errors2.rs index 88d1bf43..051516b9 100644 --- a/exercises/13_error_handling/errors2.rs +++ b/exercises/13_error_handling/errors2.rs @@ -29,6 +29,10 @@ pub fn total_cost(item_quantity: &str) -> Result { Ok(qty * cost_per_item + processing_fee) } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/13_error_handling/errors4.rs b/exercises/13_error_handling/errors4.rs index 0e5c08bf..94494170 100644 --- a/exercises/13_error_handling/errors4.rs +++ b/exercises/13_error_handling/errors4.rs @@ -19,6 +19,10 @@ impl PositiveNonzeroInteger { } } +fn main() { + // You can optionally experiment here. +} + #[test] fn test_creation() { assert!(PositiveNonzeroInteger::new(10).is_ok()); diff --git a/exercises/13_error_handling/errors6.rs b/exercises/13_error_handling/errors6.rs index de73a9a5..363a3b91 100644 --- a/exercises/13_error_handling/errors6.rs +++ b/exercises/13_error_handling/errors6.rs @@ -54,6 +54,10 @@ impl PositiveNonzeroInteger { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod test { use super::*; diff --git a/exercises/14_generics/generics2.rs b/exercises/14_generics/generics2.rs index d50ed174..068468ba 100644 --- a/exercises/14_generics/generics2.rs +++ b/exercises/14_generics/generics2.rs @@ -16,6 +16,10 @@ impl Wrapper { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/15_traits/traits2.rs b/exercises/15_traits/traits2.rs index 9a2bc07a..18ebcb08 100644 --- a/exercises/15_traits/traits2.rs +++ b/exercises/15_traits/traits2.rs @@ -14,6 +14,10 @@ trait AppendBar { // TODO: Implement trait `AppendBar` for a vector of strings. +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/15_traits/traits3.rs b/exercises/15_traits/traits3.rs index 357f1d7f..8412afa2 100644 --- a/exercises/15_traits/traits3.rs +++ b/exercises/15_traits/traits3.rs @@ -23,6 +23,10 @@ struct OtherSoftware { impl Licensed for SomeSoftware {} // Don't edit this line impl Licensed for OtherSoftware {} // Don't edit this line +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/15_traits/traits4.rs b/exercises/15_traits/traits4.rs index 7242c483..18db0d67 100644 --- a/exercises/15_traits/traits4.rs +++ b/exercises/15_traits/traits4.rs @@ -25,6 +25,10 @@ fn compare_license_types(software: ??, software_two: ??) -> bool { software.licensing_info() == software_two.licensing_info() } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/17_tests/tests1.rs b/exercises/17_tests/tests1.rs index bde21083..d32ace1e 100644 --- a/exercises/17_tests/tests1.rs +++ b/exercises/17_tests/tests1.rs @@ -10,6 +10,10 @@ // Execute `rustlings hint tests1` or use the `hint` watch subcommand for a // hint. +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { #[test] diff --git a/exercises/17_tests/tests2.rs b/exercises/17_tests/tests2.rs index aea5c0e4..501c44bc 100644 --- a/exercises/17_tests/tests2.rs +++ b/exercises/17_tests/tests2.rs @@ -6,6 +6,10 @@ // Execute `rustlings hint tests2` or use the `hint` watch subcommand for a // hint. +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { #[test] diff --git a/exercises/17_tests/tests3.rs b/exercises/17_tests/tests3.rs index d815e058..a2093cf2 100644 --- a/exercises/17_tests/tests3.rs +++ b/exercises/17_tests/tests3.rs @@ -11,6 +11,10 @@ pub fn is_even(num: i32) -> bool { num % 2 == 0 } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/17_tests/tests4.rs b/exercises/17_tests/tests4.rs index 0972a5b4..a50323cf 100644 --- a/exercises/17_tests/tests4.rs +++ b/exercises/17_tests/tests4.rs @@ -7,7 +7,7 @@ struct Rectangle { width: i32, - height: i32 + height: i32, } impl Rectangle { @@ -16,10 +16,14 @@ impl Rectangle { if width <= 0 || height <= 0 { panic!("Rectangle width and height cannot be negative!") } - Rectangle {width, height} + Rectangle { width, height } } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/18_iterators/iterators2.rs b/exercises/18_iterators/iterators2.rs index 4ca7742e..0ebd69a1 100644 --- a/exercises/18_iterators/iterators2.rs +++ b/exercises/18_iterators/iterators2.rs @@ -33,6 +33,10 @@ pub fn capitalize_words_string(words: &[&str]) -> String { String::new() } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/18_iterators/iterators3.rs b/exercises/18_iterators/iterators3.rs index f7da049c..3f5923cd 100644 --- a/exercises/18_iterators/iterators3.rs +++ b/exercises/18_iterators/iterators3.rs @@ -43,6 +43,10 @@ fn list_of_results() -> () { let division_results = numbers.into_iter().map(|n| divide(n, 27)); } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/18_iterators/iterators4.rs b/exercises/18_iterators/iterators4.rs index af3958c8..8fc87927 100644 --- a/exercises/18_iterators/iterators4.rs +++ b/exercises/18_iterators/iterators4.rs @@ -15,6 +15,10 @@ pub fn factorial(num: u64) -> u64 { // Execute `rustlings hint iterators4` for hints. } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/18_iterators/iterators5.rs b/exercises/18_iterators/iterators5.rs index ceec5369..2604004a 100644 --- a/exercises/18_iterators/iterators5.rs +++ b/exercises/18_iterators/iterators5.rs @@ -55,6 +55,10 @@ fn count_collection_iterator(collection: &[HashMap], value: Pr todo!(); } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/19_smart_pointers/cow1.rs b/exercises/19_smart_pointers/cow1.rs index b24591b7..51e5fdb4 100644 --- a/exercises/19_smart_pointers/cow1.rs +++ b/exercises/19_smart_pointers/cow1.rs @@ -25,6 +25,10 @@ fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> { input } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/23_conversions/as_ref_mut.rs b/exercises/23_conversions/as_ref_mut.rs index cd2c93be..6fb7c2fc 100644 --- a/exercises/23_conversions/as_ref_mut.rs +++ b/exercises/23_conversions/as_ref_mut.rs @@ -26,6 +26,10 @@ fn num_sq(arg: &mut T) { ??? } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/quiz1.rs b/exercises/quiz1.rs index b9e71f59..55bc61f6 100644 --- a/exercises/quiz1.rs +++ b/exercises/quiz1.rs @@ -16,6 +16,10 @@ // Put your function here! // fn calculate_price_of_apples { +fn main() { + // You can optionally experiment here. +} + // Don't modify this function! #[test] fn verify_test() { diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 8ace3fe0..1d73ab93 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -40,6 +40,10 @@ mod my_module { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { // TODO: What do we need to import to have `transformer` in scope? diff --git a/exercises/quiz3.rs b/exercises/quiz3.rs index 24f70829..780e130d 100644 --- a/exercises/quiz3.rs +++ b/exercises/quiz3.rs @@ -24,11 +24,17 @@ pub struct ReportCard { impl ReportCard { pub fn print(&self) -> String { - format!("{} ({}) - achieved a grade of {}", - &self.student_name, &self.student_age, &self.grade) + format!( + "{} ({}) - achieved a grade of {}", + &self.student_name, &self.student_age, &self.grade + ) } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/dev/check.rs b/src/dev/check.rs index 4688e044..d2e5fe14 100644 --- a/src/dev/check.rs +++ b/src/dev/check.rs @@ -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 Result Result, -) -> Result> { - 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) -> 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(