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.
// 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>> {
// 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<NaiveDate> = None;
let mut content: Vec<String> = Vec::new();
for line in text {
// Check if the line contains a year
if line.contains(&current_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<Colle> {
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<Colle> = names
.into_iter()
.map(|name| Colle {
date: with_time(date, &current_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, &current_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<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> {
// 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") {