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,25 +186,24 @@ impl AtRule {
devour_whitespace_or_comment(toks);
if from < to {
for i in from..(to + through) {
scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None))?;
stmts.extend(eat_stmts(
&mut body.clone().into_iter().peekable(),
scope,
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,
)?);
}
let (mut x, mut y);
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))?;
stmts.extend(eat_stmts(
&mut body.clone().into_iter().peekable(),
scope,
super_selector,
)?);
}
AtRule::For(stmts)
}
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> {

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

View File

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

View File

@ -24,7 +24,7 @@ use super::number::Number;
fn parse_hex(s: &str) -> Value {
match s.len() {
3 => {
let v = match u16::from_str_radix(&s, 16) {
let v = match u16::from_str_radix(s, 16) {
Ok(a) => a,
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)))
}
4 => {
let v = match u16::from_str_radix(&s, 16) {
let v = match u16::from_str_radix(s, 16) {
Ok(a) => a,
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)))
}
6 => {
let v = match u32::from_str_radix(&s, 16) {
let v = match u32::from_str_radix(s, 16) {
Ok(a) => a,
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)))
}
8 => {
let v = match u32::from_str_radix(&s, 16) {
let v = match u32::from_str_radix(s, 16) {
Ok(a) => a,
Err(_) => return Value::Ident(format!("#{}", s), QuoteKind::None),
};
@ -258,7 +258,7 @@ impl Value {
| q @ TokenKind::Symbol(Symbol::SingleQuote) => {
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 => {
let mut s = parse_interpolation(toks, scope, super_selector)?.to_string();
while let Some(tok) = toks.peek() {