From 0f36380fd8a433a87474ef4fd37af4a180d4932f Mon Sep 17 00:00:00 2001 From: Azalea Gui Date: Thu, 23 Feb 2023 11:53:50 -0500 Subject: [PATCH] [+] Generator: Cached results --- backend/src/generator.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/backend/src/generator.rs b/backend/src/generator.rs index 63b9772..f054102 100644 --- a/backend/src/generator.rs +++ b/backend/src/generator.rs @@ -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(&self, file: &PathBuf, token: &str, read: impl Fn(&PathBuf) -> Result, + gen: impl Fn(&PathBuf) -> Result<()>) -> Result; } 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(&self, file: &PathBuf, token: &str, read: impl Fn(&PathBuf) -> Result, + gen: impl Fn(&PathBuf) -> Result<()>) -> Result { + 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)?) + } }