fix: add debug

This commit is contained in:
Nathan 2025-09-12 20:34:01 +02:00
parent 50655b0198
commit 44e480f4f7
2 changed files with 68 additions and 53 deletions

View file

@ -10,29 +10,46 @@ use tempfile::NamedTempFile;
// Splits a vector of strings into a HashMap based on dates. // 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 // The keys are the dates, and the values are vectors of strings containing the lines after that
fn split_dates(text: Vec<&str>) -> HashMap<NaiveDate, Vec<String>> { fn split_dates(text: Vec<&str>) -> HashMap<NaiveDate, Vec<String>> {
// Get the current year
let current_year = chrono::Utc::now().year(); let current_year = chrono::Utc::now().year();
let mut result = HashMap::new(); let mut result = HashMap::new();
let mut content = Vec::new(); let mut current_date: Option<NaiveDate> = None;
let mut content: Vec<String> = Vec::new();
for line in text { for line in text {
// Check if the line contains a year
if line.contains(&current_year.to_string()) { if line.contains(&current_year.to_string()) {
let date = parse_french_date(line); // Found a potential date line
if date.is_ok() { match parse_french_date(line) {
if !content.is_empty() { Ok(date) => {
result.insert(date.unwrap(), content.clone()); // If we already have a date, store its content before switching
content.clear(); 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 { } 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 result
} }
@ -84,48 +101,45 @@ fn parse_header(line: &str) -> (String, String, String) {
// Parse the PDF text into a vector of Upcoming colles // Parse the PDF text into a vector of Upcoming colles
fn parse_upcoming(text: Vec<&str>) -> Vec<Colle> { fn parse_upcoming(text: Vec<&str>) -> Vec<Colle> {
let dates = split_dates(text); split_dates(text)
let mut result = Vec::new(); .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 { lines.into_iter().flat_map(move |line| {
let mut current_header = (String::new(), String::new(), String::new()); let trimmed = line.trim();
let mut current_subject = String::new(); if trimmed.is_empty() {
return Vec::new();
}
for line in lines { if is_time(trimmed) {
let trimmed = line.trim(); current_header = parse_header(trimmed);
if trimmed.is_empty() { Vec::new()
continue; } else if is_name(trimmed) {
} extract_names(trimmed)
.into_iter()
if is_time(trimmed) { .map(|name| Colle {
current_header = parse_header(trimmed); date: with_time(date, &current_header.0).unwrap_or_default(),
} else if is_name(trimmed) { examiner: current_header.1.clone(),
let names = extract_names(trimmed); room: current_header.2.clone(),
// Pre-allocate and extend instead of pushing individual items subject: current_subject.clone(),
let entries: Vec<Colle> = names student: name,
.into_iter() bjid: None,
.map(|name| Colle { bjsecret: None,
date: with_time(date, &current_header.0).unwrap_or_default(), grade: None,
examiner: current_header.1.clone(), content: None,
room: current_header.2.clone(), comment: None,
subject: current_subject.clone(), attachments: None,
student: name, })
bjid: None, .collect()
bjsecret: None, } else {
grade: None, current_subject = trimmed.to_string();
content: None, Vec::new()
comment: None, }
attachments: None, })
}) })
.collect(); .collect()
result.extend(entries);
} else {
current_subject = trimmed.to_string();
}
}
}
result
} }
pub async fn fetch(session: &str, last_hash: &str) -> (Vec<Colle>, String) { pub async fn fetch(session: &str, last_hash: &str) -> (Vec<Colle>, String) {

View file

@ -89,6 +89,7 @@ pub fn clean_content(content: &str) -> String {
pub fn with_time(date: NaiveDate, time_str: &str) -> Result<NaiveDateTime, String> { pub fn with_time(date: NaiveDate, time_str: &str) -> Result<NaiveDateTime, String> {
// Replace 'h' with ':' to make parsing easier // Replace 'h' with ':' to make parsing easier
let clean_time = time_str.replace('h', ":"); let clean_time = time_str.replace('h', ":");
println!("TIME: {}", time_str);
// Parse the time string into NaiveTime // Parse the time string into NaiveTime
match NaiveTime::parse_from_str(&clean_time, "%H:%M") { match NaiveTime::parse_from_str(&clean_time, "%H:%M") {