File watching

Watch files or directories and execute a function when they change

notify-website notify notify-crates.io notify-github notify-lib.rs cat-filesystem

notify is a cross-platform filesystem notification library.


use std::path::Path;

use notify::EventHandler;
use notify::RecursiveMode;
use notify::Result;
use notify::Watcher;
use notify::event::Event;

/// Prints received events
struct EventPrinter;

impl EventHandler for EventPrinter {
    fn handle_event(&mut self, res_event: Result<Event>) {
        match res_event {
            Ok(event) => println!("event: {:?}", event),
            Err(e) => println!("watch error: {:?}", e),
        }
    }
}

fn main() -> Result<()> {
    // Automatically select the best implementation for your platform.
    let mut watcher = notify::recommended_watcher(EventPrinter)?;

    // Add a path to be watched. All files and directories at that path
    // and below will be monitored for changes.
    watcher.watch(Path::new("."), RecursiveMode::Recursive)?;

    Ok(())
}