Implement unit conversions in numeric subtraction

This commit is contained in:
ConnorSkees 2020-03-18 10:14:35 -04:00
parent b3b5163113
commit efaa33088b
2 changed files with 20 additions and 1 deletions

View File

@ -123,8 +123,17 @@ impl Sub for Value {
} }
if unit == unit2 { if unit == unit2 {
Value::Dimension(num - num2, unit) Value::Dimension(num - num2, unit)
} else if unit == Unit::None {
Value::Dimension(num - num2, unit2)
} else if unit2 == Unit::None {
Value::Dimension(num - num2, unit)
} else { } else {
todo!("unit conversions") Value::Dimension(
num - num2
* UNIT_CONVERSION_TABLE[&unit.to_string()][&unit2.to_string()]
.clone(),
unit,
)
} }
} }
_ => todo!(), _ => todo!(),

View File

@ -54,6 +54,16 @@ test!(
"a {\n color: 10 + 10px;\n}\n", "a {\n color: 10 + 10px;\n}\n",
"a {\n color: 20px;\n}\n" "a {\n color: 20px;\n}\n"
); );
test!(
unit_minus_none,
"a {\n color: 10px - 10;\n}\n",
"a {\n color: 0px;\n}\n"
);
test!(
none_minus_unit,
"a {\n color: 10 - 10px;\n}\n",
"a {\n color: 0px;\n}\n"
);
macro_rules! test_unit_addition { macro_rules! test_unit_addition {
($u1:ident, $u2:ident, $out:literal) => { ($u1:ident, $u2:ident, $out:literal) => {