From 0902851862a19c8b3f39d25857ab807a2f477216 Mon Sep 17 00:00:00 2001 From: connorskees Date: Sun, 8 Jan 2023 15:25:18 +0000 Subject: [PATCH] simplify div and modulo implementations --- crates/compiler/src/evaluate/bin_op.rs | 164 ++++++++----------------- 1 file changed, 53 insertions(+), 111 deletions(-) diff --git a/crates/compiler/src/evaluate/bin_op.rs b/crates/compiler/src/evaluate/bin_op.rs index c773b43..a504924 100644 --- a/crates/compiler/src/evaluate/bin_op.rs +++ b/crates/compiler/src/evaluate/bin_op.rs @@ -5,7 +5,6 @@ use codemap::Span; use crate::{ common::{BinaryOp, QuoteKind}, error::SassResult, - serializer::serialize_number, unit::Unit, value::{SassNumber, Value}, 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 { - Ok(match left { - Value::Dimension(SassNumber { - num, - unit, - as_slash: as_slash1, - }) => match right { - Value::Dimension(SassNumber { - num: num2, - unit: unit2, - .. - }) => { - if unit2 == Unit::None { - return Ok(Value::Dimension(SassNumber { - num: num / num2, - unit, - as_slash: None, - })); - } - - let n = SassNumber { - num, - unit, + Ok(match (left, right) { + (Value::Dimension(num1), Value::Dimension(num2)) => { + if num2.unit == Unit::None { + return Ok(Value::Dimension(SassNumber { + num: num1.num / num2.num, + unit: num1.unit, as_slash: None, - } / SassNumber { - num: num2, - unit: unit2, - as_slash: None, - }; + })); + } - Value::Dimension(n) - } - _ => Value::String( + let n = SassNumber { + num: num1.num, + unit: num1.unit, + as_slash: None, + } / SassNumber { + num: num2.num, + unit: num2.unit, + as_slash: None, + }; + + Value::Dimension(n) + } + ( + left @ Value::Color(..), + right @ (Value::Dimension(SassNumber { .. }) | Value::Color(..)), + ) => { + return Err(( format!( - "{}/{}", - serialize_number( - &SassNumber { - num, - unit, - as_slash: as_slash1 - }, - options, - span - )?, - right.to_css_string(span, options.is_compressed())? + "Undefined operation \"{} / {}\".", + left.inspect(span)?, + right.inspect(span)? ), - QuoteKind::None, - ), - }, - c @ Value::Color(..) => match right { - Value::Dimension(SassNumber { .. }) | Value::Color(..) => { - return Err(( - format!( - "Undefined operation \"{} / {}\".", - c.inspect(span)?, - right.inspect(span)? - ), - span, - ) - .into()) - } - _ => Value::String( - format!( - "{}/{}", - c.to_css_string(span, options.is_compressed())?, - right.to_css_string(span, options.is_compressed())? - ), - QuoteKind::None, - ), - }, - _ => Value::String( + span, + ) + .into()) + } + (left, right) => Value::String( format!( "{}/{}", left.to_css_string(span, options.is_compressed())?, @@ -537,54 +502,31 @@ 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 { - Ok(match left { - Value::Dimension(SassNumber { - num: n, - unit: u, - as_slash: _, - }) => match right { - Value::Dimension(SassNumber { - num: n2, - 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_unit = if u == u2 { - u - } else if u == Unit::None { - u2 - } else { - u - }; - Value::Dimension(SassNumber { - num: new_num, - unit: new_unit, - as_slash: None, - }) - } - _ => { - let val = Value::Dimension(SassNumber { - num: n, - unit: u, - as_slash: None, - }) - .inspect(span)?; + Ok(match (left, right) { + (Value::Dimension(num1), Value::Dimension(num2)) => { + if !num1.unit.comparable(&num2.unit) { return Err(( - format!( - "Undefined operation \"{} % {}\".", - val, - right.inspect(span)? - ), + format!("Incompatible units {} and {}.", num1.unit, num2.unit), span, ) .into()); } - }, - _ => { + + let new_num = num1.num % num2.num.convert(&num2.unit, &num1.unit); + let new_unit = if num1.unit == num2.unit { + num1.unit + } else if num1.unit == Unit::None { + num2.unit + } else { + num1.unit + }; + Value::Dimension(SassNumber { + num: new_num, + unit: new_unit, + as_slash: None, + }) + } + (left, right) => { return Err(( format!( "Undefined operation \"{} % {}\".",