From efaa33088bc890914fb9aa8a7994912cdccd2d23 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Wed, 18 Mar 2020 10:14:35 -0400 Subject: [PATCH] Implement unit conversions in numeric subtraction --- src/value/ops.rs | 11 ++++++++++- tests/units.rs | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/value/ops.rs b/src/value/ops.rs index 4faa25d..0f07328 100644 --- a/src/value/ops.rs +++ b/src/value/ops.rs @@ -123,8 +123,17 @@ impl Sub for Value { } if unit == unit2 { 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 { - todo!("unit conversions") + Value::Dimension( + num - num2 + * UNIT_CONVERSION_TABLE[&unit.to_string()][&unit2.to_string()] + .clone(), + unit, + ) } } _ => todo!(), diff --git a/tests/units.rs b/tests/units.rs index 9634213..88888e3 100644 --- a/tests/units.rs +++ b/tests/units.rs @@ -54,6 +54,16 @@ test!( "a {\n color: 10 + 10px;\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 { ($u1:ident, $u2:ident, $out:literal) => {