math-mumu 0.2.0-rc.2

Math functions plugin for the MuMu / Lava language
Documentation
//! Simple √(x) implementation that works on Int / Long / Float and
//! on decimal strings using `rust_decimal`.

use mumu::parser::interpreter::Interpreter;
use mumu::parser::types::Value;

use rust_decimal::prelude::ToPrimitive;        // ← crate path fixed
use rust_decimal::Decimal;

/// Bridge for `math:sqrt(x)`
pub fn math_sqrt_bridge(
    _interp: &mut Interpreter,
    args: Vec<Value>,
) -> Result<Value, String> {
    if args.len() != 1 {
        return Err(format!("math:sqrt expects 1 argument, got {}", args.len()));
    }

    match &args[0] {
        Value::Int(i) if *i >= 0 => Ok(Value::Float((*i as f64).sqrt())),
        Value::Long(l) if *l >= 0 => Ok(Value::Float((*l as f64).sqrt())),
        Value::Float(f) if *f >= 0.0 => Ok(Value::Float(f.sqrt())),
        Value::SingleString(s) => {
            let dec = s.parse::<Decimal>()
                .map_err(|_| "math:sqrt: cannot parse decimal string")?;
            if dec < Decimal::ZERO {
                return Err("math:sqrt: negative input".into());
            }
            let f = dec.to_f64().unwrap();
            Ok(Value::Float(f.sqrt()))
        }
        other => Err(format!(
            "math:sqrt: unsupported or negative input: {:?}",
            other
        )),
    }
}