Add Handler trait

This commit is contained in:
Félix Saparelli 2021-08-19 20:30:01 +12:00
parent 0bb38f40a5
commit 4e4a8e6853
No known key found for this signature in database
GPG key ID: B948C4BAE44FC474
2 changed files with 45 additions and 0 deletions

42
lib/src/handler.rs Normal file
View file

@ -0,0 +1,42 @@
// it would be good to have an async handle here but it's not clear how to do it
/// A callable that can be used to hook into watchexec.
pub trait Handler<T> {
type Error: std::error::Error;
fn handle(&mut self, _data: T) -> Result<(), Self::Error>;
}
impl<F, T, E> Handler<T> for F
where
E: std::error::Error,
F: FnMut(T) -> Result<(), E> + Send + 'static,
{
type Error = E;
fn handle(&mut self, data: T) -> Result<(), Self::Error> {
(self)(data)
}
}
impl<T> Handler<T> for std::sync::mpsc::Sender<T>
where
T: Send,
{
type Error = std::sync::mpsc::SendError<T>;
fn handle(&mut self, data: T) -> Result<(), Self::Error> {
self.send(data)
}
}
impl<T> Handler<T> for tokio::sync::mpsc::Sender<T>
where
T: std::fmt::Debug,
{
type Error = tokio::sync::mpsc::error::TrySendError<T>;
fn handle(&mut self, data: T) -> Result<(), Self::Error> {
self.try_send(data)
}
}

View file

@ -24,11 +24,14 @@ pub mod signal;
// the core experience
mod config;
mod handler;
mod watchexec;
#[doc(inline)]
pub use config::Config;
#[doc(inline)]
pub use handler::Handler;
#[doc(inline)]
pub use watchexec::Watchexec;
// the *action* is debounced, not the events