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-02-08 16:17:58 -05:00
|
|
|
use crate::common::{ListSeparator, Op, QuoteKind};
|
2020-02-17 10:27:04 -05:00
|
|
|
use crate::error::SassResult;
|
2020-03-19 16:24:31 -04:00
|
|
|
use crate::unit::Unit;
|
2020-02-08 18:43:18 -05:00
|
|
|
pub(crate) use number::Number;
|
2020-01-25 09:58:53 -05:00
|
|
|
|
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
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub(crate) enum Value {
|
|
|
|
Important,
|
|
|
|
True,
|
|
|
|
False,
|
|
|
|
Null,
|
2020-02-08 18:43:18 -05:00
|
|
|
Dimension(Number, Unit),
|
2020-01-25 09:58:53 -05:00
|
|
|
List(Vec<Value>, ListSeparator),
|
|
|
|
Color(Color),
|
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),
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-25 09:58:53 -05:00
|
|
|
Self::List(vals, sep) => write!(
|
|
|
|
f,
|
|
|
|
"{}",
|
|
|
|
vals.iter()
|
2020-02-08 17:03:43 -05:00
|
|
|
.map(std::string::ToString::to_string)
|
2020-01-25 09:58:53 -05:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(sep.as_str())
|
|
|
|
),
|
|
|
|
Self::Color(c) => write!(f, "{}", c),
|
2020-02-29 20:01:43 -05:00
|
|
|
Self::BinaryOp(..) => write!(
|
|
|
|
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 {
|
|
|
|
self == &Value::Null
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
Self::BinaryOp(..) => self.clone().eval()?.is_true(),
|
|
|
|
_ => 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-02-17 10:27:04 -05:00
|
|
|
Value::Color(..) => Ok("color"),
|
|
|
|
Value::Ident(..) => Ok("string"),
|
|
|
|
Value::Dimension(..) => Ok("number"),
|
|
|
|
Value::List(..) => Ok("list"),
|
|
|
|
// Value::Function(..) => Ok("function"),
|
|
|
|
Value::True | Value::False => Ok("bool"),
|
|
|
|
Value::Null => Ok("null"),
|
|
|
|
Value::BinaryOp(..) => self.clone().eval()?.kind(),
|
|
|
|
_ => Ok("unknown"),
|
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-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-02-17 10:27:04 -05:00
|
|
|
_ => Ok(Self::BinaryOp(lhs, op, rhs)),
|
2020-02-07 00:10:43 -05:00
|
|
|
},
|
2020-03-19 14:20:16 -04:00
|
|
|
Self::Paren(v) => v.eval(),
|
2020-02-17 10:27:04 -05:00
|
|
|
_ => Ok(self),
|
2020-02-03 07:56:21 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-25 09:58:53 -05:00
|
|
|
}
|