use super::DataValue;
#[inline]
pub(crate) fn parse_finite(s: &str) -> Option<f64> {
s.parse::<f64>().ok().filter(|f| f.is_finite())
}
#[inline]
pub(crate) fn coerce_to_number_cfg(v: &DataValue<'_>, engine: &crate::Engine) -> Option<f64> {
let coercion = &engine.config().numeric_coercion;
if coercion.reject_non_numeric {
return match v {
DataValue::Number(n) => Some(n.as_f64()),
DataValue::String(s) => parse_finite(s),
_ => None,
};
}
match v {
DataValue::Number(n) => Some(n.as_f64()),
DataValue::String(s) => {
if s.is_empty() && coercion.empty_string_to_zero {
Some(0.0)
} else {
parse_finite(s)
}
}
DataValue::Bool(b) if coercion.bool_to_number => Some(if *b { 1.0 } else { 0.0 }),
DataValue::Null if coercion.null_to_zero => Some(0.0),
_ => None,
}
}
#[inline]
pub(crate) fn try_coerce_to_integer_cfg(v: &DataValue<'_>, engine: &crate::Engine) -> Option<i64> {
let coercion = &engine.config().numeric_coercion;
if coercion.reject_non_numeric {
return match v {
DataValue::Number(n) => n.as_i64(),
DataValue::String(s) => s.parse().ok(),
_ => None,
};
}
match v {
DataValue::Number(n) => n.as_i64(),
DataValue::String(s) => {
if s.is_empty() && coercion.empty_string_to_zero {
Some(0)
} else {
s.parse().ok()
}
}
DataValue::Bool(b) if coercion.bool_to_number => Some(if *b { 1 } else { 0 }),
DataValue::Null if coercion.null_to_zero => Some(0),
_ => None,
}
}