mirror of
https://github.com/watchexec/watchexec.git
synced 2024-11-13 07:41:11 +01:00
Add Handler trait
This commit is contained in:
parent
0bb38f40a5
commit
4e4a8e6853
2 changed files with 45 additions and 0 deletions
42
lib/src/handler.rs
Normal file
42
lib/src/handler.rs
Normal 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)
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue