flow-mumu 0.2.0-rc.9

Stream transform tools plugin for the Lava language
Documentation
// src/lib.rs
// flow/src/lib.rs

use std::ffi::c_void;
use std::mem::{align_of, size_of};

use mumu::parser::interpreter::Interpreter;
use mumu::parser::types::{FunctionValue, IteratorHandle, IteratorKind, Value};

mod common;
mod trans;
mod compose;
mod slice;
mod to_array;
mod filter;
mod throttle;
mod throttle_with; // NEW
// New modules
mod reduce;
mod of;
mod empty;
mod concat;
mod chain;
mod ap;
mod drain;
mod read;         // keep read
mod drain_async;  // async drain (micro-bursts)
mod drain_then;   // async drain with required callback at EOF
mod stop_when;    // NEW: stop pipeline when predicate becomes true

pub use trans::register_flow_trans;
pub use compose::register_flow_compose;
pub use slice::register_flow_slice;
pub use to_array::register_flow_to_array;
pub use filter::register_flow_filter;
pub use throttle::register_flow_throttle;
pub use throttle_with::register_flow_throttle_with; // NEW

// New exports
pub use trans::register_flow_map_alias;
pub use reduce::register_flow_reduce;
pub use of::register_flow_of;
pub use empty::register_flow_empty;
pub use concat::register_flow_concat;
pub use chain::register_flow_chain;
pub use ap::{register_flow_ap_zip, register_flow_ap};
pub use drain::register_flow_drain;
pub use read::register_flow_read;
pub use drain_async::register_flow_drain_async;
pub use drain_then::register_flow_drain_then;
pub use stop_when::register_flow_stop_when;

fn log_abi_info(interp: &Interpreter) {
    if !interp.is_verbose() {
        return;
    }

    eprintln!("[flow][abi] sizeof(Value)        = {}", size_of::<Value>());
    eprintln!("[flow][abi] alignof(Value)        = {}", align_of::<Value>());
    eprintln!("[flow][abi] sizeof(FunctionValue) = {}", size_of::<FunctionValue>());
    eprintln!("[flow][abi] alignof(FunctionValue) = {}", align_of::<FunctionValue>());
    eprintln!("[flow][abi] sizeof(IteratorHandle) = {}", size_of::<IteratorHandle>());
    eprintln!("[flow][abi] sizeof(IteratorKind)   = {}", size_of::<IteratorKind>());

    let dbg_fn = Value::Function(Box::new(FunctionValue::Named("dbg".to_string())));
    let dbg_it = Value::Iterator(IteratorHandle {
        kind: IteratorKind::Core(std::sync::Arc::new(std::sync::Mutex::new(
            mumu::parser::types::IteratorState { current: 0, end: 1, done: false },
        ))),
    });
    eprintln!("[flow][abi] variant(Function) discriminant ok? {:?}", dbg_fn);
    eprintln!("[flow][abi] variant(Iterator) discriminant ok?  {:?}", dbg_it);
}

#[export_name = "Cargo_lock"]
pub unsafe extern "C" fn cargo_lock(interp_ptr: *mut c_void, extra_str: *const c_void) -> i32 {
    if interp_ptr.is_null() {
        return 1;
    }
    let interp = &mut *(interp_ptr as *mut Interpreter);

    if interp.is_verbose() {
        eprintln!("[flow:Cargo.lock] => bridging flow plugin...");
        if !extra_str.is_null() {
            use std::ffi::CStr;
            let c_str = CStr::from_ptr(extra_str as *const i8);
            eprintln!("[flow:Cargo.lock] => extra arg='{}'", c_str.to_string_lossy());
        }
    }

    log_abi_info(interp);

    // Core stages
    register_flow_trans(interp);
    register_flow_compose(interp);
    register_flow_slice(interp);
    register_flow_to_array(interp);
    register_flow_filter(interp);
    register_flow_throttle(interp);
    register_flow_throttle_with(interp); // NEW

    // Aliases / extra operators
    register_flow_map_alias(interp);

    // Functional set
    register_flow_of(interp);
    register_flow_empty(interp);
    register_flow_concat(interp);
    register_flow_reduce(interp);
    register_flow_chain(interp);
    register_flow_ap_zip(interp);
    register_flow_ap(interp);

    // Sinks & utilities
    register_flow_drain_async(interp);
    register_flow_drain_then(interp);
    register_flow_drain(interp);
    register_flow_read(interp);

    // NEW control operator
    register_flow_stop_when(interp);

    if interp.is_verbose() {
        eprintln!("[flow:Cargo.lock] => done bridging flow plugin");
    }

    0
}