From 33c5acc35f2e56a341862da9b8e854eedc4062ac Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Fri, 22 May 2020 22:43:26 -0400 Subject: [PATCH] use Identifier for FuncArg --- src/args.rs | 20 ++++++++++---------- src/atrule/function.rs | 10 ++++++---- src/atrule/mixin.rs | 5 +++-- src/common.rs | 6 ++++++ 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/args.rs b/src/args.rs index 3c1d340..c02ddae 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::mem; use codemap::{Span, Spanned}; @@ -20,7 +21,7 @@ pub(crate) struct FuncArgs(pub Vec); #[derive(Debug, Clone, Eq, PartialEq)] pub(crate) struct FuncArg { - pub name: String, + pub name: Identifier, pub default: Option>, pub is_variadic: bool, } @@ -128,10 +129,10 @@ impl CallArgs { } } - pub fn get( + pub fn get>( &mut self, position: usize, - name: String, + name: T, scope: &Scope, super_selector: &Selector, ) -> Option>> { @@ -244,7 +245,7 @@ pub(crate) fn eat_func_args>( ',' => { toks.next(); args.push(FuncArg { - name: name.replace('_', "-"), + name: name.node.into(), default: Some(default), is_variadic, }); @@ -252,7 +253,7 @@ pub(crate) fn eat_func_args>( } ')' => { args.push(FuncArg { - name: name.replace('_', "-"), + name: name.node.into(), default: Some(default), is_variadic, }); @@ -285,7 +286,7 @@ pub(crate) fn eat_func_args>( is_variadic = true; args.push(FuncArg { - name: name.replace('_', "-"), + name: name.node.into(), default: Some(default), is_variadic, }); @@ -294,7 +295,7 @@ pub(crate) fn eat_func_args>( ')' => { close_paren_span = span; args.push(FuncArg { - name: name.replace('_', "-"), + name: name.node.into(), default: if default.is_empty() { None } else { @@ -305,7 +306,7 @@ pub(crate) fn eat_func_args>( break; } ',' => args.push(FuncArg { - name: name.replace('_', "-"), + name: name.node.into(), default: None, is_variadic, }), @@ -399,9 +400,8 @@ pub(crate) fn eat_call_args>( } else { CallArg::Named(name.as_str().into()) }, - val.clone(), + mem::take(&mut val), ); - val.clear(); devour_whitespace(toks); if toks.peek().is_none() { diff --git a/src/atrule/function.rs b/src/atrule/function.rs index c72da9a..071a2ce 100644 --- a/src/atrule/function.rs +++ b/src/atrule/function.rs @@ -1,3 +1,5 @@ +use std::mem; + use super::eat_stmts; use codemap::{Span, Spanned}; @@ -75,7 +77,7 @@ impl Function { let span = args.span(); let arg_list = Value::ArgList(args.get_variadic(&scope, super_selector)?); self.scope.insert_var( - &arg.name, + arg.name.clone(), Spanned { node: arg_list, span, @@ -87,7 +89,7 @@ impl Function { Some(v) => v?, None => match arg.default.as_mut() { Some(v) => Value::from_tokens( - &mut std::mem::take(v).into_iter().peekmore(), + &mut mem::take(v).into_iter().peekmore(), &scope, super_selector, )?, @@ -98,8 +100,8 @@ impl Function { } }, }; - scope.insert_var(&arg.name, val.clone())?; - self.scope.insert_var(&arg.name, val)?; + scope.insert_var(arg.name.clone(), val.clone())?; + self.scope.insert_var(mem::take(&mut arg.name), val)?; } Ok(()) } diff --git a/src/atrule/mixin.rs b/src/atrule/mixin.rs index 07476ec..f0e4f82 100644 --- a/src/atrule/mixin.rs +++ b/src/atrule/mixin.rs @@ -1,3 +1,4 @@ +use std::mem; use std::vec::IntoIter; use codemap::{Span, Spanned}; @@ -67,7 +68,7 @@ impl Mixin { if arg.is_variadic { let span = args.span(); self.scope.insert_var( - &arg.name, + mem::take(&mut arg.name), Spanned { node: Value::ArgList(args.get_variadic(scope, super_selector)?), span, @@ -90,7 +91,7 @@ impl Mixin { } }, }; - self.scope.insert_var(&arg.name, val)?; + self.scope.insert_var(mem::take(&mut arg.name), val)?; } Ok(self) } diff --git a/src/common.rs b/src/common.rs index 163fece..34a9ba3 100644 --- a/src/common.rs +++ b/src/common.rs @@ -146,3 +146,9 @@ impl Display for Identifier { write!(f, "{}", self.0) } } + +impl Default for Identifier { + fn default() -> Self { + Self(String::new()) + } +}