mini-docs 0.7.0

A minimal, secure build-time Markdown to HTML generator for the mini-* family.
Documentation
use std::fs;
use std::path::Path;
use std::time::SystemTime;

use crate::error::DocError;
use crate::walk;

/// Returns `true` if `output_path` exists and is at least as new as both `md_mtime`
/// and `template_mtime` (the newest mtime across every file under the templates
/// dir, or `None` if there are no templates yet).
///
/// This is the render cache: the key is the pair `(md_mtime, template_mtime)`, and
/// the cached value is simply whatever is already sitting on disk at `output_path`.
/// No in-memory state is needed — and none would survive across separate process
/// invocations of a build anyway — the filesystem's own mtimes are the cache.
pub(crate) fn is_up_to_date(
    output_path: &Path,
    md_mtime: SystemTime,
    template_mtime: Option<SystemTime>,
) -> Result<bool, DocError> {
    let Ok(output_meta) = fs::metadata(output_path) else {
        return Ok(false);
    };
    let output_mtime = output_meta.modified()?;

    let newest_input = match template_mtime {
        Some(t) if t > md_mtime => t,
        _ => md_mtime,
    };

    Ok(output_mtime >= newest_input)
}

/// Returns the newest mtime across every file with `extension` under `dir`, or
/// `None` if there are no such files.
pub(crate) fn latest_mtime(dir: &Path, extension: &str) -> Result<Option<SystemTime>, DocError> {
    let mut latest: Option<SystemTime> = None;

    for path in walk::walk_files_with_extension(dir, extension)? {
        let mtime = fs::metadata(&path)?.modified()?;
        latest = Some(match latest {
            Some(l) if l > mtime => l,
            _ => mtime,
        });
    }

    Ok(latest)
}

#[cfg(test)]
#[path = "../tests/unit/cache.rs"]
mod tests;