grass/src/value/ops.rs

342 lines
15 KiB
Rust
Raw Normal View History

2020-02-09 18:28:24 -05:00
use std::ops::{Add, Div, Mul, Sub};
2020-02-08 16:07:37 -05:00
use crate::common::QuoteKind;
2020-02-17 10:27:04 -05:00
use crate::error::SassResult;
use crate::units::{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-02-08 16:07:37 -05:00
impl Add for Value {
2020-02-17 10:27:04 -05:00
type Output = SassResult<Self>;
2020-02-08 16:07:37 -05:00
2020-02-17 10:27:04 -05:00
fn add(self, other: Self) -> Self::Output {
Ok(match self {
2020-02-08 16:07:37 -05:00
Self::Important | Self::True | Self::False => match other {
Self::Ident(s, QuoteKind::Double) | Self::Ident(s, QuoteKind::Single) => {
Value::Ident(format!("{}{}", self, s), QuoteKind::Double)
}
Self::Null => Value::Ident(self.to_string(), QuoteKind::None),
_ => Value::Ident(format!("{}{}", self, other), QuoteKind::None),
},
Self::Null => match other {
Self::Null => Self::Null,
_ => Value::Ident(format!("{}", other), QuoteKind::None),
},
Self::Dimension(num, unit) => match other {
2020-03-01 14:53:52 -05:00
Self::Dimension(num2, unit2) => {
if !unit.comparable(&unit2) {
return Err(format!("Incompatible units {} and {}.", unit2, unit).into());
}
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()][&unit2.to_string()]
.clone(),
unit,
)
2020-03-01 14:53:52 -05:00
}
}
Self::Ident(s, q) => {
let quotes = match q {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}{}{}", num, unit, s), quotes)
}
2020-02-17 10:39:32 -05:00
Self::Null => Value::Ident(format!("{}{}", num, unit), QuoteKind::None),
2020-02-29 20:01:43 -05:00
_ => {
return Err(
format!("Undefined operation \"{}{} + {}\".", num, unit, other).into(),
)
}
2020-02-08 16:07:37 -05:00
},
// Self::List(..) => todo!(),
2020-03-01 17:06:55 -05:00
Self::Color(c) => match other {
Self::Ident(s, QuoteKind::Double) | Self::Ident(s, QuoteKind::Single) => {
Value::Ident(format!("{}{}", c, s), QuoteKind::Double)
}
2020-02-22 11:59:33 -05:00
Self::Ident(s, QuoteKind::None) => {
Value::Ident(format!("{}{}", c, s), QuoteKind::None)
}
Self::Null => Value::Ident(c.to_string(), QuoteKind::None),
2020-02-17 10:39:32 -05:00
_ => return Err(format!("Undefined operation \"{} + {}\".", c, other).into()),
},
2020-03-01 07:42:12 -05:00
Self::BinaryOp(..) | Self::Paren(..) => self.eval()?,
2020-02-08 16:07:37 -05:00
// Self::Paren(..) => todo!(),
Self::Ident(s1, quotes1) => match other {
Self::Ident(s2, quotes2) => {
let quotes = match (quotes1, quotes2) {
(QuoteKind::Double, _)
| (QuoteKind::Single, _)
| (_, QuoteKind::Double)
| (_, QuoteKind::Single) => QuoteKind::Double,
_ => QuoteKind::None,
};
Value::Ident(format!("{}{}", s1, s2), quotes)
}
Self::Important | Self::True | Self::False | Self::Dimension(..) => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}{}", s1, other), quotes)
}
Self::Null => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(s1, quotes)
}
Self::Color(c) => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}{}", s1, c), quotes)
}
Self::BinaryOp(..) | Self::Paren(..) => {
return Self::Ident(s1, quotes1) + other.eval()?
}
2020-03-01 07:42:12 -05:00
Self::List(..) => todo!(),
2020-02-08 16:07:37 -05:00
},
_ => todo!(),
2020-02-17 10:27:04 -05:00
})
2020-02-08 16:07:37 -05:00
}
}
impl Sub for Value {
2020-02-17 10:27:04 -05:00
type Output = SassResult<Self>;
2020-02-08 16:07:37 -05:00
2020-02-17 10:27:04 -05:00
fn sub(self, other: Self) -> Self::Output {
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) {
return Err(format!("Incompatible units {} and {}.", unit2, unit).into());
}
if unit == unit2 {
Value::Dimension(num - num2, unit)
} else {
todo!("unit conversions")
}
}
2020-02-08 16:07:37 -05:00
_ => todo!(),
},
// Self::List(..) => todo!(),
2020-02-09 11:07:13 -05:00
Self::Color(c) => match other {
Self::Ident(s, quotes) => {
let quotes = match quotes {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}-{}{}{}", c, quotes, s, quotes), QuoteKind::None)
}
Self::Null => Value::Ident(format!("{}-", c), QuoteKind::None),
2020-02-29 20:14:51 -05:00
Self::Dimension(..) | Self::Color(..) => {
return Err(format!("Undefined operation \"{} - {}\".", c, other).into())
2020-02-29 20:01:43 -05:00
}
_ => Value::Ident(format!("{}-{}", c, other), QuoteKind::None),
2020-02-09 11:07:13 -05:00
},
2020-02-17 10:27:04 -05:00
Self::BinaryOp(..) | Self::Paren(..) => self.eval()?,
2020-02-08 16:07:37 -05:00
Self::Ident(s1, quotes1) => match other {
Self::Ident(s2, quotes2) => {
let quotes1 = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
let quotes2 = match quotes2 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(
format!("{}{}{}-{}{}{}", quotes1, s1, quotes1, quotes2, s2, quotes2),
QuoteKind::None,
)
}
Self::Important
| Self::True
| Self::False
| Self::Dimension(..)
| Self::Color(..) => {
2020-02-08 16:07:37 -05:00
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(
format!("{}{}{}-{}", quotes, s1, quotes, other),
QuoteKind::None,
)
}
Self::Null => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}{}{}-", quotes, s1, quotes), QuoteKind::None)
}
_ => todo!(),
},
_ => match other {
Self::Ident(s, quotes) => {
let quotes = match quotes {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
2020-02-09 18:28:24 -05:00
Value::Ident(
format!("{}-{}{}{}", self, quotes, s, quotes),
QuoteKind::None,
)
}
Self::Null => Value::Ident(format!("{}-", self), QuoteKind::None),
_ => Value::Ident(format!("{}-{}", self, other), QuoteKind::None),
},
2020-02-17 10:27:04 -05:00
})
}
}
impl Mul for Value {
2020-02-17 10:27:04 -05:00
type Output = SassResult<Self>;
2020-02-17 10:27:04 -05:00
fn mul(self, other: Self) -> Self::Output {
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
}
}
Self::BinaryOp(..) | Self::Paren(..) => {
return Self::Dimension(num, unit) * other.eval()?
}
2020-02-29 20:01:43 -05:00
_ => {
return Err(
format!("Undefined operation \"{}{} * {}\".", num, unit, other).into(),
)
}
},
2020-02-17 10:27:04 -05:00
Self::BinaryOp(..) | Self::Paren(..) => self.eval()?,
2020-02-29 20:14:51 -05:00
_ => return Err(format!("Undefined operation \"{} * {}\".", self, other).into()),
2020-02-17 10:27:04 -05:00
})
}
}
impl Div for Value {
2020-02-17 10:27:04 -05:00
type Output = SassResult<Self>;
2020-02-17 10:27:04 -05:00
fn div(self, other: Self) -> Self::Output {
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 == unit2 {
Value::Dimension(num / num2, Unit::None)
} else {
todo!("unit conversions")
}
}
Self::Ident(s, q) => {
let quotes = match q {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(
format!("{}{}/{}{}{}", num, unit, quotes, s, quotes),
QuoteKind::None,
)
}
2020-02-17 10:27:04 -05:00
Self::BinaryOp(..) | Self::Paren(..) => {
(Self::Dimension(num, unit) / other.eval()?)?
}
_ => todo!(),
},
// Self::List(..) => todo!(),
Self::Color(c) => match other {
Self::Ident(s, quotes) => {
let quotes = match quotes {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}/{}{}{}", c, quotes, s, quotes), QuoteKind::None)
}
Self::Null => Value::Ident(format!("{}/", c), QuoteKind::None),
2020-02-29 20:14:51 -05:00
Self::Dimension(..) | Self::Color(..) => {
2020-02-29 20:01:43 -05:00
return Err(format!("Undefined operation \"{} / {}\".", c, other).into())
}
_ => Value::Ident(format!("{}/{}", c, other), QuoteKind::None),
},
2020-02-17 10:27:04 -05:00
Self::BinaryOp(..) | Self::Paren(..) => self.eval()?,
Self::Ident(s1, quotes1) => match other {
Self::Ident(s2, quotes2) => {
let quotes1 = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
let quotes2 = match quotes2 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(
format!("{}{}{}/{}{}{}", quotes1, s1, quotes1, quotes2, s2, quotes2),
QuoteKind::None,
)
}
Self::Important
| Self::True
| Self::False
| Self::Dimension(..)
| Self::Color(..) => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(
format!("{}{}{}/{}", quotes, s1, quotes, other),
QuoteKind::None,
)
}
Self::Null => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}{}{}/", quotes, s1, quotes), QuoteKind::None)
}
_ => todo!(),
},
_ => match other {
Self::Ident(s, quotes) => {
let quotes = match quotes {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
2020-02-09 18:28:24 -05:00
Value::Ident(
format!("{}/{}{}{}", self, quotes, s, quotes),
QuoteKind::None,
)
}
Self::Null => Value::Ident(format!("{}/", self), QuoteKind::None),
_ => Value::Ident(format!("{}/{}", self, other), QuoteKind::None),
},
2020-02-17 10:27:04 -05:00
})
2020-02-08 16:07:37 -05:00
}
}