2022-06-15 05:25:05 +02:00
|
|
|
//! The `Filterer` trait for event filtering.
|
2021-10-16 06:13:32 +02:00
|
|
|
|
2021-09-27 13:54:33 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-06-11 08:43:11 +02:00
|
|
|
use crate::{
|
|
|
|
error::RuntimeError,
|
|
|
|
event::{Event, Priority},
|
|
|
|
};
|
2021-09-22 13:39:41 +02:00
|
|
|
|
2021-10-16 12:14:57 +02:00
|
|
|
/// An interface for filtering events.
|
2021-09-29 17:03:46 +02:00
|
|
|
pub trait Filterer: std::fmt::Debug + Send + Sync {
|
2021-10-16 12:14:57 +02:00
|
|
|
/// Called on (almost) every event, and should return `false` if the event is to be discarded.
|
|
|
|
///
|
|
|
|
/// Checking whether an event passes a filter is synchronous, should be fast, and must not block
|
|
|
|
/// the thread. Do any expensive stuff upfront during construction of your filterer, or in a
|
|
|
|
/// separate thread/task, as needed.
|
|
|
|
///
|
|
|
|
/// Returning an error will also fail the event processing, but the error will be propagated to
|
|
|
|
/// the watchexec error handler. While the type signature supports any [`RuntimeError`], it's
|
|
|
|
/// preferred that you create your own error type and return it wrapped in the
|
|
|
|
/// [`RuntimeError::Filterer`] variant with the name of your filterer as `kind`.
|
2022-06-11 08:43:11 +02:00
|
|
|
fn check_event(&self, event: &Event, priority: Priority) -> Result<bool, RuntimeError>;
|
2021-09-22 13:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Filterer for () {
|
2022-06-11 08:43:11 +02:00
|
|
|
fn check_event(&self, _event: &Event, _priority: Priority) -> Result<bool, RuntimeError> {
|
2021-09-22 13:39:41 +02:00
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 13:54:33 +02:00
|
|
|
|
|
|
|
impl<T: Filterer> Filterer for Arc<T> {
|
2022-06-11 08:43:11 +02:00
|
|
|
fn check_event(&self, event: &Event, priority: Priority) -> Result<bool, RuntimeError> {
|
2023-01-06 14:53:49 +01:00
|
|
|
Self::as_ref(self).check_event(event, priority)
|
2021-09-27 13:54:33 +02:00
|
|
|
}
|
|
|
|
}
|