From 09144621be16814a0460c60451a4599484ace3f3 Mon Sep 17 00:00:00 2001 From: Matthew Perry Date: Sat, 2 Dec 2023 04:44:52 -0700 Subject: [PATCH 1/2] convert only if the matcha file has been modified --- src/main.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4ee4644..26496f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,6 +59,21 @@ fn color_choice() -> ColorChoice { } } +fn requires_update(matcha_path: &std::path::Path) -> bool { + let matcha_meta = std::fs::metadata(matcha_path).unwrap(); + let matcha_time = matcha_meta.modified().unwrap(); + + let gleam_path = matcha_path.with_extension("gleam"); + let gleam_meta = std::fs::metadata(gleam_path).unwrap(); + let gleam_time = gleam_meta.modified().unwrap(); + + if let Ok(_duration) = matcha_time.duration_since(gleam_time) { + true + } else { + false + } +} + #[derive(Debug, StructOpt)] #[structopt(name = "matcha", about = "Compiles templates into Gleam modules")] struct Opt { @@ -86,10 +101,17 @@ fn main() { let path = entry.path(); if path.extension() == Some(std::ffi::OsStr::new("matcha")) { - if opt.verbose { - println!("Converting {}", path.display()); + if requires_update(path) { + if opt.verbose { + println!("Converting {}", path.display()); + } + Some(convert(NAME, path)) + } else { + if opt.verbose { + println!("Skipping {}, not modified", path.display()); + } + None } - Some(convert(NAME, path)) } else { None } From d2611d9be88f49e11d38660d3e83a9c3d97d50a7 Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Sun, 3 Dec 2023 19:50:17 +0000 Subject: [PATCH 2/2] Re-write requires_update logic to remove unwraps And to use a direct comparison instead of the duration_since method which I assume is ok. Otherwise, we just and_then the results and exit early if we're not getting access to the 'modified' times that we need to make the decision. We conservatively require an update if we fail to access the modified times. --- src/main.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 26496f3..111d08a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,18 +60,23 @@ fn color_choice() -> ColorChoice { } fn requires_update(matcha_path: &std::path::Path) -> bool { - let matcha_meta = std::fs::metadata(matcha_path).unwrap(); - let matcha_time = matcha_meta.modified().unwrap(); + let matcha_result = std::fs::metadata(matcha_path).and_then(|data| data.modified()); + + // If we fail to get the modified time for any reason than assume we have to update + let Ok(matcha_time) = matcha_result else { + return true; + }; let gleam_path = matcha_path.with_extension("gleam"); - let gleam_meta = std::fs::metadata(gleam_path).unwrap(); - let gleam_time = gleam_meta.modified().unwrap(); + let gleam_result = std::fs::metadata(gleam_path).and_then(|data| data.modified()); - if let Ok(_duration) = matcha_time.duration_since(gleam_time) { - true - } else { - false - } + // If we fail to get the modified time for any reason than assume we have to update + let Ok(gleam_time) = gleam_result else { + return true; + }; + + // Should update the gleam file if the matcha file is newer than it + matcha_time > gleam_time } #[derive(Debug, StructOpt)]