replace Vec by Arc when possible

This commit is contained in:
Mylloon 2024-01-01 14:14:13 +01:00
parent 64ce6e478b
commit 5c922a530e
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 17 additions and 16 deletions

View file

@ -1,7 +1,7 @@
use chrono::{DateTime, Duration, Utc}; use chrono::{DateTime, Duration, Utc};
use regex::{Captures, Regex}; use regex::{Captures, Regex};
use scraper::Selector; use scraper::Selector;
use std::collections::HashMap; use std::{collections::HashMap, sync::Arc};
use crate::utils::{get_semester, get_webpage, get_year}; 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 // New regex of all the french month
let re = Regex::new(&format!( let re = Regex::new(&format!(
"({})", "({})",
dico.keys().cloned().collect::<Vec<_>>().join("|") dico.keys().cloned().collect::<Arc<[_]>>().join("|")
)) ))
.unwrap(); .unwrap();

View file

@ -1,10 +1,10 @@
use chrono::{Datelike, Duration, TimeZone, Utc}; use chrono::{Datelike, Duration, TimeZone, Utc};
use regex::Regex; use regex::Regex;
use scraper::Selector; use scraper::Selector;
use std::collections::HashMap; use std::{collections::HashMap, sync::Arc};
use crate::utils::{ use crate::utils::{
self, fill_hours, get_semester, get_webpage, get_year, self, get_hours, get_semester, get_webpage, get_year,
models::{Position, TabChar}, models::{Position, TabChar},
Capitalize, Capitalize,
}; };
@ -17,7 +17,7 @@ pub async fn timetable(
semester_opt: Option<i8>, semester_opt: Option<i8>,
year_opt: Option<i32>, year_opt: Option<i32>,
user_agent: &str, user_agent: &str,
) -> (Vec<String>, (usize, Vec<models::Day>)) { ) -> models::Timetable {
let semester = get_semester(semester_opt); let semester = get_semester(semester_opt);
let year = get_year(year_opt, semester); let year = get_year(year_opt, semester);
@ -36,8 +36,7 @@ pub async fn timetable(
// Find the timetable // Find the timetable
let raw_timetable = document.select(&sel_table).next().unwrap(); let raw_timetable = document.select(&sel_table).next().unwrap();
let mut schedules = Vec::new(); let schedules = get_hours();
fill_hours(&mut schedules);
let mut timetable: Vec<models::Day> = Vec::new(); let mut timetable: Vec<models::Day> = Vec::new();
@ -136,8 +135,8 @@ pub fn build(timetable: models::Timetable, dates: D) -> Vec<models::Course> {
// h1 => heure de début | m1 => minute de début // h1 => heure de début | m1 => minute de début
// h2 => heure de fin | m2 => minute de fin // h2 => heure de fin | m2 => minute de fin
let re = Regex::new(r"(?P<h1>\d{1,2})h(?P<m1>\d{2})-(?P<h2>\d{1,2})h(?P<m2>\d{2})").unwrap(); let re = Regex::new(r"(?P<h1>\d{1,2})h(?P<m1>\d{2})-(?P<h2>\d{1,2})h(?P<m2>\d{2})").unwrap();
for hour in timetable.0 { for hour in timetable.0.iter() {
let captures = re.captures(&hour).unwrap(); let captures = re.captures(hour).unwrap();
let h1 = match captures.name("h1") { let h1 = match captures.name("h1") {
Some(h) => h.as_str().parse().unwrap(), Some(h) => h.as_str().parse().unwrap(),
@ -216,7 +215,7 @@ pub fn build(timetable: models::Timetable, dates: D) -> Vec<models::Course> {
} }
/// Display the timetable /// Display the timetable
pub fn display(timetable: (Vec<String>, (usize, Vec<models::Day>)), cell_length: usize) { pub fn display(timetable: (Arc<[String]>, (usize, Vec<models::Day>)), cell_length: usize) {
// Cell length for hours // Cell length for hours
let clh = 11; let clh = 11;
// Cell number // Cell number
@ -242,7 +241,7 @@ pub fn display(timetable: (Vec<String>, (usize, Vec<models::Day>)), cell_length:
// Store the data of the course for utils::line_table // Store the data of the course for utils::line_table
let mut next_skip = HashMap::new(); let mut next_skip = HashMap::new();
// For each hours -- i the hour's number // 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 // Draw separator line
utils::line_table(clh, cell_length, cn, Position::Middle, next_skip); utils::line_table(clh, cell_length, cn, Position::Middle, next_skip);

View file

@ -56,7 +56,7 @@ pub struct Day {
// Data builded in the timetable webpage // Data builded in the timetable webpage
pub type Timetable = ( pub type Timetable = (
// Schedules // Schedules
Vec<String>, Arc<[String]>,
// Timetable per days with the semester as the key // Timetable per days with the semester as the key
(usize, Vec<Day>), (usize, Vec<Day>),
); );

View file

@ -1,4 +1,4 @@
use std::collections::HashMap; use std::{collections::HashMap, sync::Arc};
use chrono::{Datelike, Utc}; use chrono::{Datelike, Utc};
use scraper::Html; use scraper::Html;
@ -184,7 +184,8 @@ impl Capitalize for str {
} }
} }
pub fn fill_hours(hours: &mut Vec<String>) { pub fn get_hours() -> Arc<[String]> {
let mut hours = vec![];
for hour in 8..=20 { for hour in 8..=20 {
for minute in &[0, 15, 30, 45] { for minute in &[0, 15, 30, 45] {
let hour_str = format!("{}h{:02}", hour, minute); let hour_str = format!("{}h{:02}", hour, minute);
@ -197,12 +198,13 @@ pub fn fill_hours(hours: &mut Vec<String>) {
for _ in 0..4 { for _ in 0..4 {
hours.pop(); hours.pop();
} }
hours.into()
} }
/// Names showed to the users /// Names showed to the users
pub fn get_selection(data: &(&Course, String)) -> String { pub fn get_selection(data: &(&Course, String)) -> String {
let mut hours = vec![]; let hours = get_hours();
fill_hours(&mut hours);
format!( format!(
"{} - {} {}-{}", "{} - {} {}-{}",