Skip to main content

Module watch

Module watch 

Source
Expand description

File-watching for live rebuild. File-watching module for the static site generator.

Provides a polling-based file watcher that monitors a content directory for changes and triggers rebuilds when modifications are detected. Uses only std library types — no external dependencies required.

§Architecture

The watcher tracks file modification times in a HashMap and compares them on each poll cycle. Three kinds of changes are detected:

  • Modified — a file’s mtime has advanced since the last snapshot.
  • Added — a file exists on disk but was not present in the snapshot.
  • Removed — a file was in the snapshot but is no longer on disk.

§Example

use std::path::PathBuf;
use std::time::Duration;
use ssg::watch::{FileWatcher, WatchConfig};

let config = WatchConfig::new(
    PathBuf::from("content"),
    Duration::from_secs(2),
);

let mut watcher = FileWatcher::new(config).expect("failed to create watcher");

// Non-blocking: check once and get changed paths.
let changes = watcher.check_for_changes().expect("check failed");
if !changes.is_empty() {
    println!("Changed files: {:?}", changes);
}

Structs§

FileWatcher
A polling-based file watcher that tracks modification times.
WatchConfig
Configuration for the file watcher.

Enums§

ChangeKind
Categorises a file change for selective reload.

Constants§

MAX_WATCH_ITERATIONS
Enters a blocking poll loop that invokes callback whenever file changes are detected.

Functions§

classify_change
Classifies a changed file path for selective reload.
watch_blocking
Polls for file changes in a blocking loop, invoking callback with changed paths.