next_web_utils/file/
watch.rs1use std::time::Duration;
2
3use notify::{EventHandler, RecommendedWatcher, Watcher as MyWatcher};
4
5pub struct Watcher(RecommendedWatcher);
6
7impl Watcher {
8 pub fn new<F: EventHandler>(event_handler: F) -> Result<Self, notify::Error> {
9 RecommendedWatcher::new(event_handler, Default::default())
10 .map(|watcher| Watcher(watcher))
11 .map_err(|e| e.into())
12 }
13
14 pub fn watch<P: AsRef<std::path::Path>>(
15 &mut self,
16 path: P,
17 recursive: bool,
18 ) -> Result<(), notify::Error> {
19 self.0.watch(
20 path.as_ref(),
21 if recursive {
22 notify::RecursiveMode::Recursive
23 } else {
24 notify::RecursiveMode::NonRecursive
25 },
26 )
27 }
28
29 pub fn unwatch<P: AsRef<std::path::Path>>(&mut self, path: P) -> Result<(), notify::Error> {
30 self.0.unwatch(path.as_ref())
31 }
32
33 pub fn configure(
34 &mut self,
35 poll_interval: Option<Duration>,
36 compare_contents: bool,
37 follow_symlinks: bool,
38 ) -> Result<bool, notify::Error> {
39 let config = notify::Config::default()
40 .with_compare_contents(compare_contents)
41 .with_follow_symlinks(follow_symlinks);
42 poll_interval.map(|c| config.with_poll_interval(c));
43 self.0.configure(config)
44 }
45}