x-pipe-rs 0.1.0

Composable recommendation/feed pipeline framework built on comp-cat-rs
Documentation
//! Side effects: observing candidates without modifying them.
//!
//! A [`SideEffect`] is a tap that observes items for logging,
//! metrics, or analytics.  Items pass through unchanged.

use comp_cat_rs::effect::io::Io;

use crate::stage::Stage;

/// Observes candidates without modifying them.
///
/// Used for logging, metrics, and analytics.  The items pass
/// through the stage unchanged after the effect runs.
pub trait SideEffect<I, E> {
    /// Observe the candidates.
    fn observe(&self, items: &[I]) -> Io<E, ()>;
}

/// Convert a [`SideEffect`] into a [`Stage`] that passes items through.
///
/// The side effect runs, then the original items are returned.
pub fn side_effect_stage<S, I, E>(effect: S) -> Stage<E, Vec<I>, Vec<I>>
where
    S: SideEffect<I, E> + Send + 'static,
    I: Send + 'static,
    E: Send + 'static,
{
    Stage::new(move |items: Vec<I>| {
        effect.observe(&items).map(move |()| items)
    })
}