array-mumu 0.2.0-rc.5

Array tools plugin for the Mumu ecosystem
Documentation
//! Bridging code for `array:keys`.
//
/// Usage in Lava / MuMu:
/// ```lava
/// extend("array")
///
/// sput(array:keys([
///   alpha: 10,
///   beta: 20,
///   gamma: 42,
/// ]))   // → [ "alpha", "beta", "gamma" ]
/// ```
use mumu::{
    parser::interpreter::Interpreter,
    parser::types::{FunctionValue, Value},
};
use std::sync::{Arc, Mutex};

/// Core implementation of `array:keys`.
///
/// * Accepts **exactly one** argument – a keyed array.
/// * Returns a `StrArray` containing every key (in the order returned by the
///   underlying map – for `BTreeMap` that is sorted order; for an
///   insertion‑preserving map that is insertion order).
pub fn array_keys_bridge(
    _interp: &mut Interpreter,
    args: Vec<Value>,
) -> Result<Value, String> {
    // ---------------------------------------------------------------------
    // 1. Validate arity
    // ---------------------------------------------------------------------
    if args.len() != 1 {
        return Err(format!(
            "array:keys => expected 1 argument (keyed_array), got {}",
            args.len()
        ));
    }

    // ---------------------------------------------------------------------
    // 2. Extract argument and build result
    // ---------------------------------------------------------------------
    match &args[0] {
        Value::KeyedArray(map) => {
            let keys: Vec<String> = map.keys().cloned().collect();
            Ok(Value::StrArray(keys))
        }
        other => Err(format!(
            "array:keys => argument must be a keyed array, got {:?}",
            other
        )),
    }
}

/// Helper to register the bridging function into an interpreter.
///
/// Called from `plugin_entry.rs` at plugin‑initialisation time.
pub fn register_keys(interp: &mut Interpreter) {
    let dyn_fn = Arc::new(Mutex::new(array_keys_bridge));
    interp.register_dynamic_function("array:keys", dyn_fn);
    interp.set_variable(
        "array:keys",
        Value::Function(Box::new(FunctionValue::Named("array:keys".to_string()))),
    );
}