[+] Generator: Cached results

This commit is contained in:
Azalea Gui
2023-02-23 11:53:50 -05:00
parent 8e2072cbed
commit 0f36380fd8
+16
View File
@@ -29,6 +29,8 @@ pub struct Generator {
pub trait GeneratorTraits {
fn new(base: PathBuf) -> Generator;
fn dot_path(&self, path: &PathBuf) -> PathBuf;
fn get_cached<T>(&self, file: &PathBuf, token: &str, read: impl Fn(&PathBuf) -> Result<T>,
gen: impl Fn(&PathBuf) -> Result<()>) -> Result<T>;
}
impl GeneratorTraits for Generator {
@@ -40,5 +42,19 @@ impl GeneratorTraits for Generator {
fn dot_path(&self, path: &PathBuf) -> PathBuf {
self.base.join(DOT_PATH).join(diff_paths(&path, &self.base).unwrap())
}
/// Get the cached result
fn get_cached<T>(&self, file: &PathBuf, token: &str, read: impl Fn(&PathBuf) -> Result<T>,
gen: impl Fn(&PathBuf) -> Result<()>) -> Result<T> {
let dot = self.dot_path(file).with_extension(token);
if ! dot.exists() || match (dot.metadata(), file.metadata()) {
(Ok(dm), Ok(fm)) => { fm.mtime() > dm.mtime() }
(_, _) => true
} {
debug!("Regenerating cached result {}", dot.display());
gen(&dot)?;
}
Ok(read(&dot)?)
}
}