use mumu::parser::interpreter::Interpreter;
use mumu::parser::types::Value;
use rust_decimal::prelude::ToPrimitive; use rust_decimal::Decimal;
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
)),
}
}