I'm trying to implement a filesystem watcher in Rust. I can receive events when filesystem objects have changed but determining what change was made has me stumped. I found code on the latest released version of the Notify package here that takes me almost the whole way there.
How can I extract the path and type out of the event? The event is an enumerated type, yet somehow when it's printed, I see all the info I want.
I am obviously missing something very fundamental.
use notify::{watcher, RecursiveMode, Watcher};
use std::sync::mpsc::channel;
use std::time::Duration;
fn main() {
let (tx, rx) = channel();
let mut watcher = watcher(tx, Duration::from_secs(10)).unwrap();
watcher
.watch("/tmp/path", RecursiveMode::Recursive)
.unwrap();
loop {
match rx.recv() {
Ok(event) => {
// **>> event.filename? event.type? how?
println!("{:?}", event);
}
Err(e) => println!("watch error: {:?}", e),
}
}
}
Using a debounced watcher, the event you get is of type DebouncedEvent. The enum variant specifies the type, and its contents is the path(s). To get it out of the event, you should match on the event for the desired event types:
match &event {
Read(path) => {
// do thing
}
Rename(src, dest) => {
// do other thing
}
_ => () // don't care about other types
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With