grass/src/value/ops.rs

834 lines
31 KiB
Rust
Raw Normal View History

use std::cmp::Ordering;
use codemap::{Span, Spanned};
2020-02-08 16:07:37 -05:00
2020-04-01 15:32:52 -04:00
use crate::common::{Op, QuoteKind};
2020-02-17 10:27:04 -05:00
use crate::error::SassResult;
use crate::interner::InternedString;
use crate::unit::{Unit, UNIT_CONVERSION_TABLE};
2020-02-08 16:07:37 -05:00
use crate::value::Value;
2020-02-17 10:39:32 -05:00
2020-04-12 19:37:12 -04:00
impl Value {
pub fn equals(self, mut other: Value, span: Span) -> SassResult<Spanned<Value>> {
if let Self::Paren(..) = other {
other = other.eval(span)?.node
}
let precedence = Op::Equal.precedence();
Ok(Value::bool(match self {
Self::Ident(s1, ..) => match other {
Self::Ident(s2, ..) => s1 == s2,
_ => false,
},
Self::Dimension(n, unit) => match other {
Self::Dimension(n2, unit2) => {
if !unit.comparable(&unit2) {
false
} else if unit == unit2 {
n == n2
} else if unit == Unit::None || unit2 == Unit::None {
false
} else {
n == (n2
* UNIT_CONVERSION_TABLE[unit.to_string().as_str()]
[unit2.to_string().as_str()]
.clone())
}
}
_ => false,
},
Self::BinaryOp(left, op2, right) => {
if op2.precedence() >= precedence {
Self::BinaryOp(left, op2, right).eval(span)?.node == other
} else {
return Self::BinaryOp(
left,
op2,
Box::new(
Self::BinaryOp(right, Op::Equal, Box::new(other))
.eval(span)?
.node,
),
)
.eval(span);
}
}
s => s == other.eval(span)?.node,
})
.span(span))
}
pub fn not_equals(self, mut other: Value, span: Span) -> SassResult<Spanned<Value>> {
if let Self::Paren(..) = other {
other = other.eval(span)?.node
}
let precedence = Op::Equal.precedence();
Ok(Value::bool(match self {
Self::Ident(s1, ..) => match other {
Self::Ident(s2, ..) => s1 != s2,
_ => true,
},
Self::Dimension(n, unit) => match other {
Self::Dimension(n2, unit2) => {
if !unit.comparable(&unit2) {
true
} else if unit == unit2 {
n != n2
} else if unit == Unit::None || unit2 == Unit::None {
true
} else {
n != (n2
* UNIT_CONVERSION_TABLE[unit.to_string().as_str()]
[unit2.to_string().as_str()]
.clone())
}
}
_ => true,
},
Self::BinaryOp(left, op2, right) => {
if op2.precedence() >= precedence {
Self::BinaryOp(left, op2, right).eval(span)?.node != other
} else {
return Self::BinaryOp(
left,
op2,
Box::new(
Self::BinaryOp(right, Op::NotEqual, Box::new(other))
.eval(span)?
.node,
),
)
.eval(span);
}
}
s => s != other.eval(span)?.node,
})
.span(span))
}
pub fn unary_op_plus(self, span: Span) -> SassResult<Self> {
Ok(match self.eval(span)?.node {
v @ Value::Dimension(..) => v,
v => Value::Ident(
InternedString::get_or_intern(format!("+{}", v.to_css_string(span)?)),
QuoteKind::None,
),
})
}
pub fn eval(self, span: Span) -> SassResult<Spanned<Self>> {
Ok(match self {
Self::BinaryOp(lhs, op, rhs) => match op {
Op::Plus => lhs.add(*rhs, span)?,
Op::Minus => lhs.sub(*rhs, span)?,
Op::Equal => lhs.equals(*rhs, span)?.node,
Op::NotEqual => lhs.not_equals(*rhs, span)?.node,
Op::Mul => lhs.mul(*rhs, span)?,
Op::Div => lhs.div(*rhs, span)?,
Op::Rem => lhs.rem(*rhs, span)?,
Op::GreaterThan => return lhs.cmp(*rhs, op, span),
Op::GreaterThanEqual => return lhs.cmp(*rhs, op, span),
Op::LessThan => return lhs.cmp(*rhs, op, span),
Op::LessThanEqual => return lhs.cmp(*rhs, op, span),
Op::Not => unreachable!(),
Op::And => {
if lhs.is_true(span)? {
rhs.eval(span)?.node
} else {
lhs.eval(span)?.node
}
}
Op::Or => {
if lhs.is_true(span)? {
lhs.eval(span)?.node
} else {
rhs.eval(span)?.node
}
}
},
Self::Paren(v) => v.eval(span)?.node,
Self::UnaryOp(op, val) => match op {
Op::Plus => val.unary_op_plus(span)?,
Op::Minus => val.neg(span)?,
Op::Not => Self::bool(!val.eval(span)?.is_true(span)?),
_ => unreachable!(),
},
_ => self,
}
.span(span))
}
pub fn cmp(self, mut other: Self, op: Op, span: Span) -> SassResult<Spanned<Value>> {
if let Self::Paren(..) = other {
other = other.eval(span)?.node
}
let precedence = op.precedence();
let ordering = match self {
Self::Dimension(num, unit) => match &other {
Self::Dimension(num2, unit2) => {
if !unit.comparable(unit2) {
return Err(
(format!("Incompatible units {} and {}.", unit2, unit), span).into(),
);
}
if &unit == unit2 || unit == Unit::None || unit2 == &Unit::None {
num.cmp(num2)
} else {
num.cmp(
&(num2.clone()
* UNIT_CONVERSION_TABLE[unit.to_string().as_str()]
[unit2.to_string().as_str()]
.clone()),
)
}
}
Self::BinaryOp(..) => todo!(),
v => {
return Err((
format!(
"Undefined operation \"{} {} {}\".",
v.to_css_string(span)?,
op,
other.to_css_string(span)?
),
span,
)
.into())
}
},
Self::BinaryOp(left, op2, right) => {
return if op2.precedence() >= precedence {
Self::BinaryOp(left, op2, right)
.eval(span)?
.node
.cmp(other, op, span)
} else {
Self::BinaryOp(
left,
op2,
Box::new(Self::BinaryOp(right, op, Box::new(other)).eval(span)?.node),
)
.eval(span)
}
}
Self::UnaryOp(..) | Self::Paren(..) => {
return self.eval(span)?.node.cmp(other, op, span)
}
_ => {
return Err((
format!(
"Undefined operation \"{} {} {}\".",
self.to_css_string(span)?,
op,
other.to_css_string(span)?
),
span,
)
.into())
}
};
Ok(match op {
Op::GreaterThan => match ordering {
Ordering::Greater => Self::True,
Ordering::Less | Ordering::Equal => Self::False,
},
Op::GreaterThanEqual => match ordering {
Ordering::Greater | Ordering::Equal => Self::True,
Ordering::Less => Self::False,
},
Op::LessThan => match ordering {
Ordering::Less => Self::True,
Ordering::Greater | Ordering::Equal => Self::False,
},
Op::LessThanEqual => match ordering {
Ordering::Less | Ordering::Equal => Self::True,
Ordering::Greater => Self::False,
},
_ => unreachable!(),
}
.span(span))
}
2020-04-12 19:37:12 -04:00
pub fn add(self, mut other: Self, span: Span) -> SassResult<Self> {
2020-04-01 15:32:52 -04:00
if let Self::Paren(..) = other {
2020-04-12 19:37:12 -04:00
other = other.eval(span)?.node
2020-04-20 02:16:22 -04:00
} else if let Self::UnaryOp(..) = other {
other = other.eval(span)?.node
2020-04-01 15:32:52 -04:00
}
let precedence = Op::Plus.precedence();
2020-02-17 10:27:04 -05:00
Ok(match self {
Self::Function(..) | Self::ArgList(..) | Self::Map(..) => todo!(),
2020-02-08 16:07:37 -05:00
Self::Important | Self::True | Self::False => match other {
2020-04-18 13:19:30 -04:00
Self::Ident(s, QuoteKind::Quoted) => Value::Ident(
InternedString::get_or_intern(format!("{}{}", self.to_css_string(span)?, s)),
2020-04-18 13:19:30 -04:00
QuoteKind::Quoted,
),
Self::Null => Value::Ident(
InternedString::get_or_intern(self.to_css_string(span)?.into_owned()),
QuoteKind::None,
),
2020-04-12 19:37:12 -04:00
_ => Value::Ident(
InternedString::get_or_intern(format!(
2020-04-12 19:37:12 -04:00
"{}{}",
self.to_css_string(span)?,
other.to_css_string(span)?
)),
2020-04-12 19:37:12 -04:00
QuoteKind::None,
),
2020-02-08 16:07:37 -05:00
},
Self::Null => match other {
Self::Null => Self::Null,
_ => Value::Ident(
InternedString::get_or_intern(other.to_css_string(span)?.into_owned()),
QuoteKind::None,
),
2020-02-08 16:07:37 -05:00
},
Self::Dimension(num, unit) => match other {
2020-03-01 14:53:52 -05:00
Self::Dimension(num2, unit2) => {
if !unit.comparable(&unit2) {
2020-04-12 19:37:12 -04:00
return Err(
(format!("Incompatible units {} and {}.", unit2, unit), span).into(),
);
2020-03-01 14:53:52 -05:00
}
if unit == unit2 {
Value::Dimension(num + num2, unit)
} else if unit == Unit::None {
Value::Dimension(num + num2, unit2)
} else if unit2 == Unit::None {
Value::Dimension(num + num2, unit)
2020-03-01 14:53:52 -05:00
} else {
Value::Dimension(
num + num2
* UNIT_CONVERSION_TABLE[unit.to_string().as_str()]
[unit2.to_string().as_str()]
.clone(),
unit,
)
2020-03-01 14:53:52 -05:00
}
}
Self::Ident(s, q) => Value::Ident(
InternedString::get_or_intern(format!("{}{}{}", num, unit, s)),
q,
),
Self::Null => Value::Ident(
InternedString::get_or_intern(format!("{}{}", num, unit)),
QuoteKind::None,
),
2020-04-12 19:37:12 -04:00
Self::List(..) => Value::Ident(
InternedString::get_or_intern(format!(
"{}{}{}",
num,
unit,
other.to_css_string(span)?
)),
2020-04-12 19:37:12 -04:00
QuoteKind::None,
),
2020-04-20 02:13:28 -04:00
Self::True | Self::False => Self::Ident(
InternedString::get_or_intern(format!(
"{}{}{}",
num,
unit,
other.to_css_string(span)?
)),
2020-04-20 02:13:28 -04:00
QuoteKind::None,
),
2020-02-29 20:01:43 -05:00
_ => {
2020-04-12 19:37:12 -04:00
return Err((
format!(
"Undefined operation \"{}{} + {}\".",
num,
unit,
other.to_css_string(span)?
),
span,
2020-02-29 20:01:43 -05:00
)
2020-04-12 19:37:12 -04:00
.into())
2020-02-29 20:01:43 -05:00
}
2020-02-08 16:07:37 -05:00
},
2020-03-01 17:06:55 -05:00
Self::Color(c) => match other {
Self::Ident(s, q) => {
Value::Ident(InternedString::get_or_intern(format!("{}{}", c, s)), q)
}
Self::Null => Value::Ident(
InternedString::get_or_intern(c.to_string()),
QuoteKind::None,
),
2020-04-12 19:37:12 -04:00
Self::List(..) => Value::Ident(
InternedString::get_or_intern(format!("{}{}", c, other.to_css_string(span)?)),
2020-04-12 19:37:12 -04:00
QuoteKind::None,
),
_ => {
return Err((
format!(
"Undefined operation \"{} + {}\".",
c,
other.to_css_string(span)?
),
span,
)
.into())
}
},
2020-04-01 15:32:52 -04:00
Self::BinaryOp(left, op, right) => {
if op.precedence() >= precedence {
2020-04-12 19:37:12 -04:00
Self::BinaryOp(left, op, right)
.eval(span)?
.node
.add(other, span)?
2020-04-01 15:32:52 -04:00
} else {
Self::BinaryOp(
left,
op,
2020-04-12 19:37:12 -04:00
Box::new(
Self::BinaryOp(right, Op::Plus, Box::new(other))
.eval(span)?
.node,
),
2020-04-01 15:32:52 -04:00
)
2020-04-12 19:37:12 -04:00
.eval(span)?
.node
2020-04-01 15:32:52 -04:00
}
}
2020-04-12 19:37:12 -04:00
Self::UnaryOp(..) | Self::Paren(..) => self.eval(span)?.node.add(other, span)?,
2020-04-18 13:44:11 -04:00
Self::Ident(text, quotes) => match other {
Self::Ident(text2, ..) => Self::Ident(
InternedString::get_or_intern(text.resolve() + &text2.resolve()),
quotes,
),
_ => Value::Ident(
InternedString::get_or_intern(text.resolve() + &other.to_css_string(span)?),
quotes,
),
2020-02-08 16:07:37 -05:00
},
2020-03-19 14:20:16 -04:00
Self::List(..) => match other {
Self::Ident(s, q) => Value::Ident(
InternedString::get_or_intern(format!("{}{}", self.to_css_string(span)?, s)),
q,
),
2020-04-12 19:37:12 -04:00
Self::Paren(..) => (self.add(other.eval(span)?.node, span))?,
_ => Value::Ident(
InternedString::get_or_intern(format!(
2020-04-12 19:37:12 -04:00
"{}{}",
self.to_css_string(span)?,
other.to_css_string(span)?
)),
2020-04-12 19:37:12 -04:00
QuoteKind::None,
),
},
2020-02-17 10:27:04 -05:00
})
2020-02-08 16:07:37 -05:00
}
2020-04-12 19:37:12 -04:00
pub fn sub(self, mut other: Self, span: Span) -> SassResult<Self> {
2020-04-05 17:34:30 -04:00
if let Self::Paren(..) = other {
2020-04-12 19:37:12 -04:00
other = other.eval(span)?.node
2020-04-05 17:34:30 -04:00
}
let precedence = Op::Mul.precedence();
2020-02-17 10:27:04 -05:00
Ok(match self {
Self::Null => todo!(),
2020-02-08 16:07:37 -05:00
Self::Dimension(num, unit) => match other {
2020-03-01 14:53:52 -05:00
Self::Dimension(num2, unit2) => {
if !unit.comparable(&unit2) {
2020-04-12 19:37:12 -04:00
return Err(
(format!("Incompatible units {} and {}.", unit2, unit), span).into(),
);
2020-03-01 14:53:52 -05:00
}
if unit == unit2 {
Value::Dimension(num - num2, unit)
} else if unit == Unit::None {
Value::Dimension(num - num2, unit2)
} else if unit2 == Unit::None {
Value::Dimension(num - num2, unit)
2020-03-01 14:53:52 -05:00
} else {
Value::Dimension(
num - num2
* UNIT_CONVERSION_TABLE[unit.to_string().as_str()]
[unit2.to_string().as_str()]
.clone(),
unit,
)
2020-03-01 14:53:52 -05:00
}
}
2020-04-12 19:37:12 -04:00
Self::List(..) => Value::Ident(
InternedString::get_or_intern(format!(
"{}{}-{}",
num,
unit,
other.to_css_string(span)?
)),
2020-04-12 19:37:12 -04:00
QuoteKind::None,
),
Self::Ident(..) => Value::Ident(
InternedString::get_or_intern(format!(
"{}{}-{}",
num,
unit,
other.to_css_string(span)?
)),
2020-04-12 19:37:12 -04:00
QuoteKind::None,
),
2020-02-08 16:07:37 -05:00
_ => todo!(),
},
2020-02-09 11:07:13 -05:00
Self::Color(c) => match other {
Self::Ident(s, q) => Value::Ident(
InternedString::get_or_intern(format!("{}-{}{}{}", c, q, s, q)),
QuoteKind::None,
),
Self::Null => Value::Ident(
InternedString::get_or_intern(format!("{}-", c)),
QuoteKind::None,
),
2020-02-29 20:14:51 -05:00
Self::Dimension(..) | Self::Color(..) => {
2020-04-12 19:37:12 -04:00
return Err((
format!(
"Undefined operation \"{} - {}\".",
c,
other.to_css_string(span)?
),
span,
)
.into())
2020-02-29 20:01:43 -05:00
}
2020-04-12 19:37:12 -04:00
_ => Value::Ident(
InternedString::get_or_intern(format!("{}-{}", c, other.to_css_string(span)?)),
2020-04-12 19:37:12 -04:00
QuoteKind::None,
),
2020-02-09 11:07:13 -05:00
},
2020-04-05 17:34:30 -04:00
Self::BinaryOp(left, op, right) => {
if op.precedence() >= precedence {
2020-04-12 19:37:12 -04:00
Self::BinaryOp(left, op, right)
.eval(span)?
.node
.sub(other, span)?
2020-04-05 17:34:30 -04:00
} else {
Self::BinaryOp(
left,
op,
2020-04-12 19:37:12 -04:00
Box::new(
Self::BinaryOp(right, Op::Minus, Box::new(other))
.eval(span)?
.node,
),
2020-04-05 17:34:30 -04:00
)
2020-04-12 19:37:12 -04:00
.eval(span)?
.node
2020-04-05 17:34:30 -04:00
}
}
2020-04-12 19:37:12 -04:00
Self::Paren(..) => self.eval(span)?.node.sub(other, span)?,
2020-04-18 13:44:11 -04:00
Self::Ident(..) => Self::Ident(
InternedString::get_or_intern(format!(
2020-04-18 13:44:11 -04:00
"{}-{}",
self.to_css_string(span)?,
other.to_css_string(span)?
)),
2020-04-18 13:44:11 -04:00
QuoteKind::None,
),
2020-03-19 14:20:16 -04:00
Self::List(..) => match other {
Self::Ident(s, q) => Value::Ident(
InternedString::get_or_intern(format!(
"{}-{}{}{}",
self.to_css_string(span)?,
q,
s,
q
)),
2020-04-12 19:37:12 -04:00
QuoteKind::None,
),
_ => Value::Ident(
InternedString::get_or_intern(format!(
2020-04-12 19:37:12 -04:00
"{}-{}",
self.to_css_string(span)?,
other.to_css_string(span)?
)),
QuoteKind::None,
),
},
_ => match other {
Self::Ident(s, q) => Value::Ident(
InternedString::get_or_intern(format!(
"{}-{}{}{}",
self.to_css_string(span)?,
q,
s,
q
)),
QuoteKind::None,
),
Self::Null => Value::Ident(
InternedString::get_or_intern(format!("{}-", self.to_css_string(span)?)),
2020-04-12 19:37:12 -04:00
QuoteKind::None,
),
_ => Value::Ident(
InternedString::get_or_intern(format!(
2020-04-12 19:37:12 -04:00
"{}-{}",
self.to_css_string(span)?,
other.to_css_string(span)?
)),
QuoteKind::None,
),
},
2020-02-17 10:27:04 -05:00
})
}
2020-04-12 19:37:12 -04:00
pub fn mul(self, mut other: Self, span: Span) -> SassResult<Self> {
2020-04-05 17:34:30 -04:00
if let Self::Paren(..) = other {
2020-04-12 19:37:12 -04:00
other = other.eval(span)?.node
2020-04-05 17:34:30 -04:00
}
let precedence = Op::Mul.precedence();
2020-02-17 10:27:04 -05:00
Ok(match self {
Self::Null => todo!(),
Self::Dimension(num, unit) => match other {
2020-03-01 14:53:52 -05:00
Self::Dimension(num2, unit2) => {
2020-03-16 21:29:00 -04:00
if unit == Unit::None {
Value::Dimension(num * num2, unit2)
} else if unit2 == Unit::None {
2020-03-01 14:53:52 -05:00
Value::Dimension(num * num2, unit)
} else if let Unit::Mul(mut u) = unit {
u.push(unit2);
Value::Dimension(num * num2, Unit::Mul(u))
} else if let Unit::Mul(u2) = unit2 {
let mut u = vec![unit];
u.extend(u2);
Value::Dimension(num * num2, Unit::Mul(u))
2020-03-01 14:53:52 -05:00
} else {
Value::Dimension(num * num2, Unit::Mul(vec![unit, unit2]))
2020-03-01 14:53:52 -05:00
}
}
2020-02-29 20:01:43 -05:00
_ => {
2020-04-12 19:37:12 -04:00
return Err((
format!(
"Undefined operation \"{}{} * {}\".",
num,
unit,
other.to_css_string(span)?
),
span,
2020-02-29 20:01:43 -05:00
)
2020-04-12 19:37:12 -04:00
.into())
2020-02-29 20:01:43 -05:00
}
},
2020-04-05 17:34:30 -04:00
Self::BinaryOp(left, op, right) => {
if op.precedence() >= precedence {
2020-04-12 19:37:12 -04:00
Self::BinaryOp(left, op, right)
.eval(span)?
.node
.mul(other, span)?
2020-04-05 17:34:30 -04:00
} else {
Self::BinaryOp(
left,
op,
2020-04-12 19:37:12 -04:00
Box::new(
Self::BinaryOp(right, Op::Mul, Box::new(other))
.eval(span)?
.node,
),
2020-04-05 17:34:30 -04:00
)
2020-04-12 19:37:12 -04:00
.eval(span)?
.node
2020-04-05 17:34:30 -04:00
}
}
2020-04-12 19:37:12 -04:00
Self::UnaryOp(..) | Self::Paren(..) => self.eval(span)?.node.mul(other, span)?,
_ => {
return Err((
format!(
"Undefined operation \"{} * {}\".",
self.to_css_string(span)?,
other.to_css_string(span)?
),
span,
)
.into())
}
2020-02-17 10:27:04 -05:00
})
}
2020-04-12 19:37:12 -04:00
pub fn div(self, other: Self, span: Span) -> SassResult<Self> {
2020-04-01 15:32:52 -04:00
let precedence = Op::Div.precedence();
2020-02-17 10:27:04 -05:00
Ok(match self {
Self::Null => todo!(),
Self::Dimension(num, unit) => match other {
2020-02-09 18:28:24 -05:00
Self::Dimension(num2, unit2) => {
if !unit.comparable(&unit2) {
2020-04-12 19:37:12 -04:00
return Err(
(format!("Incompatible units {} and {}.", unit2, unit), span).into(),
);
}
2020-02-09 18:28:24 -05:00
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)
2020-02-09 18:28:24 -05:00
} else {
Value::Dimension(
num / (num2
* UNIT_CONVERSION_TABLE[unit.to_string().as_str()]
[unit2.to_string().as_str()]
.clone()),
Unit::None,
)
2020-02-09 18:28:24 -05:00
}
}
Self::Ident(s, q) => Value::Ident(
InternedString::get_or_intern(format!("{}{}/{}{}{}", num, unit, q, s, q)),
QuoteKind::None,
),
2020-02-17 10:27:04 -05:00
Self::BinaryOp(..) | Self::Paren(..) => {
2020-04-12 19:37:12 -04:00
Self::Dimension(num, unit).div(other.eval(span)?.node, span)?
2020-02-17 10:27:04 -05:00
}
_ => todo!(),
},
Self::Color(c) => match other {
Self::Ident(s, q) => Value::Ident(
InternedString::get_or_intern(format!("{}/{}{}{}", c, q, s, q)),
QuoteKind::None,
),
Self::Null => Value::Ident(
InternedString::get_or_intern(format!("{}/", c)),
QuoteKind::None,
),
2020-02-29 20:14:51 -05:00
Self::Dimension(..) | Self::Color(..) => {
2020-04-12 19:37:12 -04:00
return Err((
format!(
"Undefined operation \"{} / {}\".",
c,
other.to_css_string(span)?
),
span,
)
.into())
2020-02-29 20:01:43 -05:00
}
2020-04-12 19:37:12 -04:00
_ => Value::Ident(
InternedString::get_or_intern(format!("{}/{}", c, other.to_css_string(span)?)),
2020-04-12 19:37:12 -04:00
QuoteKind::None,
),
},
2020-04-01 15:32:52 -04:00
Self::BinaryOp(left, op, right) => {
if op.precedence() >= precedence {
2020-04-12 19:37:12 -04:00
Self::BinaryOp(left, op, right)
.eval(span)?
.node
.div(other, span)?
2020-04-01 15:32:52 -04:00
} else {
Self::BinaryOp(
left,
op,
2020-04-12 19:37:12 -04:00
Box::new(
Self::BinaryOp(right, Op::Div, Box::new(other))
.eval(span)?
.node,
),
2020-04-01 15:32:52 -04:00
)
2020-04-12 19:37:12 -04:00
.eval(span)?
.node
2020-04-01 15:32:52 -04:00
}
}
2020-04-12 19:37:12 -04:00
Self::Paren(..) => self.eval(span)?.node.div(other, span)?,
2020-03-23 12:12:08 -04:00
Self::Ident(s1, q1) => match other {
Self::Ident(s2, q2) => Value::Ident(
InternedString::get_or_intern(format!("{}{}{}/{}{}{}", q1, s1, q1, q2, s2, q2)),
QuoteKind::None,
),
Self::Important
| Self::True
| Self::False
| Self::Dimension(..)
| Self::Color(..) => Value::Ident(
InternedString::get_or_intern(format!(
"{}{}{}/{}",
q1,
s1,
q1,
other.to_css_string(span)?
)),
QuoteKind::None,
),
Self::Null => Value::Ident(
InternedString::get_or_intern(format!("{}{}{}/", q1, s1, q1)),
QuoteKind::None,
),
_ => todo!(),
},
_ => match other {
Self::Ident(s, q) => Value::Ident(
InternedString::get_or_intern(format!(
"{}/{}{}{}",
self.to_css_string(span)?,
q,
s,
q
)),
QuoteKind::None,
),
Self::Null => Value::Ident(
InternedString::get_or_intern(format!("{}/", self.to_css_string(span)?)),
2020-04-12 19:37:12 -04:00
QuoteKind::None,
),
_ => Value::Ident(
InternedString::get_or_intern(format!(
2020-04-12 19:37:12 -04:00
"{}/{}",
self.to_css_string(span)?,
other.to_css_string(span)?
)),
QuoteKind::None,
),
},
2020-02-17 10:27:04 -05:00
})
2020-02-08 16:07:37 -05:00
}
2020-03-18 11:39:58 -04:00
2020-04-12 19:37:12 -04:00
pub fn rem(self, other: Self, span: Span) -> SassResult<Self> {
2020-03-18 11:39:58 -04:00
Ok(match self {
Value::Dimension(n, u) => match other {
Value::Dimension(n2, u2) => {
if !u.comparable(&u2) {
2020-04-12 19:37:12 -04:00
return Err((format!("Incompatible units {} and {}.", u2, u), span).into());
2020-03-18 11:39:58 -04:00
}
if u == u2 {
Value::Dimension(n % n2, u)
} else if u == Unit::None {
Value::Dimension(n % n2, u2)
} else if u2 == Unit::None {
Value::Dimension(n % n2, u)
} else {
Value::Dimension(n, u)
}
}
_ => {
2020-04-12 19:37:12 -04:00
return Err((
format!(
"Undefined operation \"{} % {}\".",
Value::Dimension(n, u).to_css_string(span)?,
other.to_css_string(span)?
),
span,
2020-03-18 11:39:58 -04:00
)
2020-04-12 19:37:12 -04:00
.into())
2020-03-18 11:39:58 -04:00
}
},
2020-04-12 19:37:12 -04:00
_ => {
return Err((
format!(
"Undefined operation \"{} % {}\".",
self.to_css_string(span)?,
other.to_css_string(span)?
),
span,
)
.into())
}
2020-03-18 11:39:58 -04:00
})
}
2020-03-21 12:14:02 -04:00
2020-04-12 19:37:12 -04:00
pub fn neg(self, span: Span) -> SassResult<Self> {
Ok(match self.eval(span)?.node {
2020-03-21 12:14:02 -04:00
Value::Dimension(n, u) => Value::Dimension(-n, u),
v => Value::Ident(
InternedString::get_or_intern(format!("-{}", v.to_css_string(span)?)),
QuoteKind::None,
),
2020-03-21 12:14:02 -04:00
})
}
}