From 44ff1c99b3f3f5e5f05ab91173a0e1d8910e3d4f Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 6 Apr 2020 00:11:18 -0400 Subject: [PATCH] add helper function Value::from_vec --- src/args.rs | 18 +++----------- src/atrule/for_rule.rs | 17 ++++++------- src/atrule/if_rule.rs | 8 +----- src/atrule/mod.rs | 12 +++------ src/style.rs | 6 ++--- src/utils.rs | 12 +++------ src/value/parse.rs | 56 ++++++++++++++---------------------------- 7 files changed, 39 insertions(+), 90 deletions(-) diff --git a/src/args.rs b/src/args.rs index 6018aec..8d8456c 100644 --- a/src/args.rs +++ b/src/args.rs @@ -68,11 +68,7 @@ impl CallArgs { super_selector: &Selector, ) -> Option> { match self.0.remove(&CallArg::Named(val)) { - Some(v) => Some(Value::from_tokens( - &mut v.into_iter().peekable(), - scope, - super_selector, - )), + Some(v) => Some(Value::from_vec(v, scope, super_selector)), None => None, } } @@ -87,11 +83,7 @@ impl CallArgs { super_selector: &Selector, ) -> Option> { match self.0.remove(&CallArg::Positional(val)) { - Some(v) => Some(Value::from_tokens( - &mut v.into_iter().peekable(), - scope, - super_selector, - )), + Some(v) => Some(Value::from_vec(v, scope, super_selector)), None => None, } } @@ -105,11 +97,7 @@ impl CallArgs { .collect::)>>>()?; args.sort_by(|(a1, _), (a2, _)| a1.cmp(a2)); for arg in args { - vals.push(Value::from_tokens( - &mut arg.1.into_iter().peekable(), - scope, - super_selector, - )?); + vals.push(Value::from_vec(arg.1, scope, super_selector)?); } Ok(vals) } diff --git a/src/atrule/for_rule.rs b/src/atrule/for_rule.rs index 9234f23..cba6650 100644 --- a/src/atrule/for_rule.rs +++ b/src/atrule/for_rule.rs @@ -98,18 +98,17 @@ pub(crate) fn parse_for>( _ => from_toks.extend(these_toks), } } - let from = - match Value::from_tokens(&mut from_toks.into_iter().peekable(), scope, super_selector)? { - Value::Dimension(n, _) => match n.to_integer().to_usize() { - Some(v) => v, - None => return Err(format!("{} is not a int.", n).into()), - }, - v => return Err(format!("{} is not an integer.", v).into()), - }; + let from = match Value::from_vec(from_toks, scope, super_selector)? { + Value::Dimension(n, _) => match n.to_integer().to_usize() { + Some(v) => v, + None => return Err(format!("{} is not a int.", n).into()), + }, + v => return Err(format!("{} is not an integer.", v).into()), + }; devour_whitespace(toks); let to_toks = read_until_open_curly_brace(toks); toks.next(); - let to = match Value::from_tokens(&mut to_toks.into_iter().peekable(), scope, super_selector)? { + let to = match Value::from_vec(to_toks, scope, super_selector)? { Value::Dimension(n, _) => match n.to_integer().to_usize() { Some(v) => v, None => return Err(format!("{} is not a int.", n).into()), diff --git a/src/atrule/if_rule.rs b/src/atrule/if_rule.rs index b503076..25f2fc0 100644 --- a/src/atrule/if_rule.rs +++ b/src/atrule/if_rule.rs @@ -95,13 +95,7 @@ impl If { let mut toks = Vec::new(); let mut found_true = false; for branch in self.branches { - if Value::from_tokens( - &mut branch.cond.into_iter().peekable(), - scope, - super_selector, - )? - .is_true()? - { + if Value::from_vec(branch.cond, scope, super_selector)?.is_true()? { toks = branch.toks; found_true = true; break; diff --git a/src/atrule/mod.rs b/src/atrule/mod.rs index 8a48ab6..1b26a5f 100644 --- a/src/atrule/mod.rs +++ b/src/atrule/mod.rs @@ -128,8 +128,8 @@ impl AtRule { return Err("Expected \"in\".".into()); } devour_whitespace(toks); - let iterator = match Value::from_tokens( - &mut read_until_open_curly_brace(toks).into_iter().peekable(), + let iterator = match Value::from_vec( + read_until_open_curly_brace(toks), scope, super_selector, )? { @@ -201,13 +201,7 @@ impl AtRule { devour_whitespace(toks); - while Value::from_tokens( - &mut cond.clone().into_iter().peekable(), - scope, - super_selector, - )? - .is_true()? - { + while Value::from_vec(cond.clone(), scope, super_selector)?.is_true()? { stmts.extend(eat_stmts( &mut body.clone().into_iter().peekable(), scope, diff --git a/src/style.rs b/src/style.rs index 512f662..cb14eba 100644 --- a/src/style.rs +++ b/src/style.rs @@ -71,10 +71,8 @@ impl<'a> StyleParser<'a> { scope: &Scope, ) -> SassResult { devour_whitespace(toks); - Value::from_tokens( - &mut read_until_semicolon_or_open_or_closing_curly_brace(toks) - .into_iter() - .peekable(), + Value::from_vec( + read_until_semicolon_or_open_or_closing_curly_brace(toks), scope, self.super_selector, ) diff --git a/src/utils.rs b/src/utils.rs index 4f71133..54f148c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -65,11 +65,7 @@ pub(crate) fn parse_interpolation>( scope: &Scope, super_selector: &Selector, ) -> SassResult { - let val = Value::from_tokens( - &mut read_until_closing_curly_brace(toks).into_iter().peekable(), - scope, - super_selector, - )?; + let val = Value::from_vec(read_until_closing_curly_brace(toks), scope, super_selector)?; toks.next(); Ok(val.eval()?.unquote()) } @@ -323,7 +319,7 @@ pub(crate) fn eat_variable_value>( if toks.peek().is_some() && toks.peek().unwrap().kind == ';' { toks.next(); } - let mut x = Vec::new(); + let mut val_toks = Vec::new(); while let Some(tok) = raw.next() { match tok.kind { '!' => { @@ -355,12 +351,12 @@ pub(crate) fn eat_variable_value>( _ => return Err("Invalid flag name.".into()), } } - _ => x.push(tok), + _ => val_toks.push(tok), } } devour_whitespace(toks); - let val = Value::from_tokens(&mut x.into_iter().peekable(), scope, super_selector)?; + let val = Value::from_vec(val_toks, scope, super_selector)?; Ok(VariableDecl::new(val, default, global)) } diff --git a/src/value/parse.rs b/src/value/parse.rs index b69d14c..355742f 100644 --- a/src/value/parse.rs +++ b/src/value/parse.rs @@ -159,22 +159,14 @@ fn parse_paren( let paren_toks = &mut t.into_iter().peekable(); let mut map = SassMap::new(); - let key = Value::from_tokens( - &mut read_until_char(paren_toks, ':').into_iter().peekable(), - scope, - super_selector, - )?; + let key = Value::from_vec(read_until_char(paren_toks, ':'), scope, super_selector)?; if paren_toks.peek().is_none() { space_separated.push(Value::Paren(Box::new(key))); return Ok(()); } - let val = Value::from_tokens( - &mut read_until_char(paren_toks, ',').into_iter().peekable(), - scope, - super_selector, - )?; + let val = Value::from_vec(read_until_char(paren_toks, ','), scope, super_selector)?; map.insert(key, val); @@ -184,17 +176,9 @@ fn parse_paren( } loop { - let key = Value::from_tokens( - &mut read_until_char(paren_toks, ':').into_iter().peekable(), - scope, - super_selector, - )?; + let key = Value::from_vec(read_until_char(paren_toks, ':'), scope, super_selector)?; devour_whitespace(paren_toks); - let val = Value::from_tokens( - &mut read_until_char(paren_toks, ',').into_iter().peekable(), - scope, - super_selector, - )?; + let val = Value::from_vec(read_until_char(paren_toks, ','), scope, super_selector)?; devour_whitespace(paren_toks); if map.insert(key, val) { return Err("Duplicate key.".into()); @@ -289,17 +273,13 @@ fn single_value>( }, IntermediateValue::Whitespace => unreachable!(), IntermediateValue::Comma => return Err("Expected expression.".into()), - IntermediateValue::Bracketed(t) => { - match Value::from_tokens(&mut t.into_iter().peekable(), scope, super_selector)? { - Value::List(v, sep, Brackets::None) => Value::List(v, sep, Brackets::Bracketed), - v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed), - } + IntermediateValue::Bracketed(t) => match Value::from_vec(t, scope, super_selector)? { + Value::List(v, sep, Brackets::None) => Value::List(v, sep, Brackets::Bracketed), + v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed), + }, + IntermediateValue::Paren(t) => { + Value::Paren(Box::new(Value::from_vec(t, scope, super_selector)?)) } - IntermediateValue::Paren(t) => Value::Paren(Box::new(Value::from_tokens( - &mut t.into_iter().peekable(), - scope, - super_selector, - )?)), }) } @@ -334,14 +314,14 @@ impl Value { )); } } - IntermediateValue::Bracketed(t) => space_separated.push(match Value::from_tokens( - &mut t.into_iter().peekable(), - scope, - super_selector, - )? { - Value::List(v, sep, Brackets::None) => Value::List(v, sep, Brackets::Bracketed), - v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed), - }), + IntermediateValue::Bracketed(t) => { + space_separated.push(match Value::from_vec(t, scope, super_selector)? { + Value::List(v, sep, Brackets::None) => { + Value::List(v, sep, Brackets::Bracketed) + } + v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed), + }) + } IntermediateValue::Paren(t) => { parse_paren(t, scope, super_selector, &mut space_separated)?; }