@@ -13,7 +13,6 @@ use std::time::Duration;
1313use std:: time:: Instant ;
1414
1515use anyhow:: Context as _;
16- use async_trait:: async_trait;
1716use notify:: Event ;
1817use notify:: RecursiveMode ;
1918use notify:: Watcher as _;
@@ -30,28 +29,44 @@ use watchman_client::pdu::FileType;
3029use watchman_client:: pdu:: SubscribeRequest ;
3130use watchman_client:: pdu:: SyncTimeout ;
3231
33- #[ async_trait]
34- pub trait Watcher {
35- async fn wait ( & mut self ) -> anyhow:: Result < Vec < Event > > ;
32+ pub struct Watcher ( WatcherInner ) ;
33+
34+ enum WatcherInner {
35+ Watchman ( Watchman ) ,
36+ Notify ( NotifyWatcher ) ,
3637}
3738
38- pub struct NotifyWatcher {
39+ impl Watcher {
40+ pub async fn wait ( & mut self ) -> anyhow:: Result < Vec < Event > > {
41+ match & mut self . 0 {
42+ WatcherInner :: Watchman ( w) => w. wait ( ) . await ,
43+ WatcherInner :: Notify ( w) => w. wait ( ) . await ,
44+ }
45+ }
46+
47+ pub async fn watchman ( path : & Path ) -> anyhow:: Result < Self > {
48+ Ok ( Self ( WatcherInner :: Watchman ( Watchman :: new ( path) . await ?) ) )
49+ }
50+
51+ pub fn notify ( paths : & [ PathBuf ] ) -> anyhow:: Result < Self > {
52+ Ok ( Self ( WatcherInner :: Notify ( NotifyWatcher :: new ( paths) ?) ) )
53+ }
54+ }
55+
56+ struct NotifyWatcher {
3957 receiver : Receiver < notify:: Result < Event > > ,
4058}
4159
4260impl NotifyWatcher {
43- pub fn new ( paths : & [ PathBuf ] ) -> anyhow:: Result < Self > {
61+ fn new ( paths : & [ PathBuf ] ) -> anyhow:: Result < Self > {
4462 let ( sender, receiver) = channel ( ) ;
4563 let mut watcher = recommended_watcher ( sender) ?;
4664 for path in paths {
4765 watcher. watch ( path, RecursiveMode :: Recursive ) ?;
4866 }
4967 Ok ( Self { receiver } )
5068 }
51- }
5269
53- #[ async_trait]
54- impl Watcher for NotifyWatcher {
5570 async fn wait ( & mut self ) -> anyhow:: Result < Vec < Event > > {
5671 let mut res = Vec :: new ( ) ;
5772 res. push ( self . receiver . recv ( ) ??) ;
@@ -122,13 +137,12 @@ mod watchman_query {
122137 }
123138}
124139
125- pub struct Watchman {
140+ struct Watchman {
126141 root : PathBuf ,
127142 subscription : Subscription < watchman_query:: SubscriptionFields > ,
128143}
129144
130- #[ async_trait]
131- impl Watcher for Watchman {
145+ impl Watchman {
132146 async fn wait ( & mut self ) -> anyhow:: Result < Vec < Event > > {
133147 loop {
134148 match self
@@ -157,10 +171,8 @@ impl Watcher for Watchman {
157171 }
158172 }
159173 }
160- }
161174
162- impl Watchman {
163- pub async fn new ( path : & Path ) -> anyhow:: Result < Self > {
175+ async fn new ( path : & Path ) -> anyhow:: Result < Self > {
164176 let root = CanonicalPath :: canonicalize ( path) ?;
165177 let watchman_client = Connector :: new ( )
166178 . connect ( )
0 commit comments