solarized-emacs

a fork of Bozhidar Batsov's solarized-emacs
git clone https://git.trogloxene.org/solarized-emacs.git
Log | Files | Refs | README

rust.rs (1633B)


      1 use notify::{raw_watcher, PollWatcher, RecommendedWatcher, RecursiveMode};
      2 use std::path::PathBuf;
      3 use std::sync::mpsc::Sender;
      4 
      5 /// Thin wrapper over the notify crate
      6 ///
      7 /// `PollWatcher` and `RecommendedWatcher` are distinct types, but watchexec
      8 /// really just wants to handle them without regard to the exact type
      9 /// (e.g. polymorphically). This has the nice side effect of separating out
     10 /// all coupling to the notify crate into this module.
     11 pub struct Watcher {
     12     watcher_impl: WatcherImpl,
     13 }
     14 
     15 pub use notify::Error;
     16 pub use notify::RawEvent as Event;
     17 
     18 enum WatcherImpl {
     19     Recommended(RecommendedWatcher),
     20     Poll(PollWatcher),
     21 }
     22 
     23 impl Watcher {
     24     pub fn new(
     25         tx: Sender<Event>,
     26         paths: &[PathBuf],
     27         poll: bool,
     28         interval_ms: u32,
     29     ) -> Result<Self, Error> {
     30         use notify::Watcher;
     31 
     32         let imp = if poll {
     33             let mut watcher = PollWatcher::with_delay_ms(tx, interval_ms)?;
     34             for path in paths {
     35                 watcher.watch(path, RecursiveMode::Recursive)?;
     36                 debug!("Watching {:?}", path);
     37             }
     38 
     39             WatcherImpl::Poll(watcher)
     40         } else {
     41             let mut watcher = raw_watcher(tx)?;
     42             for path in paths {
     43                 watcher.watch(path, RecursiveMode::Recursive)?;
     44                 debug!("Watching {:?}", path);
     45             }
     46 
     47             WatcherImpl::Recommended(watcher)
     48         };
     49 
     50         Ok(Self { watcher_impl: imp })
     51     }
     52 
     53     pub fn is_polling(&self) -> bool {
     54         if let WatcherImpl::Poll(_) = self.watcher_impl {
     55             true
     56         } else {
     57             false
     58         }
     59     }
     60 }