use std::{
env,
path::{Path, PathBuf},
time::{Duration, SystemTime},
};
use directories::ProjectDirs;
pub trait Cacher {
fn get_cache_dir() -> PathBuf {
env::var("CPP_LINTER_CACHE").map(PathBuf::from).unwrap_or(
ProjectDirs::from("", "cpp-linter", "cpp-linter")
.map(|d| d.cache_dir().to_path_buf())
.unwrap_or(PathBuf::from(".cpp-linter_cache")),
)
}
fn is_cache_valid(cache_file: &Path, max_age: Option<Duration>) -> bool {
let now = SystemTime::now();
cache_file.exists() && {
cache_file
.metadata()
.and_then(|metadata| metadata.modified())
.map(|modified_time| {
max_age.is_none_or(|age| {
now.duration_since(modified_time)
.is_ok_and(|duration| duration < age)
})
})
.unwrap_or(false)
}
}
}