tikz-gallery-generator

Custum build of stapix for tikz.pablopie.xyz

Commit
983f58332fd7a0b8bb69938ed12a628779977374
Parent
1460d5928ca4b3340f70058e67e8de5d140f8e54
Author
Pablo <pablo-pie@riseup.net>
Date

Refactored the incremental build check

Diffstats

1 files changed, 28 insertions, 34 deletions

Status Name Changes Insertions Deletions
Modified src/main.rs 2 files changed 28 34
diff --git a/src/main.rs b/src/main.rs
@@ -1,7 +1,7 @@
 use crossterm::style::Stylize;
 use image::{DynamicImage, io::Reader as ImageReader};
 use std::{
-  cmp::min,
+  cmp,
   env,
   fmt::{self, Display},
   fs::{self, File},
@@ -122,7 +122,7 @@ fn render_gallery(
   }
 
   // ========================================================================
-  let num_threads = min(num_cpus::get() - 1, pics.len());
+  let num_threads = cmp::min(num_cpus::get() - 1, pics.len());
   let rendering_pool = ThreadPool::with_name(
     String::from("thumbnails renderer"),
     num_threads
@@ -134,18 +134,13 @@ fn render_gallery(
   let mut render_pool_count = 0;
   for pic in &pics {
     let thumb_path: PathBuf = ThumbPath(pic).into();
-    let thumb_meta = fs::metadata(&thumb_path);
-
-    if !full_build {
-      if let (Ok(thumb_m), Some(pic_m)) = (&thumb_meta, &pic.metadata) {
-        if thumb_m.modified().unwrap() > pic_m.modified().unwrap() {
-          warnln!(
-            "Skipped rendering the thumbnail for {name:?} (use {FULL_BUILD_OPT} to overwrite)",
-            name = pic.file_name
-          );
-          continue;
-        }
-      }
+
+    if !full_build && !needs_rebuild(pic, &thumb_path) {
+      warnln!(
+        "Skipped rendering the thumbnail for {:?} (use {FULL_BUILD_OPT} to overwrite)",
+        pic.file_name,
+      );
+      continue;
     }
 
     render_pool_count  += 1;
@@ -156,16 +151,14 @@ fn render_gallery(
     rendering_pool.execute(move || {
       sender.send(render_thumbnail(pic, &thumb_path))
         .expect("channel should still be alive awaiting for the completion of this task");
-      });
+    });
   }
 
   for _ in 0..render_pool_count {
     let msg = reciever.recv();
-    if msg.is_err() {
-      // propagate the panic to the main thread: reciever.recv should
-      // only fail if some of the rendering threads panicked
-      panic!("rendering thread panicked!");
-    }
+    // propagate the panic to the main thread: reciever.recv should
+    // only fail if some of the rendering threads panicked
+    if msg.is_err() { panic!("rendering thread panicked!"); }
     msg.unwrap()?;
   }
 
@@ -181,18 +174,8 @@ fn render_gallery(
     path.push(PAGES_PATH);
     path.push(pic.file_name.clone() + ".html");
 
-    // only try to re-render HTML page in case the page is older than the
-    // image file
-    // TODO: measure how useful this optimization really is
-    if !full_build {
-      if let (Ok(path_m), Some(pic_m)) = (fs::metadata(&path), &pic.metadata) {
-        if path_m.modified().unwrap() > pic_m.modified().unwrap() {
-          info!("Rendering HTML page for {name:?}...", name = pic.file_name);
-          continue;
-        }
-      }
-    }
-
+    if !full_build && !needs_rebuild(&pic, &path) { continue; }
+    info!("Rendering HTML page for {name:?}...", name = pic.file_name);
     render_pic_page(&pic, &path).map_err(|_| ())?;
   }
 
@@ -212,7 +195,7 @@ fn render_index(pics: &Vec) -> io::Result<()> {
   writeln!(f, "<title>{PAGE_TITLE}</title>")?;
   write_head(&mut f)?;
 
-  // Preload the first 2 pictures in the gallery
+  // preload the first 2 pictures in the gallery
   for pic in pics.iter().take(2) {
     writeln!(
       f,
@@ -485,7 +468,7 @@ fn render_thumbnail(pic: GalleryEntry, thumb_path: &Path) -> Result<(), ()> {
       let h = THUMB_HEIGHT;
       let w = (h * img.width()) / img.height();
 
-      // We should make sure that the image is in the RGBA8 format so that
+      // we should make sure that the image is in the RGBA8 format so that
       // the webp crate can encode it
       let img = DynamicImage::from(img.thumbnail(w, h).into_rgba8());
       let mem = webp::Encoder::from_image(&img)
@@ -503,6 +486,17 @@ fn render_thumbnail(pic: GalleryEntry, thumb_path: &Path) -> Result<(), ()> {
   Ok(())
 }
 
+fn needs_rebuild(pic: &GalleryEntry, dst: &Path) -> bool {
+  let dst_meta = fs::metadata(dst);
+  if let (Ok(dst_meta), Some(pic_meta)) = (&dst_meta, &pic.metadata) {
+    if dst_meta.modified().unwrap() > pic_meta.modified().unwrap() {
+      return false;
+    }
+  }
+
+  true
+}
+
 fn create_file(path: &Path) -> io::Result<File> {
   File::create(path)
     .map_err(|e| { errorln!("Could not open file {path:?}: {e}"); e })