no-mistakes-postgres-engine 0.109.130

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
//! Inverted runtime hooks for `no-mistakes-postgres-engine`.

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();

/// Register process-wide PostgreSQL engine hooks (first registration wins).
///
/// The `no-mistakes` crate registers hooks at load time. Standalone consumers
/// that rely on invocation metrics or inline disable-directive suppression
/// should register hooks before calling engine entrypoints.
pub fn register_hooks(hooks: &'static dyn EngineHooks) {
    let _ = REGISTERED_HOOKS.set(hooks);
}

/// Return the active invocation metrics observer from registered hooks, if any.
pub fn current_observer() -> Option<Arc<dyn MetricsObserver>> {
    current_observer_with(REGISTERED_HOOKS.get().copied())
}

/// Check whether `source` suppresses `rule_id` at 1-based `line` via registered hooks.
///
/// Returns `false` when no hooks have been registered via [`register_hooks`].
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;