Don't generate if source hasn't been modified

This commit is contained in:
Gwilym Kuiper 2021-04-20 21:07:57 +01:00
parent d1412fb709
commit b4c3bd7356

View file

@ -1,6 +1,9 @@
use std::fs;
use std::fs::File;
use std::io;
use std::io::BufWriter;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
mod colour;
mod image_loader;
@ -35,6 +38,15 @@ pub struct ImageConverterConfig {
}
pub fn convert_image(settings: &ImageConverterConfig) {
let source_modified_time =
get_modified_time(&settings.input_image).unwrap_or(SystemTime::now());
let target_modified_time =
get_modified_time(&settings.output_file).unwrap_or(SystemTime::UNIX_EPOCH);
if source_modified_time < target_modified_time {
return;
}
let image = Image::load_from_file(&settings.input_image);
let tile_size = settings.tile_size.to_size();
@ -82,3 +94,8 @@ fn optimiser_for_image(image: &Image, tile_size: usize) -> palette16::Palette16O
palette_optimiser
}
fn get_modified_time(path: impl AsRef<Path>) -> io::Result<SystemTime> {
let metadata = fs::metadata(path)?;
metadata.modified()
}