use crate::error::{Error, Result};
use notify::{Config as NotifyConfig, RecommendedWatcher, RecursiveMode, Watcher};
use std::path::PathBuf;
use std::sync::mpsc::Receiver;
pub(super) fn create_watcher(
watch_dirs: &[PathBuf],
) -> Result<(Receiver<notify::Result<notify::Event>>, RecommendedWatcher)> {
let (tx, rx) = std::sync::mpsc::channel::<notify::Result<notify::Event>>();
let mut watcher = RecommendedWatcher::new(tx, NotifyConfig::default()).map_err(|e| {
Error::Io(std::io::Error::other(format!(
"watch: failed to create watcher: {e}"
)))
})?;
for dir in watch_dirs {
watcher.watch(dir, RecursiveMode::Recursive).map_err(|e| {
Error::Io(std::io::Error::other(format!(
"watch: failed to watch {}: {e}",
dir.display()
)))
})?;
}
Ok((rx, watcher))
}