grass/src/value/mod.rs

109 lines
3.0 KiB
Rust
Raw Normal View History

2020-01-25 09:58:53 -05:00
#![allow(dead_code, unused_variables)]
use std::fmt::{self, Display};
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-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-03 07:56:21 -05:00
Self::BinaryOp(..) => write!(f, "{}", self.eval()),
Self::Paren(val) => write!(f, "{}", val),
2020-01-25 09:58:53 -05:00
Self::Ident(val, kind) => write!(f, "{}{}{}", kind.as_str(), val, kind.as_str()),
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-01-25 09:58:53 -05:00
pub fn is_true(&self) -> bool {
2020-02-08 16:01:21 -05:00
match self {
Value::Null | Value::False => false,
Self::BinaryOp(..) => self.eval().is_true(),
2020-02-08 16:17:58 -05:00
_ => 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),
_ => todo!(),
}
2020-01-25 09:58:53 -05:00
}
2020-02-03 07:56:21 -05:00
pub fn kind(&self) -> &'static str {
match self {
Value::Color(..) => "color",
Value::Ident(..) => "string",
Value::Dimension(..) => "number",
Value::List(..) => "list",
// Value::Function(..) => "function",
Value::True | Value::False => "bool",
Value::Null => "null",
Value::BinaryOp(..) => self.eval().kind(),
_ => "unknown",
}
}
2020-02-08 15:53:49 -05:00
pub fn bool(b: bool) -> Self {
if b {
Value::True
} else {
Value::False
}
}
2020-02-03 07:56:21 -05:00
pub fn eval(&self) -> Self {
match self {
Self::BinaryOp(lhs, op, rhs) => match op {
Op::Plus => *lhs.clone() + *rhs.clone(),
Op::Minus => *lhs.clone() - *rhs.clone(),
2020-02-08 15:53:49 -05:00
Op::Equal => Self::bool(*lhs == *rhs),
Op::NotEqual => Self::bool(*lhs != *rhs),
Op::Mul => *lhs.clone() * *rhs.clone(),
Op::Div => *lhs.clone() / *rhs.clone(),
2020-02-07 00:10:43 -05:00
_ => Self::BinaryOp(lhs.clone(), *op, rhs.clone()),
},
2020-02-03 07:56:21 -05:00
_ => self.clone(),
}
}
2020-01-25 09:58:53 -05:00
}