mumu 0.11.1

Lava Mumu is a language for those in the now and that know
Documentation
use crate::parser::types::Value;
use crate::parser::interpreter::Interpreter;

pub fn handle_extend_call(interp: &mut Interpreter, expr: &Value) -> Result<Value, String> {
    let plugin_name = match expr {
        Value::SingleString(s) => s,
        _ => return Err("extend: argument must be a string literal".to_string()),
    };

    let so_candidates = [
        format!("libmumu{}.so", plugin_name),
        format!("{}/libmumu{}.so", plugin_name, plugin_name),
        format!("/usr/local/lib/libmumu{}.so", plugin_name),
    ];

    for so_path in &so_candidates {
        if interp.is_verbose() {
            eprintln!("[extend] Trying path: '{}'", so_path);
        }
        match interp.load_plugin(so_path) {
            Ok(_) => {
                if interp.is_verbose() {
                    eprintln!("[extend] Loaded plugin from '{}'", so_path);
                }
                return Ok(Value::SingleString(plugin_name.clone()));
            }
            Err(e) => {
                if interp.is_verbose() {
                    eprintln!("[extend] Failed: {}", e);
                }
            }
        }
    }

    Err(format!(
        "extend => could not load plugin. Tried:\n  - {}\n  - {}\n  - {}",
        so_candidates[0], so_candidates[1], so_candidates[2]
    ))
}

pub fn extend_bridge(interp: &mut Interpreter, args: Vec<Value>) -> Result<Value, String> {
    if args.len() != 1 {
        return Err("extend expects one argument: a string literal plugin name".to_string());
    }
    handle_extend_call(interp, &args[0])
}