mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-21 21:42:23 -05:00
Require a main function in all exercises
This commit is contained in:
parent
d83cc69afe
commit
cb9f1ac9ce
39 changed files with 199 additions and 44 deletions
|
@ -10,6 +10,10 @@ pub fn bigger(a: i32, b: i32) -> i32 {
|
||||||
// - additional variables
|
// - additional variables
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
// Don't mind this for now :)
|
// Don't mind this for now :)
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
|
@ -13,6 +13,10 @@ pub fn foo_if_fizz(fizzish: &str) -> &str {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
// No test changes needed!
|
// No test changes needed!
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
|
@ -27,6 +27,10 @@ pub fn animal_habitat(animal: &str) -> &'static str {
|
||||||
habitat
|
habitat
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
// No test changes needed.
|
// No test changes needed.
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
|
@ -5,6 +5,10 @@
|
||||||
// Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand
|
// Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand
|
||||||
// for a hint.
|
// for a hint.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn slice_out_of_array() {
|
fn slice_out_of_array() {
|
||||||
let a = [1, 2, 3, 4, 5];
|
let a = [1, 2, 3, 4, 5];
|
||||||
|
|
|
@ -6,6 +6,10 @@
|
||||||
// Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand
|
// Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand
|
||||||
// for a hint.
|
// for a hint.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn indexing_tuple() {
|
fn indexing_tuple() {
|
||||||
let numbers = (1, 2, 3);
|
let numbers = (1, 2, 3);
|
||||||
|
|
|
@ -14,6 +14,10 @@ fn array_and_vec() -> ([i32; 4], Vec<i32>) {
|
||||||
(a, v)
|
(a, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -26,6 +26,10 @@ fn vec_map(v: &Vec<i32>) -> Vec<i32> {
|
||||||
}).collect()
|
}).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -14,6 +14,10 @@ struct ColorTupleStruct(/* TODO: Something goes here */);
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct UnitLikeStruct;
|
struct UnitLikeStruct;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -28,6 +28,10 @@ fn create_order_template() -> Order {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -38,6 +38,10 @@ impl Package {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -45,6 +45,10 @@ impl State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -18,6 +18,10 @@ fn replace_me(input: &str) -> String {
|
||||||
???
|
???
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -24,6 +24,10 @@ fn fruit_basket() -> HashMap<String, u32> {
|
||||||
basket
|
basket
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -41,6 +41,10 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -79,7 +83,7 @@ mod tests {
|
||||||
let count = basket.values().sum::<u32>();
|
let count = basket.values().sum::<u32>();
|
||||||
assert!(count > 11);
|
assert!(count > 11);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn all_fruit_types_in_basket() {
|
fn all_fruit_types_in_basket() {
|
||||||
let mut basket = get_fruit_basket();
|
let mut basket = get_fruit_basket();
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
// Example: England,France,4,2 (England scored 4 goals, France 2).
|
// 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
|
// 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
|
// 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.
|
// conceded. One approach to build the scores table is to use a Hashmap.
|
||||||
// The solution is partially written to use a Hashmap,
|
// The solution is partially written to use a Hashmap,
|
||||||
// complete it to pass the test.
|
// complete it to pass the test.
|
||||||
//
|
//
|
||||||
// Make me pass the tests!
|
// Make me pass the tests!
|
||||||
|
@ -42,6 +42,10 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
|
||||||
scores
|
scores
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -14,6 +14,10 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
|
||||||
???
|
???
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -9,6 +9,10 @@
|
||||||
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
pub fn generate_nametag_text(name: String) -> Option<String> {
|
pub fn generate_nametag_text(name: String) -> Option<String> {
|
||||||
if name.is_empty() {
|
if name.is_empty() {
|
||||||
// Empty names aren't allowed.
|
// Empty names aren't allowed.
|
||||||
|
|
|
@ -29,6 +29,10 @@ pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||||
Ok(qty * cost_per_item + processing_fee)
|
Ok(qty * cost_per_item + processing_fee)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -19,6 +19,10 @@ impl PositiveNonzeroInteger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_creation() {
|
fn test_creation() {
|
||||||
assert!(PositiveNonzeroInteger::new(10).is_ok());
|
assert!(PositiveNonzeroInteger::new(10).is_ok());
|
||||||
|
|
|
@ -54,6 +54,10 @@ impl PositiveNonzeroInteger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -16,6 +16,10 @@ impl Wrapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -14,6 +14,10 @@ trait AppendBar {
|
||||||
|
|
||||||
// TODO: Implement trait `AppendBar` for a vector of strings.
|
// TODO: Implement trait `AppendBar` for a vector of strings.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -23,6 +23,10 @@ struct OtherSoftware {
|
||||||
impl Licensed for SomeSoftware {} // Don't edit this line
|
impl Licensed for SomeSoftware {} // Don't edit this line
|
||||||
impl Licensed for OtherSoftware {} // Don't edit this line
|
impl Licensed for OtherSoftware {} // Don't edit this line
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -25,6 +25,10 @@ fn compare_license_types(software: ??, software_two: ??) -> bool {
|
||||||
software.licensing_info() == software_two.licensing_info()
|
software.licensing_info() == software_two.licensing_info()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -10,6 +10,10 @@
|
||||||
// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -6,6 +6,10 @@
|
||||||
// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -11,6 +11,10 @@ pub fn is_even(num: i32) -> bool {
|
||||||
num % 2 == 0
|
num % 2 == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
struct Rectangle {
|
struct Rectangle {
|
||||||
width: i32,
|
width: i32,
|
||||||
height: i32
|
height: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rectangle {
|
impl Rectangle {
|
||||||
|
@ -16,10 +16,14 @@ impl Rectangle {
|
||||||
if width <= 0 || height <= 0 {
|
if width <= 0 || height <= 0 {
|
||||||
panic!("Rectangle width and height cannot be negative!")
|
panic!("Rectangle width and height cannot be negative!")
|
||||||
}
|
}
|
||||||
Rectangle {width, height}
|
Rectangle { width, height }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -33,6 +33,10 @@ pub fn capitalize_words_string(words: &[&str]) -> String {
|
||||||
String::new()
|
String::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -43,6 +43,10 @@ fn list_of_results() -> () {
|
||||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -15,6 +15,10 @@ pub fn factorial(num: u64) -> u64 {
|
||||||
// Execute `rustlings hint iterators4` for hints.
|
// Execute `rustlings hint iterators4` for hints.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -55,6 +55,10 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -25,6 +25,10 @@ fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
|
||||||
input
|
input
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -26,6 +26,10 @@ fn num_sq<T>(arg: &mut T) {
|
||||||
???
|
???
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -16,6 +16,10 @@
|
||||||
// Put your function here!
|
// Put your function here!
|
||||||
// fn calculate_price_of_apples {
|
// fn calculate_price_of_apples {
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
// Don't modify this function!
|
// Don't modify this function!
|
||||||
#[test]
|
#[test]
|
||||||
fn verify_test() {
|
fn verify_test() {
|
||||||
|
|
|
@ -40,6 +40,10 @@ mod my_module {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
// TODO: What do we need to import to have `transformer` in scope?
|
// TODO: What do we need to import to have `transformer` in scope?
|
||||||
|
|
|
@ -24,11 +24,17 @@ pub struct ReportCard {
|
||||||
|
|
||||||
impl ReportCard {
|
impl ReportCard {
|
||||||
pub fn print(&self) -> String {
|
pub fn print(&self) -> String {
|
||||||
format!("{} ({}) - achieved a grade of {}",
|
format!(
|
||||||
&self.student_name, &self.student_age, &self.grade)
|
"{} ({}) - achieved a grade of {}",
|
||||||
|
&self.student_name, &self.student_age, &self.grade
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{anyhow, bail, Context, Error, Result};
|
||||||
use std::{
|
use std::{
|
||||||
cmp::Ordering,
|
cmp::Ordering,
|
||||||
fs::{self, read_dir},
|
fs::{self, read_dir, OpenOptions},
|
||||||
path::PathBuf,
|
io::Read,
|
||||||
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
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 names = hashbrown::HashSet::with_capacity(info_file.exercises.len());
|
||||||
let mut paths = 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 {
|
for exercise_info in &info_file.exercises {
|
||||||
if exercise_info.name.is_empty() {
|
if exercise_info.name.is_empty() {
|
||||||
bail!("Found an empty exercise name in `info.toml`");
|
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 let Some(dir) = &exercise_info.dir {
|
||||||
if dir.is_empty() {
|
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) {
|
if let Some(c) = forbidden_char(dir) {
|
||||||
bail!("Char `{c}` in the exercise dir `{dir}` is not allowed");
|
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()) {
|
if !names.insert(exercise_info.name.as_str()) {
|
||||||
bail!(
|
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,
|
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)
|
Ok(paths)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_exercise_dir_files(
|
fn unexpected_file(path: &Path) -> Error {
|
||||||
info_file: &InfoFile,
|
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())
|
||||||
info_file_paths: hashbrown::HashSet<PathBuf>,
|
}
|
||||||
) -> Result<hashbrown::HashSet<String>> {
|
|
||||||
let mut names = hashbrown::HashSet::with_capacity(info_file.exercises.len());
|
|
||||||
|
|
||||||
|
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")? {
|
for entry in read_dir("exercises").context("Failed to open the `exercises` directory")? {
|
||||||
let entry = entry.context("Failed to read 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) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +106,7 @@ fn check_exercise_dir_files(
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
|
|
||||||
if !entry.file_type().unwrap().is_file() {
|
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();
|
let file_name = path.file_name().unwrap();
|
||||||
|
@ -98,21 +115,15 @@ fn check_exercise_dir_files(
|
||||||
}
|
}
|
||||||
|
|
||||||
if !info_file_paths.contains(&path) {
|
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) {
|
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::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"),
|
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 info_file_paths = check_info_file_exercises(info_file)?;
|
||||||
let names_in_exercises_dir = check_exercise_dir_files(info_file, info_file_paths)?;
|
check_exercise_dir_files(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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -190,7 +191,7 @@ fn check_cargo_toml(
|
||||||
|
|
||||||
pub fn check() -> Result<()> {
|
pub fn check() -> Result<()> {
|
||||||
let info_file = InfoFile::parse()?;
|
let info_file = InfoFile::parse()?;
|
||||||
check_info_file(&info_file)?;
|
check_exercises(&info_file)?;
|
||||||
|
|
||||||
if DEVELOPING_OFFICIAL_RUSTLINGS {
|
if DEVELOPING_OFFICIAL_RUSTLINGS {
|
||||||
check_cargo_toml(
|
check_cargo_toml(
|
||||||
|
|
Loading…
Reference in a new issue