simplify div and modulo implementations

This commit is contained in:
connorskees 2023-01-08 15:25:18 +00:00
parent f321f36faf
commit 0902851862

View File

@ -5,7 +5,6 @@ use codemap::Span;
use crate::{ use crate::{
common::{BinaryOp, QuoteKind}, common::{BinaryOp, QuoteKind},
error::SassResult, error::SassResult,
serializer::serialize_number,
unit::Unit, unit::Unit,
value::{SassNumber, Value}, value::{SassNumber, Value},
Options, Options,
@ -454,78 +453,44 @@ pub(crate) fn single_eq(
)) ))
} }
// todo: simplify matching
pub(crate) fn div(left: Value, right: Value, options: &Options, span: Span) -> SassResult<Value> { pub(crate) fn div(left: Value, right: Value, options: &Options, span: Span) -> SassResult<Value> {
Ok(match left { Ok(match (left, right) {
Value::Dimension(SassNumber { (Value::Dimension(num1), Value::Dimension(num2)) => {
num, if num2.unit == Unit::None {
unit,
as_slash: as_slash1,
}) => match right {
Value::Dimension(SassNumber {
num: num2,
unit: unit2,
..
}) => {
if unit2 == Unit::None {
return Ok(Value::Dimension(SassNumber { return Ok(Value::Dimension(SassNumber {
num: num / num2, num: num1.num / num2.num,
unit, unit: num1.unit,
as_slash: None, as_slash: None,
})); }));
} }
let n = SassNumber { let n = SassNumber {
num, num: num1.num,
unit, unit: num1.unit,
as_slash: None, as_slash: None,
} / SassNumber { } / SassNumber {
num: num2, num: num2.num,
unit: unit2, unit: num2.unit,
as_slash: None, as_slash: None,
}; };
Value::Dimension(n) Value::Dimension(n)
} }
_ => Value::String( (
format!( left @ Value::Color(..),
"{}/{}", right @ (Value::Dimension(SassNumber { .. }) | Value::Color(..)),
serialize_number( ) => {
&SassNumber {
num,
unit,
as_slash: as_slash1
},
options,
span
)?,
right.to_css_string(span, options.is_compressed())?
),
QuoteKind::None,
),
},
c @ Value::Color(..) => match right {
Value::Dimension(SassNumber { .. }) | Value::Color(..) => {
return Err(( return Err((
format!( format!(
"Undefined operation \"{} / {}\".", "Undefined operation \"{} / {}\".",
c.inspect(span)?, left.inspect(span)?,
right.inspect(span)? right.inspect(span)?
), ),
span, span,
) )
.into()) .into())
} }
_ => Value::String( (left, right) => Value::String(
format!(
"{}/{}",
c.to_css_string(span, options.is_compressed())?,
right.to_css_string(span, options.is_compressed())?
),
QuoteKind::None,
),
},
_ => Value::String(
format!( format!(
"{}/{}", "{}/{}",
left.to_css_string(span, options.is_compressed())?, left.to_css_string(span, options.is_compressed())?,
@ -537,28 +502,23 @@ pub(crate) fn div(left: Value, right: Value, options: &Options, span: Span) -> S
} }
pub(crate) fn rem(left: Value, right: Value, _: &Options, span: Span) -> SassResult<Value> { pub(crate) fn rem(left: Value, right: Value, _: &Options, span: Span) -> SassResult<Value> {
Ok(match left { Ok(match (left, right) {
Value::Dimension(SassNumber { (Value::Dimension(num1), Value::Dimension(num2)) => {
num: n, if !num1.unit.comparable(&num2.unit) {
unit: u, return Err((
as_slash: _, format!("Incompatible units {} and {}.", num1.unit, num2.unit),
}) => match right { span,
Value::Dimension(SassNumber { )
num: n2, .into());
unit: u2,
as_slash: _,
}) => {
if !u.comparable(&u2) {
return Err((format!("Incompatible units {} and {}.", u, u2), span).into());
} }
let new_num = n % n2.convert(&u2, &u); let new_num = num1.num % num2.num.convert(&num2.unit, &num1.unit);
let new_unit = if u == u2 { let new_unit = if num1.unit == num2.unit {
u num1.unit
} else if u == Unit::None { } else if num1.unit == Unit::None {
u2 num2.unit
} else { } else {
u num1.unit
}; };
Value::Dimension(SassNumber { Value::Dimension(SassNumber {
num: new_num, num: new_num,
@ -566,25 +526,7 @@ pub(crate) fn rem(left: Value, right: Value, _: &Options, span: Span) -> SassRes
as_slash: None, as_slash: None,
}) })
} }
_ => { (left, right) => {
let val = Value::Dimension(SassNumber {
num: n,
unit: u,
as_slash: None,
})
.inspect(span)?;
return Err((
format!(
"Undefined operation \"{} % {}\".",
val,
right.inspect(span)?
),
span,
)
.into());
}
},
_ => {
return Err(( return Err((
format!( format!(
"Undefined operation \"{} % {}\".", "Undefined operation \"{} % {}\".",