use notify::RecursiveMode;
use rolldown_error::BuildResult;
use std::path::Path;
use crate::{EventHandler, PathsMut, WatcherConfig};
pub trait Watcher {
fn new<F: EventHandler>(event_handler: F) -> BuildResult<Self>
where
Self: Sized;
fn with_config<F: EventHandler>(event_handler: F, config: WatcherConfig) -> BuildResult<Self>
where
Self: Sized;
fn watch(&mut self, path: &Path, recursive_mode: RecursiveMode) -> BuildResult<()>;
fn unwatch(&mut self, path: &Path) -> BuildResult<()>;
fn paths_mut<'me>(&'me mut self) -> Box<dyn PathsMut + 'me> {
struct DefaultPathsMut<'a, T: ?Sized>(&'a mut T);
impl<T: Watcher + ?Sized> PathsMut for DefaultPathsMut<'_, T> {
fn add(&mut self, path: &Path, recursive_mode: RecursiveMode) -> BuildResult<()> {
self.0.watch(path, recursive_mode)
}
fn remove(&mut self, path: &Path) -> BuildResult<()> {
self.0.unwatch(path)
}
fn commit(self: Box<Self>) -> BuildResult<()> {
Ok(())
}
}
Box::new(DefaultPathsMut(self))
}
}