noema 0.4.0

Noema IOC and DI framework for Rust
Documentation
use std::sync::atomic::{AtomicBool, Ordering};

/// Optional runtime switch for compile-time global pipeline behaviors (`global_pipeline!`).
/// When unset, global behaviors are skipped (nullable — not required).
pub static GLOBAL: AtomicBool = AtomicBool::new(false);

/// Enable global pipeline behaviors. Safe to call more than once (e.g. in tests).
pub fn enable_global_pipeline() {
    GLOBAL.store(true, Ordering::Release);
}

/// Alias for [`enable_global_pipeline`] (same pattern as `dispatcher_config`).
pub fn mediator_config() {
    enable_global_pipeline();
}

/// No-op hook when `global_pipeline!` was not invoked. Import when using `request!` alone:
///
/// ```ignore
/// use noema::__noema_emit_globals;
/// request!(Ping: PingHandler);
/// ```
#[macro_export]
macro_rules! __noema_emit_globals {
    ($input:ty, $behaviors:expr) => {};
}

/// Register compile-time global pipeline behavior types. Pair with [`enable_global_pipeline`] at startup.
///
/// Must appear in the same module **before** [`request!`](crate::request), and overrides the default
/// [`__noema_emit_globals`](crate::__noema_emit_globals) hook for that module.
///
/// # Example
///
/// ```ignore
/// global_pipeline!(GlobalLog, Metrics);
///
/// fn main() {
///     enable_global_pipeline();
/// }
///
/// request!(CreateOrder: CreateOrderHandler => [ValidateOrder]);
/// ```
#[macro_export]
macro_rules! global_pipeline {
    ($($behavior:ty),* $(,)?) => {
        macro_rules! __noema_emit_globals {
            ($input:ty, $behaviors:expr) => {
                $(
                    $behaviors.push(
                        ::std::sync::Arc::new(
                            <$behavior as $crate::core::Injectable<$crate::core::Container>>::inject(
                                &$crate::core::Container,
                            ),
                        ) as ::std::sync::Arc<
                            dyn $crate::mediator::PipelineBehavior<$input> + Send + Sync,
                        >,
                    );
                )*
            };
        }
    };
}

/// Alias for [`global_pipeline!`] with the same syntax.
#[macro_export]
macro_rules! global_request {
    ($($behavior:ty),* $(,)?) => {
        $crate::global_pipeline! { $($behavior),* }
    };
}