From 08f7dba00d6c434fc6d2b26f5e55c00618c45d25 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Tue, 31 Mar 2020 02:31:14 -0400 Subject: [PATCH] implement integer division to an extent --- src/value/ops.rs | 14 +++++++++++++- tests/division.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/division.rs diff --git a/src/value/ops.rs b/src/value/ops.rs index 2d153de..cf43587 100644 --- a/src/value/ops.rs +++ b/src/value/ops.rs @@ -233,10 +233,22 @@ impl Div for Value { Self::Null => todo!(), Self::Dimension(num, unit) => match other { Self::Dimension(num2, unit2) => { + if !unit.comparable(&unit2) { + return Err(format!("Incompatible units {} and {}.", unit2, unit).into()); + } if unit == unit2 { Value::Dimension(num / num2, Unit::None) + } else if unit == Unit::None { + todo!("inverse units") + } 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::None, + ) } } Self::Ident(s, q) => Value::Ident( diff --git a/tests/division.rs b/tests/division.rs new file mode 100644 index 0000000..8c261d9 --- /dev/null +++ b/tests/division.rs @@ -0,0 +1,30 @@ +#![cfg(test)] + +#[macro_use] +mod macros; + +test!( + none_div_none, + "a {\n color: (35 / 7);\n}\n", + "a {\n color: 5;\n}\n" +); +test!( + unit_div_none, + "a {\n color: (35% / 7);\n}\n", + "a {\n color: 5%;\n}\n" +); +test!( + unit_div_unit, + "a {\n color: (35% / 7%);\n}\n", + "a {\n color: 5;\n}\n" +); +test!( + unit_conversion, + "a {\n color: (35px / 7in);\n}\n", + "a {\n color: 0.0520833333;\n}\n" +); +// error!( +// none_div_unit, +// "a {\n color: (35 / 7%);\n}\n", +// "Error: 5%^-1 isn't a valid CSS value." +// );