use crate::workspace::Workspace;
use std::path::{Path, PathBuf};
pub fn find_most_recent_logfile(log_prefix: &Path, workspace: &dyn Workspace) -> Option<PathBuf> {
let parent = log_prefix.parent().unwrap_or_else(|| Path::new("."));
let prefix_str = log_prefix.file_name().and_then(|s| s.to_str())?;
workspace
.read_dir(parent)
.ok()
.and_then(|entries: Vec<crate::workspace::DirEntry>| {
entries
.into_iter()
.filter_map(|entry: crate::workspace::DirEntry| {
if !entry.is_file() {
return None;
}
let filename = entry.file_name().and_then(|s| s.to_str())?;
let has_log_ext = entry
.path()
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("log"));
let remainder = filename.strip_prefix(prefix_str)?;
if remainder.is_empty() || !has_log_ext {
return None;
}
entry
.modified()
.map(|modified| (entry.path().to_path_buf(), modified))
})
.max_by_key(|(_, time)| *time)
.map(|(path, _)| path)
})
}
pub fn read_most_recent_logfile(log_prefix: &Path, workspace: &dyn Workspace) -> String {
find_most_recent_logfile(log_prefix, workspace)
.and_then(|path| workspace.read(&path).ok())
.unwrap_or_default()
}