From 6bc3a5643cee69326cbff60fd5a49e2b82518d78 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 24 Jan 2024 09:28:43 +0100 Subject: [PATCH] option to not differentiate TD from TP (#5) --- src/filter.rs | 24 +++++++++++++++--------- src/main.rs | 6 +++++- src/utils.rs | 8 +++++++- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/filter.rs b/src/filter.rs index 5319afe..ea18499 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -4,12 +4,13 @@ use crate::timetable::models::Category; use crate::timetable::models::Timetable; use crate::utils::get_count; use crate::utils::get_entry; +use crate::utils::get_entry_nocat; use crate::utils::get_selection; const DISCLAIMER: &str = "(selection avec ESPACE, ENTRER pour valider)"; /// Filter the timetable -pub fn timetable(timetable: Timetable) -> Timetable { +pub fn timetable(timetable: Timetable, merge_td_tp: bool) -> Timetable { let mut my_timetable = timetable; /* Note on Cours/TD: @@ -21,7 +22,7 @@ pub fn timetable(timetable: Timetable) -> Timetable { choice(&mut my_timetable); courses(&mut my_timetable); - tdtp(&mut my_timetable); + tdtp(&mut my_timetable, merge_td_tp); my_timetable } @@ -65,12 +66,14 @@ fn choice(timetable: &mut Timetable) { /// Filter the multiple courses fn courses(timetable: &mut Timetable) { + let entry_getter = get_entry; + // List of courses and Counter of how much they appears // to know if multiples slots are available - let (mut courses, counts) = get_count(timetable, &[Category::Cours]); + let (mut courses, counts) = get_count(timetable, &[Category::Cours], entry_getter); // Keep only elements who have multiples slots - courses.retain(|course| *counts.get(&get_entry(course.0)).unwrap() > 1); + courses.retain(|course| *counts.get(&entry_getter(course.0)).unwrap() > 1); let mut multiselected: Vec = courses.iter().map(get_selection).collect(); multiselected.sort(); @@ -98,7 +101,7 @@ fn courses(timetable: &mut Timetable) { } // Keep if only one slot is available - if *counts.get(&get_entry(course)).unwrap() == 1 { + if *counts.get(&entry_getter(course)).unwrap() == 1 { return true; } @@ -116,13 +119,16 @@ fn courses(timetable: &mut Timetable) { } /// Filter the multiples TD/TP -fn tdtp(timetable: &mut Timetable) { +fn tdtp(timetable: &mut Timetable, merge: bool) { + // If we differentiate TD from TP + let entry_getter = if merge { get_entry_nocat } else { get_entry }; + // List of TP/TD and Counter of how much they appears // to know if multiples slots are available - let (mut td_or_tp, counts) = get_count(timetable, &[Category::TD, Category::TP]); + let (mut td_or_tp, counts) = get_count(timetable, &[Category::TD, Category::TP], entry_getter); // Keep only elements who have multiples TD/TP - td_or_tp.retain(|course| *counts.get(&get_entry(course.0)).unwrap() > 1); + td_or_tp.retain(|course| *counts.get(&entry_getter(course.0)).unwrap() > 1); let mut multiselected: Vec = td_or_tp.iter().map(get_selection).collect(); multiselected.sort(); @@ -148,7 +154,7 @@ fn tdtp(timetable: &mut Timetable) { } // Keep if only one slot is available of the TD/TP - if *counts.get(&get_entry(course)).unwrap() == 1 { + if *counts.get(&entry_getter(course)).unwrap() == 1 { return true; } diff --git a/src/main.rs b/src/main.rs index 73a6909..97a096f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,10 @@ struct Args { /// Size of cell of the timetable (irrelevant when exporting the timetable) #[clap(short, long, value_name = "CELL LENGTH", default_value_t = 35)] cl: usize, + + /// Doesn't distinguish TD from TP + #[clap(short, long)] + td_are_tp: bool, } #[tokio::main] @@ -52,7 +56,7 @@ async fn main() { println!("Récupération de l'emploi du temps des M{}...", level,); let mut timetable = timetable::timetable(level, args.semester, args.year, &user_agent).await; - timetable = filter::timetable(timetable); + timetable = filter::timetable(timetable, args.td_are_tp); println!("Récupération des informations par rapport à l'année..."); let info = info::info(level, args.semester, args.year, &user_agent).await; diff --git a/src/utils.rs b/src/utils.rs index ccf0352..a10b7db 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -224,10 +224,16 @@ pub fn get_entry(course: &Course) -> String { format!("{} - {:?}", course.name, course.category) } +/// Entry's name used for finding duplicates, ignoring categories +pub fn get_entry_nocat(course: &Course) -> String { + course.name.to_owned() +} + /// Returns a couple of (list of courses) and (a hashmap of how much they appears in the vector) pub fn get_count<'a>( timetable: &'a mut Timetable, allowed_list: &'a [Category], + getter: fn(&Course) -> String, ) -> (Vec<(&'a Course, String)>, HashMap) { // List of courses who will be courses let mut courses = vec![]; @@ -242,7 +248,7 @@ pub fn get_count<'a>( .any(|category| allowed_list.contains(category)) { courses.push((course, day.name.to_owned())); - let count = counts.entry(get_entry(course)).or_insert(0); + let count = counts.entry(getter(course)).or_insert(0); *count += 1; } }