use std::sync::{Arc, OnceLock};
pub trait MetricsObserver: Send + Sync {
fn increment(&self, metric: &'static str, amount: u64);
fn verbose(&self) -> bool;
}
pub trait EngineHooks: Send + Sync {
fn current_observer(&self) -> Option<Arc<dyn MetricsObserver>>;
fn has_matching_disable_directive(&self, source: &str, line: u32, rule_id: &str) -> bool;
}
static REGISTERED_HOOKS: OnceLock<&'static dyn EngineHooks> = OnceLock::new();
pub fn register_hooks(hooks: &'static dyn EngineHooks) {
let _ = REGISTERED_HOOKS.set(hooks);
}
pub fn current_observer() -> Option<Arc<dyn MetricsObserver>> {
current_observer_with(REGISTERED_HOOKS.get().copied())
}
pub fn has_matching_disable_directive(source: &str, line: u32, rule_id: &str) -> bool {
has_matching_disable_directive_with(REGISTERED_HOOKS.get().copied(), source, line, rule_id)
}
fn current_observer_with(hooks: Option<&dyn EngineHooks>) -> Option<Arc<dyn MetricsObserver>> {
hooks.and_then(EngineHooks::current_observer)
}
fn has_matching_disable_directive_with(
hooks: Option<&dyn EngineHooks>,
source: &str,
line: u32,
rule_id: &str,
) -> bool {
hooks.is_some_and(|hooks| hooks.has_matching_disable_directive(source, line, rule_id))
}
#[cfg(test)]
pub(crate) mod test_observer;
#[cfg(test)]
mod tests;