diff --git a/src/info.rs b/src/info.rs index c6586cd..5713afd 100644 --- a/src/info.rs +++ b/src/info.rs @@ -1,7 +1,7 @@ use chrono::{DateTime, Duration, Utc}; use regex::{Captures, Regex}; use scraper::Selector; -use std::collections::HashMap; +use std::{collections::HashMap, sync::Arc}; use crate::utils::{get_semester, get_webpage, get_year}; @@ -75,7 +75,7 @@ fn anglophonization(date: &str) -> String { // New regex of all the french month let re = Regex::new(&format!( "({})", - dico.keys().cloned().collect::>().join("|") + dico.keys().cloned().collect::>().join("|") )) .unwrap(); diff --git a/src/timetable.rs b/src/timetable.rs index b7c0d2c..45be47a 100644 --- a/src/timetable.rs +++ b/src/timetable.rs @@ -1,10 +1,10 @@ use chrono::{Datelike, Duration, TimeZone, Utc}; use regex::Regex; use scraper::Selector; -use std::collections::HashMap; +use std::{collections::HashMap, sync::Arc}; use crate::utils::{ - self, fill_hours, get_semester, get_webpage, get_year, + self, get_hours, get_semester, get_webpage, get_year, models::{Position, TabChar}, Capitalize, }; @@ -17,7 +17,7 @@ pub async fn timetable( semester_opt: Option, year_opt: Option, user_agent: &str, -) -> (Vec, (usize, Vec)) { +) -> models::Timetable { let semester = get_semester(semester_opt); let year = get_year(year_opt, semester); @@ -36,8 +36,7 @@ pub async fn timetable( // Find the timetable let raw_timetable = document.select(&sel_table).next().unwrap(); - let mut schedules = Vec::new(); - fill_hours(&mut schedules); + let schedules = get_hours(); let mut timetable: Vec = Vec::new(); @@ -136,8 +135,8 @@ pub fn build(timetable: models::Timetable, dates: D) -> Vec { // h1 => heure de début | m1 => minute de début // h2 => heure de fin | m2 => minute de fin let re = Regex::new(r"(?P

\d{1,2})h(?P\d{2})-(?P

\d{1,2})h(?P\d{2})").unwrap(); - for hour in timetable.0 { - let captures = re.captures(&hour).unwrap(); + for hour in timetable.0.iter() { + let captures = re.captures(hour).unwrap(); let h1 = match captures.name("h1") { Some(h) => h.as_str().parse().unwrap(), @@ -216,7 +215,7 @@ pub fn build(timetable: models::Timetable, dates: D) -> Vec { } /// Display the timetable -pub fn display(timetable: (Vec, (usize, Vec)), cell_length: usize) { +pub fn display(timetable: (Arc<[String]>, (usize, Vec)), cell_length: usize) { // Cell length for hours let clh = 11; // Cell number @@ -242,7 +241,7 @@ pub fn display(timetable: (Vec, (usize, Vec)), cell_length: // Store the data of the course for utils::line_table let mut next_skip = HashMap::new(); // For each hours -- i the hour's number - for (i, hour) in timetable.0.into_iter().enumerate() { + for (i, hour) in timetable.0.iter().enumerate() { // Draw separator line utils::line_table(clh, cell_length, cn, Position::Middle, next_skip); diff --git a/src/timetable/models.rs b/src/timetable/models.rs index 4ad20c4..d55e705 100644 --- a/src/timetable/models.rs +++ b/src/timetable/models.rs @@ -56,7 +56,7 @@ pub struct Day { // Data builded in the timetable webpage pub type Timetable = ( // Schedules - Vec, + Arc<[String]>, // Timetable per days with the semester as the key (usize, Vec), ); diff --git a/src/utils.rs b/src/utils.rs index 2294491..aafbd23 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::{collections::HashMap, sync::Arc}; use chrono::{Datelike, Utc}; use scraper::Html; @@ -184,7 +184,8 @@ impl Capitalize for str { } } -pub fn fill_hours(hours: &mut Vec) { +pub fn get_hours() -> Arc<[String]> { + let mut hours = vec![]; for hour in 8..=20 { for minute in &[0, 15, 30, 45] { let hour_str = format!("{}h{:02}", hour, minute); @@ -197,12 +198,13 @@ pub fn fill_hours(hours: &mut Vec) { for _ in 0..4 { hours.pop(); } + + hours.into() } /// Names showed to the users pub fn get_selection(data: &(&Course, String)) -> String { - let mut hours = vec![]; - fill_hours(&mut hours); + let hours = get_hours(); format!( "{} - {} {}-{}",