use super::Constant;
pub(crate) fn cast_as_number(cst: &Constant, strict_casts: bool) -> Result<f64, ()> {
if strict_casts && !matches!(cst, Constant::Number(_)) {
return Err(());
}
match cst {
Constant::Number(number) => Ok(f64::from(*number)),
Constant::String(str) => match str.parse::<f64>() {
Ok(value) => Ok(value),
Err(_) => {
Ok(f64::NAN)
}
},
Constant::Boolean(val) => Ok(if *val { 1f64 } else { 0f64 }),
Constant::Regexp(_) | Constant::NaN | Constant::Undefined => Ok(f64::NAN),
Constant::Null => Ok(0f64),
Constant::Symbol { key: _ } => todo!(),
}
}
pub(crate) fn cast_as_string(cst: &Constant, strict_casts: bool) -> Result<String, ()> {
if strict_casts && !matches!(cst, Constant::String(_)) {
return Err(());
}
Ok(cst.as_js_string())
}
pub(crate) fn cast_as_boolean(cst: &Constant, strict_casts: bool) -> Result<bool, ()> {
if strict_casts {
crate::utils::notify!("TODO assert boolean type here, maybe levels. Need to also return where can do collapsation");
}
Ok(match cst {
Constant::Number(number) => number.into_inner() != 0.,
Constant::String(value) => !value.is_empty(),
Constant::Regexp(_) => true,
Constant::Boolean(value) => *value,
Constant::NaN | Constant::Undefined | Constant::Null => false,
Constant::Symbol { key } => todo!(),
})
}