grass/src/value/mod.rs

139 lines
4.3 KiB
Rust
Raw Normal View History

2020-01-25 09:58:53 -05:00
#![allow(dead_code, unused_variables)]
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-01-25 09:58:53 -05:00
use crate::units::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"),
Self::Dimension(num, unit) => write!(f, "{}{}", num, unit),
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-17 10:27:04 -05:00
Self::BinaryOp(..) => write!(f, "{}", self.clone().eval().unwrap()),
Self::Paren(val) => write!(f, "{}", val),
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)
} 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
}
pub fn unquote(self) -> Self {
match self {
Self::Ident(s1, _) => Self::Ident(s1, QuoteKind::None),
v => v,
}
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-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 {
Op::Plus => *lhs + *rhs,
Op::Minus => *lhs - *rhs,
2020-02-17 10:27:04 -05:00
Op::Equal => Ok(Self::bool(*lhs == *rhs)),
Op::NotEqual => Ok(Self::bool(*lhs != *rhs)),
Op::Mul => *lhs * *rhs,
Op::Div => *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-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
}