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) {
Ok(date) => {
// If we already have a date, store its content before switching
if let Some(prev_date) = current_date {
if !content.is_empty() { if !content.is_empty() {
result.insert(date.unwrap(), content.clone()); result.insert(prev_date, content.clone());
content.clear(); content.clear();
} }
} else { }
// If parsing failed, just continue // Update the current date
current_date = Some(date);
}
Err(_) => {
eprintln!("Failed to parse date from line: {}", line); eprintln!("Failed to parse date from line: {}", line);
} }
}
} else { } else {
// Regular line -> belongs to the current date if any
if current_date.is_some() {
content.push(line.to_string()); 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,25 +101,23 @@ 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)| {
for (date, lines) in dates {
let mut current_header = (String::new(), String::new(), String::new()); let mut current_header = (String::new(), String::new(), String::new());
let mut current_subject = String::new(); let mut current_subject = String::new();
for line in lines { lines.into_iter().flat_map(move |line| {
let trimmed = line.trim(); let trimmed = line.trim();
if trimmed.is_empty() { if trimmed.is_empty() {
continue; return Vec::new();
} }
if is_time(trimmed) { if is_time(trimmed) {
current_header = parse_header(trimmed); current_header = parse_header(trimmed);
Vec::new()
} else if is_name(trimmed) { } else if is_name(trimmed) {
let names = extract_names(trimmed); extract_names(trimmed)
// Pre-allocate and extend instead of pushing individual items
let entries: Vec<Colle> = names
.into_iter() .into_iter()
.map(|name| Colle { .map(|name| Colle {
date: with_time(date, &current_header.0).unwrap_or_default(), date: with_time(date, &current_header.0).unwrap_or_default(),
@ -117,15 +132,14 @@ fn parse_upcoming(text: Vec<&str>) -> Vec<Colle> {
comment: None, comment: None,
attachments: None, attachments: None,
}) })
.collect(); .collect()
result.extend(entries);
} else { } else {
current_subject = trimmed.to_string(); current_subject = trimmed.to_string();
Vec::new()
} }
} })
} })
.collect()
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") {