implement integer division to an extent
This commit is contained in:
parent
bb87d4f4c0
commit
08f7dba00d
@ -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
30
tests/division.rs
Normal 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."
|
||||
// );
|
Loading…
x
Reference in New Issue
Block a user