This commit is contained in:
ConnorSkees 2020-03-18 20:11:14 -04:00
parent 103781e420
commit 9bbdf762ce
7 changed files with 39 additions and 44 deletions

View File

@ -186,8 +186,16 @@ impl AtRule {
devour_whitespace_or_comment(toks); devour_whitespace_or_comment(toks);
if from < to { let (mut x, mut y);
for i in from..(to + through) { let iter: &mut dyn std::iter::Iterator<Item = usize> = if from < to {
x = from..(to + through);
&mut x
} else {
y = ((to - through)..(from + 1)).skip(1).rev();
&mut y
};
for i in iter {
scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None))?; scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None))?;
stmts.extend(eat_stmts( stmts.extend(eat_stmts(
&mut body.clone().into_iter().peekable(), &mut body.clone().into_iter().peekable(),
@ -195,16 +203,7 @@ impl AtRule {
super_selector, super_selector,
)?); )?);
} }
} else if from > to {
for i in ((to - through)..(from + 1)).skip(1).rev() {
scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None))?;
stmts.extend(eat_stmts(
&mut body.clone().into_iter().peekable(),
scope,
super_selector,
)?);
}
}
AtRule::For(stmts) AtRule::For(stmts)
} }
AtRuleKind::While => todo!("@while not yet implemented"), AtRuleKind::While => todo!("@while not yet implemented"),

View File

@ -65,7 +65,7 @@ impl Function {
} }
} }
Ok((name, Function::new(scope.clone(), args, body))) Ok((name, Function::new(scope, args, body)))
} }
pub fn args(mut self, args: &mut CallArgs) -> SassResult<Function> { pub fn args(mut self, args: &mut CallArgs) -> SassResult<Function> {

View File

@ -36,15 +36,15 @@ impl Scope {
} }
} }
pub fn vars(&self) -> &HashMap<String, Value> { pub const fn vars(&self) -> &HashMap<String, Value> {
&self.vars &self.vars
} }
pub fn get_var(&self, v: &str) -> SassResult<Value> { pub fn get_var(&self, v: &str) -> SassResult<Value> {
let v = &v.replace('_', "-"); let name = &v.replace('_', "-");
match self.vars.get(v) { match self.vars.get(name) {
Some(v) => Ok(v.clone()), Some(v) => Ok(v.clone()),
None => get_global_var(v), None => get_global_var(name),
} }
} }

View File

@ -1,6 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::f64::consts::PI; use std::f64::consts::PI;
use std::fmt; use std::fmt;
use std::string::ToString;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
@ -247,8 +248,6 @@ pub(crate) enum Unit {
/// Units multiplied together /// Units multiplied together
Mul(Vec<Unit>), Mul(Vec<Unit>),
/// A unit divided by another
Div(Box<Unit>, Box<Unit>),
} }
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub(crate) enum UnitKind { pub(crate) enum UnitKind {
@ -381,15 +380,12 @@ impl Into<String> for Unit {
Unit::Fr => "fr", Unit::Fr => "fr",
Unit::None => "", Unit::None => "",
Unit::Mul(u) => { Unit::Mul(u) => {
return format!( return u
"{}", .iter()
u.into_iter() .map(ToString::to_string)
.map(|x| x.to_string())
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("*") .join("*")
)
} }
Unit::Div(l, r) => return format!("{}/{}", l, r),
Unit::Unknown(ref s) => s, Unit::Unknown(ref s) => s,
} }
.into() .into()
@ -439,12 +435,11 @@ impl fmt::Display for Unit {
Unit::Mul(u) => write!( Unit::Mul(u) => write!(
f, f,
"{}", "{}",
u.into_iter() u.iter()
.map(|x| x.to_string()) .map(ToString::to_string)
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("*") .join("*")
), ),
Unit::Div(l, r) => write!(f, "{}/{}", l, r),
} }
} }
} }

View File

@ -30,7 +30,7 @@ impl Display for Value {
match self { match self {
Self::Important => write!(f, "!important"), Self::Important => write!(f, "!important"),
Self::Dimension(num, unit) => match unit { Self::Dimension(num, unit) => match unit {
Unit::Mul(..) | Unit::Div(..) => { Unit::Mul(..) => {
eprintln!("Error: {}{} isn't a valid CSS value.", num, unit); eprintln!("Error: {}{} isn't a valid CSS value.", num, unit);
std::process::exit(1); std::process::exit(1);
} }
@ -143,8 +143,8 @@ impl Value {
Self::Ident(s1, ..) => match other { Self::Ident(s1, ..) => match other {
Self::Ident(s2, ..) => s1 == s2, Self::Ident(s2, ..) => s1 == s2,
_ => false, _ => false,
} },
s @ _ => s == other.eval()?, s => s == other.eval()?,
}) })
} }

View File

@ -90,6 +90,7 @@ macro_rules! from_integer {
}; };
} }
// todo: implement std::convertTryFrom instead
impl From<f64> for Number { impl From<f64> for Number {
fn from(b: f64) -> Self { fn from(b: f64) -> Self {
Number { Number {

View File

@ -24,7 +24,7 @@ use super::number::Number;
fn parse_hex(s: &str) -> Value { fn parse_hex(s: &str) -> Value {
match s.len() { match s.len() {
3 => { 3 => {
let v = match u16::from_str_radix(&s, 16) { let v = match u16::from_str_radix(s, 16) {
Ok(a) => a, Ok(a) => a,
Err(_) => return Value::Ident(format!("#{}", s), QuoteKind::None), Err(_) => return Value::Ident(format!("#{}", s), QuoteKind::None),
}; };
@ -34,7 +34,7 @@ fn parse_hex(s: &str) -> Value {
Value::Color(Color::new(red, green, blue, 1, format!("#{}", s))) Value::Color(Color::new(red, green, blue, 1, format!("#{}", s)))
} }
4 => { 4 => {
let v = match u16::from_str_radix(&s, 16) { let v = match u16::from_str_radix(s, 16) {
Ok(a) => a, Ok(a) => a,
Err(_) => return Value::Ident(format!("#{}", s), QuoteKind::None), Err(_) => return Value::Ident(format!("#{}", s), QuoteKind::None),
}; };
@ -45,7 +45,7 @@ fn parse_hex(s: &str) -> Value {
Value::Color(Color::new(red, green, blue, alpha, format!("#{}", s))) Value::Color(Color::new(red, green, blue, alpha, format!("#{}", s)))
} }
6 => { 6 => {
let v = match u32::from_str_radix(&s, 16) { let v = match u32::from_str_radix(s, 16) {
Ok(a) => a, Ok(a) => a,
Err(_) => return Value::Ident(format!("#{}", s), QuoteKind::None), Err(_) => return Value::Ident(format!("#{}", s), QuoteKind::None),
}; };
@ -55,7 +55,7 @@ fn parse_hex(s: &str) -> Value {
Value::Color(Color::new(red, green, blue, 1, format!("#{}", s))) Value::Color(Color::new(red, green, blue, 1, format!("#{}", s)))
} }
8 => { 8 => {
let v = match u32::from_str_radix(&s, 16) { let v = match u32::from_str_radix(s, 16) {
Ok(a) => a, Ok(a) => a,
Err(_) => return Value::Ident(format!("#{}", s), QuoteKind::None), Err(_) => return Value::Ident(format!("#{}", s), QuoteKind::None),
}; };
@ -258,7 +258,7 @@ impl Value {
| q @ TokenKind::Symbol(Symbol::SingleQuote) => { | q @ TokenKind::Symbol(Symbol::SingleQuote) => {
parse_quoted_string(toks, scope, &q, super_selector) parse_quoted_string(toks, scope, &q, super_selector)
} }
TokenKind::Variable(ref v) => Ok(scope.get_var(v)?.clone()), TokenKind::Variable(ref v) => Ok(scope.get_var(v)?),
TokenKind::Interpolation => { TokenKind::Interpolation => {
let mut s = parse_interpolation(toks, scope, super_selector)?.to_string(); let mut s = parse_interpolation(toks, scope, super_selector)?.to_string();
while let Some(tok) = toks.peek() { while let Some(tok) = toks.peek() {