2020-03-30 15:43:15 -04:00
|
|
|
use std::cmp::Ordering;
|
2020-02-24 19:49:24 -05:00
|
|
|
use std::fmt::{self, Display, Write};
|
2020-02-08 16:17:58 -05:00
|
|
|
use std::iter::Iterator;
|
2020-01-25 09:58:53 -05:00
|
|
|
|
|
|
|
use crate::color::Color;
|
2020-03-23 19:56:24 -04:00
|
|
|
use crate::common::{Brackets, ListSeparator, Op, QuoteKind};
|
2020-02-17 10:27:04 -05:00
|
|
|
use crate::error::SassResult;
|
2020-03-30 10:42:13 -04:00
|
|
|
use crate::unit::{Unit, UNIT_CONVERSION_TABLE};
|
2020-03-30 15:43:15 -04:00
|
|
|
|
|
|
|
pub(crate) use map::SassMap;
|
2020-02-08 18:43:18 -05:00
|
|
|
pub(crate) use number::Number;
|
2020-01-25 09:58:53 -05:00
|
|
|
|
2020-03-30 15:43:15 -04:00
|
|
|
mod map;
|
2020-02-08 18:43:18 -05:00
|
|
|
mod number;
|
2020-02-08 16:07:37 -05:00
|
|
|
mod ops;
|
2020-02-08 16:17:58 -05:00
|
|
|
mod parse;
|
2020-01-25 09:58:53 -05:00
|
|
|
|
2020-03-30 15:43:15 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2020-01-25 09:58:53 -05:00
|
|
|
pub(crate) enum Value {
|
|
|
|
Important,
|
|
|
|
True,
|
|
|
|
False,
|
|
|
|
Null,
|
2020-02-08 18:43:18 -05:00
|
|
|
Dimension(Number, Unit),
|
2020-03-23 19:56:24 -04:00
|
|
|
List(Vec<Value>, ListSeparator, Brackets),
|
2020-01-25 09:58:53 -05:00
|
|
|
Color(Color),
|
2020-03-21 12:14:02 -04:00
|
|
|
UnaryOp(Op, Box<Value>),
|
2020-01-25 10:54:25 -05:00
|
|
|
BinaryOp(Box<Value>, Op, Box<Value>),
|
2020-01-25 09:58:53 -05:00
|
|
|
Paren(Box<Value>),
|
|
|
|
Ident(String, QuoteKind),
|
2020-03-30 15:43:15 -04:00
|
|
|
Map(SassMap),
|
2020-03-23 15:13:19 -04:00
|
|
|
// Returned by `get-function()`
|
2020-03-30 15:43:15 -04:00
|
|
|
// Function(String)
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Value {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Important => write!(f, "!important"),
|
2020-03-16 21:29:00 -04:00
|
|
|
Self::Dimension(num, unit) => match unit {
|
2020-03-18 20:11:14 -04:00
|
|
|
Unit::Mul(..) => {
|
2020-03-16 21:29:00 -04:00
|
|
|
eprintln!("Error: {}{} isn't a valid CSS value.", num, unit);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
_ => write!(f, "{}{}", num, unit),
|
|
|
|
},
|
2020-03-30 15:43:15 -04:00
|
|
|
Self::Map(map) => write!(
|
|
|
|
f,
|
|
|
|
"({})",
|
|
|
|
map.iter()
|
|
|
|
.map(|(k, v)| format!("{}: {}", k, v))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ")
|
|
|
|
),
|
2020-03-23 19:56:24 -04:00
|
|
|
Self::List(vals, sep, brackets) => match brackets {
|
|
|
|
Brackets::None => write!(
|
|
|
|
f,
|
|
|
|
"{}",
|
|
|
|
vals.iter()
|
2020-03-31 00:27:00 -04:00
|
|
|
.filter(|x| !x.is_null())
|
2020-03-23 19:56:24 -04:00
|
|
|
.map(std::string::ToString::to_string)
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(sep.as_str()),
|
|
|
|
),
|
|
|
|
Brackets::Bracketed => write!(
|
|
|
|
f,
|
|
|
|
"[{}]",
|
|
|
|
vals.iter()
|
2020-03-31 00:27:00 -04:00
|
|
|
.filter(|x| !x.is_null())
|
2020-03-23 19:56:24 -04:00
|
|
|
.map(std::string::ToString::to_string)
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(sep.as_str()),
|
|
|
|
),
|
|
|
|
},
|
2020-01-25 09:58:53 -05:00
|
|
|
Self::Color(c) => write!(f, "{}", c),
|
2020-03-21 12:14:02 -04:00
|
|
|
Self::UnaryOp(..) | Self::BinaryOp(..) => write!(
|
2020-02-29 20:01:43 -05:00
|
|
|
f,
|
|
|
|
"{}",
|
|
|
|
match self.clone().eval() {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("{}", e);
|
2020-02-29 20:02:58 -05:00
|
|
|
std::process::exit(1);
|
2020-02-29 20:01:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
),
|
2020-01-25 10:11:46 -05:00
|
|
|
Self::Paren(val) => write!(f, "{}", val),
|
2020-02-24 19:49:24 -05:00
|
|
|
Self::Ident(val, kind) => {
|
|
|
|
if kind == &QuoteKind::None {
|
|
|
|
return write!(f, "{}", val);
|
|
|
|
}
|
|
|
|
let has_single_quotes = val.contains(|x| x == '\'');
|
|
|
|
let has_double_quotes = val.contains(|x| x == '"');
|
|
|
|
if has_single_quotes && !has_double_quotes {
|
|
|
|
write!(f, "\"{}\"", val)
|
|
|
|
} else if !has_single_quotes && has_double_quotes {
|
|
|
|
write!(f, "'{}'", val)
|
2020-02-28 18:32:11 -05:00
|
|
|
} else if !has_single_quotes && !has_double_quotes {
|
2020-02-24 20:06:07 -05:00
|
|
|
write!(f, "\"{}\"", val)
|
2020-02-24 19:49:24 -05:00
|
|
|
} else {
|
|
|
|
let quote_char = match kind {
|
|
|
|
QuoteKind::Double => '"',
|
|
|
|
QuoteKind::Single => '\'',
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
f.write_char(quote_char)?;
|
|
|
|
for c in val.chars() {
|
|
|
|
match c {
|
|
|
|
'"' | '\'' if c == quote_char => {
|
|
|
|
f.write_char('\\')?;
|
|
|
|
f.write_char(quote_char)?;
|
|
|
|
}
|
|
|
|
v => f.write_char(v)?,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.write_char(quote_char)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 09:58:53 -05:00
|
|
|
Self::True => write!(f, "true"),
|
|
|
|
Self::False => write!(f, "false"),
|
|
|
|
Self::Null => write!(f, "null"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
2020-01-26 15:04:16 -05:00
|
|
|
pub fn is_null(&self) -> bool {
|
2020-03-22 18:36:21 -04:00
|
|
|
match self {
|
|
|
|
&Value::Null => true,
|
2020-03-22 22:28:54 -04:00
|
|
|
Value::Ident(i, QuoteKind::None) if i.is_empty() => true,
|
|
|
|
_ => false,
|
2020-03-22 18:36:21 -04:00
|
|
|
}
|
2020-01-26 15:04:16 -05:00
|
|
|
}
|
|
|
|
|
2020-02-17 10:27:04 -05:00
|
|
|
pub fn is_true(&self) -> SassResult<bool> {
|
2020-02-08 16:01:21 -05:00
|
|
|
match self {
|
2020-02-17 10:27:04 -05:00
|
|
|
Value::Null | Value::False => Ok(false),
|
2020-03-23 15:21:59 -04:00
|
|
|
Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => {
|
|
|
|
self.clone().eval()?.is_true()
|
|
|
|
}
|
2020-02-17 10:27:04 -05:00
|
|
|
_ => Ok(true),
|
2020-02-08 16:01:21 -05:00
|
|
|
}
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|
|
|
|
|
2020-02-02 21:11:22 -05:00
|
|
|
pub fn unquote(self) -> Self {
|
|
|
|
match self {
|
|
|
|
Self::Ident(s1, _) => Self::Ident(s1, QuoteKind::None),
|
2020-02-24 16:58:48 -05:00
|
|
|
v => v,
|
2020-02-02 21:11:22 -05:00
|
|
|
}
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|
|
|
|
|
2020-02-17 10:27:04 -05:00
|
|
|
pub fn kind(&self) -> SassResult<&'static str> {
|
2020-02-03 07:56:21 -05:00
|
|
|
match self {
|
2020-03-23 15:13:19 -04:00
|
|
|
Self::Color(..) => Ok("color"),
|
|
|
|
Self::Ident(..) | Self::Important => Ok("string"),
|
|
|
|
Self::Dimension(..) => Ok("number"),
|
|
|
|
Self::List(..) => Ok("list"),
|
|
|
|
// Self::Function(..) => Ok("function"),
|
|
|
|
Self::True | Self::False => Ok("bool"),
|
|
|
|
Self::Null => Ok("null"),
|
2020-03-30 15:43:15 -04:00
|
|
|
Self::Map(..) => Ok("map"),
|
2020-03-23 15:13:19 -04:00
|
|
|
Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => self.clone().eval()?.kind(),
|
2020-02-03 07:56:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 15:53:49 -05:00
|
|
|
pub fn bool(b: bool) -> Self {
|
|
|
|
if b {
|
|
|
|
Value::True
|
|
|
|
} else {
|
|
|
|
Value::False
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 17:23:38 -04:00
|
|
|
pub fn equals(self, other: Value) -> SassResult<bool> {
|
|
|
|
Ok(match self.eval()? {
|
|
|
|
Self::Ident(s1, ..) => match other {
|
|
|
|
Self::Ident(s2, ..) => s1 == s2,
|
|
|
|
_ => false,
|
2020-03-18 20:11:14 -04:00
|
|
|
},
|
|
|
|
s => s == other.eval()?,
|
2020-03-18 17:23:38 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-21 12:14:02 -04:00
|
|
|
pub fn unary_op_plus(self) -> SassResult<Self> {
|
|
|
|
Ok(match self.eval()? {
|
|
|
|
v @ Value::Dimension(..) => v,
|
|
|
|
v => Value::Ident(format!("+{}", v), QuoteKind::None),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-17 10:27:04 -05:00
|
|
|
pub fn eval(self) -> SassResult<Self> {
|
2020-02-03 07:56:21 -05:00
|
|
|
match self {
|
|
|
|
Self::BinaryOp(lhs, op, rhs) => match op {
|
2020-02-16 22:06:30 -05:00
|
|
|
Op::Plus => *lhs + *rhs,
|
|
|
|
Op::Minus => *lhs - *rhs,
|
2020-03-18 17:23:38 -04:00
|
|
|
Op::Equal => Ok(Self::bool(lhs.equals(*rhs)?)),
|
|
|
|
Op::NotEqual => Ok(Self::bool(!lhs.equals(*rhs)?)),
|
2020-02-16 22:06:30 -05:00
|
|
|
Op::Mul => *lhs * *rhs,
|
|
|
|
Op::Div => *lhs / *rhs,
|
2020-03-18 11:39:58 -04:00
|
|
|
Op::Rem => *lhs % *rhs,
|
2020-03-30 10:42:13 -04:00
|
|
|
Op::GreaterThan => match lhs.cmp(&rhs, op)? {
|
2020-03-30 15:43:15 -04:00
|
|
|
Ordering::Greater => Ok(Self::True),
|
|
|
|
Ordering::Less | Ordering::Equal => Ok(Self::False),
|
2020-03-30 10:42:13 -04:00
|
|
|
},
|
|
|
|
Op::GreaterThanEqual => match lhs.cmp(&rhs, op)? {
|
|
|
|
Ordering::Greater | Ordering::Equal => Ok(Self::True),
|
|
|
|
Ordering::Less => Ok(Self::False),
|
|
|
|
},
|
|
|
|
Op::LessThan => match lhs.cmp(&rhs, op)? {
|
2020-03-30 15:43:15 -04:00
|
|
|
Ordering::Less => Ok(Self::True),
|
|
|
|
Ordering::Greater | Ordering::Equal => Ok(Self::False),
|
2020-03-30 10:42:13 -04:00
|
|
|
},
|
|
|
|
Op::LessThanEqual => match lhs.cmp(&rhs, op)? {
|
|
|
|
Ordering::Less | Ordering::Equal => Ok(Self::True),
|
|
|
|
Ordering::Greater => Ok(Self::False),
|
|
|
|
},
|
2020-02-07 00:10:43 -05:00
|
|
|
},
|
2020-03-19 14:20:16 -04:00
|
|
|
Self::Paren(v) => v.eval(),
|
2020-03-21 12:14:02 -04:00
|
|
|
Self::UnaryOp(op, val) => match op {
|
|
|
|
Op::Plus => val.unary_op_plus(),
|
|
|
|
Op::Minus => -*val,
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
2020-02-17 10:27:04 -05:00
|
|
|
_ => Ok(self),
|
2020-02-03 07:56:21 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-30 10:42:13 -04:00
|
|
|
|
|
|
|
pub fn cmp(&self, other: &Self, op: Op) -> SassResult<Ordering> {
|
|
|
|
Ok(match self {
|
|
|
|
Self::Dimension(num, ref unit) => match other {
|
|
|
|
Self::Dimension(num2, unit2) => {
|
2020-03-30 17:06:23 -04:00
|
|
|
if !unit.comparable(unit2) {
|
2020-03-30 10:42:13 -04:00
|
|
|
return Err(format!("Incompatible units {} and {}.", unit2, unit).into());
|
|
|
|
}
|
|
|
|
if unit == unit2 {
|
|
|
|
num.cmp(num2)
|
|
|
|
} else if unit == &Unit::None {
|
|
|
|
num.cmp(num2)
|
|
|
|
} else if unit2 == &Unit::None {
|
|
|
|
num.cmp(num2)
|
|
|
|
} else {
|
2020-03-30 15:43:15 -04:00
|
|
|
num.cmp(
|
|
|
|
&(num2.clone()
|
|
|
|
* UNIT_CONVERSION_TABLE[&unit.to_string()][&unit2.to_string()]
|
|
|
|
.clone()),
|
|
|
|
)
|
2020-03-30 10:42:13 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-30 15:43:15 -04:00
|
|
|
_ => {
|
|
|
|
return Err(
|
|
|
|
format!("Undefined operation \"{} {} {}\".", self, op, other).into(),
|
|
|
|
)
|
|
|
|
}
|
2020-03-30 10:42:13 -04:00
|
|
|
},
|
2020-03-30 15:43:15 -04:00
|
|
|
_ => return Err(format!("Undefined operation \"{} {} {}\".", self, op, other).into()),
|
2020-03-30 10:42:13 -04:00
|
|
|
})
|
|
|
|
}
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|