Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/edda

crates/jera/src/jobs/selftest.rs

//! # Functional-status emit bridge
//!
//! A tiny seam the ledger (and the boot runtime) use to push a *functional* row
//! into a consumer's test matrix. jera is standalone, so by default [`emit`] is an
//! `#[inline]` no-op — nornir installs its `nornir-testmatrix` bridge via
//! [`set_emit`], and korp can leave it a no-op.

use std::sync::OnceLock;

/// The aspect tag a self-emitted row carries.
pub const ASPECT_FUNCTIONAL: &str = "functional";

/// The emit-hook signature: `(component, check, ok, detail)`.
pub type EmitFn = fn(&str, &str, bool, &str);

static HOOK: OnceLock<EmitFn> = OnceLock::new();

/// Install the process-wide functional-status hook (call once at startup). nornir
/// passes `nornir_testmatrix::functional_status`; korp needs nothing. Later calls
/// are ignored (first wins), so a lib can't stomp the app's choice.
pub fn set_emit(hook: EmitFn) {
    let _ = HOOK.set(hook);
}

/// Push ONE functional-status row for a single real check. Routes to the installed
/// [`set_emit`] hook, else a no-op.
#[inline]
pub fn emit(component: &str, check: &str, ok: bool, detail: &str) {
    if let Some(hook) = HOOK.get() {
        hook(component, check, ok, detail);
    }
}

/// `assert!`-with-emit: assert on the real return value AND record the verdict as a
/// matrix row through the installed hook.
#[macro_export]
macro_rules! assert_emit {
    ($component:expr, $check:expr, $ok:expr, $($detail:tt)+) => {{
        let __ok: bool = $ok;
        let __detail = ::std::format!($($detail)+);
        $crate::jobs::selftest::emit($component, $check, __ok, &__detail);
        ::std::assert!(__ok, "{}::{} — {}", $component, $check, __detail);
    }};
}