fix: add debug
This commit is contained in:
parent
50655b0198
commit
44e480f4f7
2 changed files with 68 additions and 53 deletions
|
|
@ -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(¤t_year.to_string()) {
|
||||
let date = parse_french_date(line);
|
||||
if date.is_ok() {
|
||||
// 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(date.unwrap(), content.clone());
|
||||
result.insert(prev_date, content.clone());
|
||||
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);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 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,25 +101,23 @@ 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();
|
||||
|
||||
for (date, lines) in dates {
|
||||
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 line in lines {
|
||||
lines.into_iter().flat_map(move |line| {
|
||||
let trimmed = line.trim();
|
||||
if trimmed.is_empty() {
|
||||
continue;
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
if is_time(trimmed) {
|
||||
current_header = parse_header(trimmed);
|
||||
Vec::new()
|
||||
} else if is_name(trimmed) {
|
||||
let names = extract_names(trimmed);
|
||||
// Pre-allocate and extend instead of pushing individual items
|
||||
let entries: Vec<Colle> = names
|
||||
extract_names(trimmed)
|
||||
.into_iter()
|
||||
.map(|name| Colle {
|
||||
date: with_time(date, ¤t_header.0).unwrap_or_default(),
|
||||
|
|
@ -117,15 +132,14 @@ fn parse_upcoming(text: Vec<&str>) -> Vec<Colle> {
|
|||
comment: None,
|
||||
attachments: None,
|
||||
})
|
||||
.collect();
|
||||
result.extend(entries);
|
||||
.collect()
|
||||
} else {
|
||||
current_subject = trimmed.to_string();
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn fetch(session: &str, last_hash: &str) -> (Vec<Colle>, String) {
|
||||
|
|
|
|||
|
|
@ -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") {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue