use crate::blocks::conversion::base::ConversionBlock;
use crate::error::MdfError;
use crate::parsing::decoder::DecodedValue;
use meval::{Context, Expr};
pub fn extract_numeric(value: &DecodedValue) -> Option<f64> {
match value {
DecodedValue::Float(n) => Some(*n),
DecodedValue::UnsignedInteger(n) => Some(*n as f64),
DecodedValue::SignedInteger(n) => Some(*n as f64),
_ => None,
}
}
pub fn apply_linear(block: &ConversionBlock, value: DecodedValue) -> Result<DecodedValue, MdfError> {
if let Some(raw) = extract_numeric(&value) {
if block.cc_val.len() >= 2 {
let result = block.cc_val[0] + block.cc_val[1] * raw;
Ok(DecodedValue::Float(result))
} else {
Ok(DecodedValue::Float(raw))
}
} else {
Ok(value)
}
}
pub fn apply_rational(block: &ConversionBlock, value: DecodedValue) -> Result<DecodedValue, MdfError> {
if let Some(raw) = extract_numeric(&value) {
if block.cc_val.len() >= 6 {
let p1 = block.cc_val[0];
let p2 = block.cc_val[1];
let p3 = block.cc_val[2];
let p4 = block.cc_val[3];
let p5 = block.cc_val[4];
let p6 = block.cc_val[5];
let num = p1 * raw * raw + p2 * raw + p3;
let den = p4 * raw * raw + p5 * raw + p6;
Ok(DecodedValue::Float(num / den))
} else {
Ok(DecodedValue::Float(raw))
}
} else {
Ok(value)
}
}
pub fn apply_algebraic(block: &ConversionBlock, value: DecodedValue) -> Result<DecodedValue, MdfError> {
if let (Some(raw), Some(formula)) = (extract_numeric(&value), block.formula.as_ref()) {
Ok(DecodedValue::Float(eval_formula(formula, raw).unwrap_or(raw)))
} else {
Ok(value)
}
}
type FormulaCache = std::collections::HashMap<String, Option<Expr>>;
const FORMULA_CACHE_LIMIT: usize = 1024;
thread_local! {
static FORMULAS: std::cell::RefCell<FormulaCache> = std::cell::RefCell::new(FormulaCache::new());
static BUILTINS: Context<'static> = Context::new();
}
fn eval_formula(formula: &str, x: f64) -> Option<f64> {
let eval = |expr: &Expr| {
BUILTINS.with(|builtins| expr.eval_with_context((("X", x), (("X1", x), builtins))).ok())
};
FORMULAS.with(|cache| {
let mut cache = cache.borrow_mut();
if let Some(parsed) = cache.get(formula) {
return eval(parsed.as_ref()?);
}
if cache.len() >= FORMULA_CACHE_LIMIT {
cache.clear();
}
let parsed = formula.replace("X1", "X").parse::<Expr>().ok();
let result = parsed.as_ref().and_then(eval);
cache.insert(formula.to_string(), parsed);
result
})
}