implement integer division to an extent

This commit is contained in:
ConnorSkees 2020-03-31 02:31:14 -04:00
parent bb87d4f4c0
commit 08f7dba00d
2 changed files with 43 additions and 1 deletions

View File

@ -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(

30
tests/division.rs Normal file
View File

@ -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."
// );