From 44e480f4f7b34cf3ace187855dc1c49832e4d6fc Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 12 Sep 2025 20:34:01 +0200 Subject: [PATCH] fix: add debug --- src/parser/colles/upcoming.rs | 120 +++++++++++++++++++--------------- src/parser/utils.rs | 1 + 2 files changed, 68 insertions(+), 53 deletions(-) diff --git a/src/parser/colles/upcoming.rs b/src/parser/colles/upcoming.rs index 762e850..bb6f150 100644 --- a/src/parser/colles/upcoming.rs +++ b/src/parser/colles/upcoming.rs @@ -10,29 +10,46 @@ use tempfile::NamedTempFile; // Splits a vector of strings into a HashMap based on dates. // The keys are the dates, and the values are vectors of strings containing the lines after that fn split_dates(text: Vec<&str>) -> HashMap> { - // Get the current year let current_year = chrono::Utc::now().year(); let mut result = HashMap::new(); - let mut content = Vec::new(); + let mut current_date: Option = None; + let mut content: Vec = Vec::new(); + for line in text { - // Check if the line contains a year if line.contains(¤t_year.to_string()) { - let date = parse_french_date(line); - if date.is_ok() { - if !content.is_empty() { - result.insert(date.unwrap(), content.clone()); - content.clear(); + // Found a potential date line + match parse_french_date(line) { + Ok(date) => { + // If we already have a date, store its content before switching + if let Some(prev_date) = current_date { + if !content.is_empty() { + result.insert(prev_date, content.clone()); + content.clear(); + } + } + // Update the current date + current_date = Some(date); + } + Err(_) => { + eprintln!("Failed to parse date from line: {}", line); } - } else { - // If parsing failed, just continue - eprintln!("Failed to parse date from line: {}", line); } } else { - content.push(line.to_string()); + // Regular line -> belongs to the current date if any + if current_date.is_some() { + content.push(line.to_string()); + } } } - // Return the result as a vector of strings + + // Flush the last group + if let Some(date) = current_date { + if !content.is_empty() { + result.insert(date, content); + } + } + result } @@ -84,48 +101,45 @@ fn parse_header(line: &str) -> (String, String, String) { // Parse the PDF text into a vector of Upcoming colles fn parse_upcoming(text: Vec<&str>) -> Vec { - let dates = split_dates(text); - let mut result = Vec::new(); + split_dates(text) + .into_iter() + .flat_map(|(date, lines)| { + let mut current_header = (String::new(), String::new(), String::new()); + let mut current_subject = String::new(); - for (date, lines) in dates { - let mut current_header = (String::new(), String::new(), String::new()); - let mut current_subject = String::new(); + lines.into_iter().flat_map(move |line| { + let trimmed = line.trim(); + if trimmed.is_empty() { + return Vec::new(); + } - for line in lines { - let trimmed = line.trim(); - if trimmed.is_empty() { - continue; - } - - if is_time(trimmed) { - current_header = parse_header(trimmed); - } else if is_name(trimmed) { - let names = extract_names(trimmed); - // Pre-allocate and extend instead of pushing individual items - let entries: Vec = names - .into_iter() - .map(|name| Colle { - date: with_time(date, ¤t_header.0).unwrap_or_default(), - examiner: current_header.1.clone(), - room: current_header.2.clone(), - subject: current_subject.clone(), - student: name, - bjid: None, - bjsecret: None, - grade: None, - content: None, - comment: None, - attachments: None, - }) - .collect(); - result.extend(entries); - } else { - current_subject = trimmed.to_string(); - } - } - } - - result + if is_time(trimmed) { + current_header = parse_header(trimmed); + Vec::new() + } else if is_name(trimmed) { + extract_names(trimmed) + .into_iter() + .map(|name| Colle { + date: with_time(date, ¤t_header.0).unwrap_or_default(), + examiner: current_header.1.clone(), + room: current_header.2.clone(), + subject: current_subject.clone(), + student: name, + bjid: None, + bjsecret: None, + grade: None, + content: None, + comment: None, + attachments: None, + }) + .collect() + } else { + current_subject = trimmed.to_string(); + Vec::new() + } + }) + }) + .collect() } pub async fn fetch(session: &str, last_hash: &str) -> (Vec, String) { diff --git a/src/parser/utils.rs b/src/parser/utils.rs index a85ecbd..e954e18 100644 --- a/src/parser/utils.rs +++ b/src/parser/utils.rs @@ -89,6 +89,7 @@ pub fn clean_content(content: &str) -> String { pub fn with_time(date: NaiveDate, time_str: &str) -> Result { // Replace 'h' with ':' to make parsing easier let clean_time = time_str.replace('h', ":"); + println!("TIME: {}", time_str); // Parse the time string into NaiveTime match NaiveTime::parse_from_str(&clean_time, "%H:%M") {