use crate::cli::TimeRange;
use anyhow::Result;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
pub struct FileInfo {
pub path: PathBuf,
pub modified_date: String,
}
pub fn collect_files_with_dates<P, F>(
dir: P,
filter_fn: F,
time_range: TimeRange,
) -> Result<Vec<FileInfo>>
where
P: AsRef<Path>,
F: Fn(&Path) -> bool,
{
if !dir.as_ref().exists() {
return Ok(Vec::new());
}
let cutoff = time_range
.cutoff_date()
.map(|d| d.format("%Y-%m-%d").to_string());
let mut results = Vec::with_capacity(20);
for entry in WalkDir::new(dir).into_iter().filter_map(|e| e.ok()) {
if !entry.file_type().is_file() {
continue;
}
let path = entry.path();
if !filter_fn(path) {
continue;
}
let Ok(metadata) = entry.metadata() else {
continue;
};
if let Ok(modified) = metadata.modified() {
let datetime: chrono::DateTime<chrono::Local> = modified.into();
let date_key = datetime.format("%Y-%m-%d").to_string();
if let Some(ref cutoff_str) = cutoff {
if date_key.as_str() < cutoff_str.as_str() {
continue;
}
}
results.push(FileInfo {
path: path.to_path_buf(),
modified_date: date_key,
});
}
}
Ok(results)
}
pub fn is_json_file(path: &Path) -> bool {
if let Some(ext) = path.extension() {
ext == "jsonl" || ext == "json"
} else {
false
}
}
pub fn is_gemini_chat_file(path: &Path) -> bool {
if let (Some(parent), Some(ext)) = (path.parent(), path.extension()) {
parent.file_name() == Some(std::ffi::OsStr::new("chats")) && ext == "json"
} else {
false
}
}